String manipulation with batch scripting

214 Views Asked by At

I need to save the variable in %%c temporarily which comes from a for loop. But when I try to do that, the content changes unexpectedly. Some space characters appear at the end of the string. The content of %%c is a.jpg by the way.

        echo %%ca                 REM prints a.jpga

        set temp=%%c    
        set temp=!temp!

        echo !temp!a              REM prints a.jpg  a

I tried the code below to get rid of the extra spaces after initializing the temp variable. But it gave me an error: "=%" was unexpected at this time". What am I missing? Thanks in advance!

        set "this=!temp!"
        set "this=%this:* =%"
        call set "this=%%temp:%this%=%%"
        set "this=%this:~0,-1%"
        echo %this%a
1

There are 1 best solutions below

1
On BEST ANSWER

your line set temp=%%c is the reason. There are spaces at the end.

Use this syntax to avoid unintended spaces:

set "temp=%%c"