Page 1 of 1

[SOLVED] SET adds space to variable?

Posted: 27 May 2020 09:51
by Shohreh
Hello,

In a FOR loop, I need to create a couple of variables so I can make use of them later.

For some reason, the following adds a space between the left side of the filename and its extension:

Code: Select all

REM c:\test.bat input.txt
FOR %%f IN ("%1") DO SET left=%%~nf & SET ext=%%~xf
ECHO %left%%ext%
REM Displays input .txt instead of input.txt
Do you know why? Is there a better solution?

Thank you.

Re: SET adds space to variable?

Posted: 27 May 2020 10:52
by Squashman
You are assigning spaces to the left variable.
Best practice is to use quotes to surround the assignment

Code: Select all

FOR %%f IN ("%~1") DO SET "left=%%~nf" & SET "ext=%%~xf"

Re: SET adds space to variable?

Posted: 27 May 2020 12:12
by Shohreh
Thank you.