Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this batch command mean?

Tags:

batch-file

setlocal enabledelayedexpansion
for /f "delims=" %%a in (%TempFile2%) do (
    set a=%%a
    set a=!a: =!   
    echo !a! >>%DataFile%
)

I understand that code looks for every "empty space" in tempfile2 data and sets it. What does this line mean ?

set a=!a: =!

Thanks

like image 984
Zeus Avatar asked Dec 28 '22 03:12

Zeus


1 Answers

It's a basic pattern match and replace.

Here's the help from set /?:

Environment variable substitution has been enhanced as follows:

    %PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2".  "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output.  "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
occurrence of the remaining portion of str1.

The additional twist in your example is that the 'delayed expansion' syntax is being used, which uses the ! character as the environment variable expansion character instead of %.

So the command set a=!a: =! is removing all space characters from the contents of the variable a.

Delayed expansion is needed (or at least makes something like this a bit easier) because of the way cmd.exe normally expands (then using the % delimiter) the entire set of commands in the block enclosed by parens before executing any part of it.

like image 114
Michael Burr Avatar answered Jan 04 '23 22:01

Michael Burr