I'm trying to set the variable of of the for statement to another variable. When I'm doing this:
for /f "tokens=* delims= " %%p in (plugins.txt) do set pp = %%p
It works correct. But I want to do more, So I do it after the ( and now it doesn't work. Why is that and how can I solve it?
for /f "tokens=* delims= " %%p in (plugins.txt) do (
set pp = %%p
echo %pp%
echo %pp%
echo %%p)
EDIT: Added input and desired output
content of plugins.txt
http://subversion/svn/dotCMSPlugins/CustomLogin/trunk
Desired output
CustomLogin
The setting works, but the expansion works in an other way as expected.
The percent expansion will be done in the moment of parsing the parenthesis block, not in the moment of execution of the single commands. How does cmd.exe parse scripts
Btw. Using spaces in the set statement isn't a good idea, as it creates a variable with a space, in your case set pp = %%p
creates a variable named pp<space>
with a value of <space><content of %%p
.
You can get the desired results by using delayed expansion.
setlocal EnableDelayedExpansion
for /f "tokens=* delims= " %%p in (plugins.txt) do (
set "myVar=%%p"
set "myVar=!myVar:subversion/svn/dotCMSPlugins/=!"
set "myVar=!myVar:/trunk=!"
echo !myVar!
)
If your input file contain this line:
subversion/svn/dotCMSPlugins/CustomLogin/trunk
and you want to get this result:
CustomLogin
then the easiest way is to separate the line in tokens by / character and take the 4th token this way:
setlocal EnableDelayedExpansion
for /F "tokens=4 delims=/" %%p in (plugins.txt) do (
set "myVar=%%p"
echo !myVar!
)
If the purpose of the FOR is just to get this result, then the delayed expansion is not even necessary:
for /F "tokens=4 delims=/" %%p in (plugins.txt) do (
set "myVar=%%p"
)
echo %myVar%