Search code examples
windowsfor-loopbatch-filetoken

batch file how to get all tokens from a string


I have an issue with going through all tokens I get in a for loop. I have a part of the batch file:

for %%f in ("*.my_file_type") do (
    set BaseName=%%~nf
    set ExportName=
    
    for /f "delims=_" %%a in ("!BaseName!") do (
        echo Word is %%a
        call :CapitalizeFirstLetter %%a
        set ExportName=!ExportName!!CapWord!
    )
...
)

So, the issue is that I am getting only first token, but I want all of them. For example, I would have my files:

my_file_name
file_name
this_is_another_file_name

And I want to get correct file names. Right now, I want them like this:

MyFileName
FileName
ThisIsAnotherFileName

And right now, the issue is that I would get only this:

My
File
This

Thanks for the help in advance


Solution

  • for %%f in ("*.my_file_type") do (
        set BaseName=%%~nf
        set ExportName=
        
        for %%a in (!BaseName:_= !) do (
            echo Word is %%a
            call :CapitalizeFirstLetter %%a
            set ExportName=!ExportName!!CapWord!
        )
    ...
    )
    

    should fix your problem (untried)

    ---odd -----

    This works:

    SET "filenames=my_file_name file_name this_is_another_file_name"
    for %%f in (%filenames%) do (
        set BaseName=%%f
        set ExportName=
        SET "basename=!BaseName:_= !"
        
        for %%a in (!BaseName!) do (
            echo Word is %%a
    rem        call :CapitalizeFirstLetter %%a
            set ExportName=!ExportName!!CapWord!
        )
        ECHO "!exportname!"
    )