-1

I've inherited a batch file which reads major, minor and build numbers from a text file as part of a long-established build process - the actual reading part code is a simple FOR /F:

FOR /F "tokens=1,2,3* delims=." %%A IN (C:\MyBuildArea\version.txt) DO (
  SET Major=%%A
  SET Minor=%%B
  SET Build=%%C
)

However this is followed by the line:

SET Build=%Build: =%

I don't understand this last line - what it is doing. Guessing it might be trying to catch and remove a trailing space (?) from the original file read as the FOR /F was delimited on dot (".") not space.

Hoping someone can help me understand - is this line redundant or serving some purpose?

n4m16
  • 121
  • 8
  • The command `SET Build=%Build: =%` removes __all__ spaces from the string value assigned to the environment variable `Build`. I suppose the file `C:\MyBuildArea\version.txt` is created sometimes or even always with one or even more trailing spaces. This line could be removed on using `FOR /F "tokens=1-3 delims=. " %%A IN (C:\MyBuildArea\version.txt) DO (` which results not only in splitting up the version string into three version numbers, but also in removal of all leading and trailing spaces. So all three environment variables would be defined without any space. – Mofi Mar 31 '22 at 12:04
  • See also my answer on [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) It describes the recommended syntax to define environment variables in a Windows batch file using the [Windows command](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) __SET__ which avoids that a trailing space in the batch file itself becomes part of an environment variable string value. – Mofi Mar 31 '22 at 12:06

1 Answers1

1

The command SET Build=%Build: =% will simply substitute all spaces with nothing, i.e. it will remove all spaces in the text stored in Build.

See set /? on command prompt for details.

Wisblade
  • 1,483
  • 4
  • 13
  • So it is indeed likely trying to catch + handle erroneous trailing spaces from the original file... Thanks... – n4m16 Mar 31 '22 at 11:41