[SOLVED] SET adds space to variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Shohreh
Posts: 33
Joined: 26 Feb 2020 08:05

[SOLVED] SET adds space to variable?

#1 Post by Shohreh » 27 May 2020 09:51

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.
Last edited by Shohreh on 27 May 2020 12:12, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: SET adds space to variable?

#2 Post by Squashman » 27 May 2020 10:52

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"

Shohreh
Posts: 33
Joined: 26 Feb 2020 08:05

Re: SET adds space to variable?

#3 Post by Shohreh » 27 May 2020 12:12

Thank you.

Post Reply