Reputation: 89
I have a batch file where I want to concatinate folders to the PATH
variable.
The folders should be given to the batch via parameter.
I would call the file like this:
set_path.bat "\Folder1;\Folder2;\Folder3"
Right now I have this:
set FOLDERS=%1
set FOLDERS=%FOLDERS:"=%
:NextItem
if [%FOLDERS%] == [] goto EOF
FOR /f "tokens=1* delims=;" %%a IN (%FOLDERS%) DO
(
echo %%a
set "FOLDERS=%%b"
)
goto NextItem
:EOF
For now it's just supposed to echo the folder names. What I expect as result is
\Folder1
\Folder2
\Folder3
However if I try this I can the error after the set FOLDERS=\Folder1,\Folder2
:
"\Folder2]" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
What am I doing wrong/missing?
Upvotes: 1
Views: 1356
Reputation: 49086
I suggest following batch file code:
@echo off
set "Folders=%~1"
:NextFolderPath
for /F "tokens=1* delims=;" %%I in ("%Folders%") do (
echo %%I
set "Folders=%%J"
if defined Folders goto NextFolderPath
)
The string assigned to environment variable Folders
is \Folder1;\Folder2;\Folder3
on running this batch file with "\Folder1;\Folder2;\Folder3"
as first and only argument string. The double quotes around argument string are removed by Windows command processor because of using %~1
instead of %1
. This behavior is explained by the help output on running in a command prompt window call /?
.
The command FOR is used to split up the string assigned currently to environment variable Folders
enclosed in double quotes.
The string with the list of semicolon separated folder paths is split up into two substrings (tokens) because of tokens=1* delims=;
. The first ;
separated string is always assigned to specified loop variable I
. As this string never begins with a semicolon because of the semicolon is the delimiter, the default eol=;
does not really matter here. The second string being everything after one or more semicolons after first folder path string is assigned to next loop variable J
according to ASCII table.
The folder path string assigned to loop variable I
is just output. The remaining folder paths are assigned again to environment variable Folders
which is now either with one folder path less as before or is not defined anymore at all because of Folders
contained on current iteration of the loop just one folder name.
The command FOR is executed once again on environment variable Folders
still defined because of one or more folder paths still to process. Otherwise the batch file processing ends with reaching end of batch file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
... explains %~1
echo /?
for /?
goto /?
if /?
set /?
See also Where does GOTO :EOF return to? I recommend to avoid the definition of label :EOF
in future. I use as label at end of a batch file :EndBatch
if I don't want to use goto :EOF
.
For string comparisons it is advisable to use if "%FOLDERS%" == ""
, i.e. enclose the two compared strings on both sides in double quotes, whereby the string value assigned to environment variable FOLDERS
should not contain one or more "
in which case this string comparison would fail. For details on string comparisons with command IF see my answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files.
if [%FOLDERS%] == []
is in general not good because of the square brackets have no special meaning in comparison to "
. [
and ]
are interpreted by Windows command processor on processing and executing the IF condition command line like the letters X
and Y
. But "
has a special meaning because all characters, except %
and with enabled delayed expansion also !
, are interpreted inside a double quoted argument string as literal characters including the command operators &
, &&
and ||
as well as the redirection operators <
, >
, >>
and |
and the round brackets (
and )
.
See also How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Upvotes: 1