1

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

Danny Gloudemans
  • 2,597
  • 7
  • 39
  • 57
  • Are you sure you want to remove trailing spaces, as with your `delims=` you do – jeb Mar 23 '12 at 10:34
  • jeb's answer addresses the main problem. But be careful of extra spaces when you assign variables. You probably want `set pp=%%p`. Your code `set pp = %%p` has extra spaces, so the variable name includes a trailing space and the value has a leading space. – dbenham Mar 23 '12 at 11:44
  • @dbenham: Hmmm, is this not the same I said? :-) – jeb Mar 23 '12 at 12:09
  • @jeb: oops, indeed you did. I somehow missed that sentence the 1st time I read your answer. – dbenham Mar 23 '12 at 12:20

2 Answers2

3

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!
)
Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Aah ok, I understand, but how can I do now something with the !var! ? This is what I want to do, but doesn't work.. set !myVar!=!myVar:http://subversion/svn/dotCMSPlugins/=! – Danny Gloudemans Mar 23 '12 at 10:42
  • I added it in my sample. Use only `myVar` as variable name in the set statement, not `!myVar!`. The syntax of set is `SET =` or `SET "="`, the second form ensures not to append unwanted spaces at the end. – jeb Mar 23 '12 at 10:47
  • Still not works 100%, but we are getting close! :) What I want is that this following sentences split the String, So that I get as final answer: CustomLogin subversion/svn/dotCMSPlugins/CustomLogin/trunk/ subversion/svn/dotCMSPlugins/= /trunk/= – Danny Gloudemans Mar 23 '12 at 11:05
  • You should edit your question: Show us what line(s) do you get from plugins.txt, and what do you expect the output should be – jeb Mar 23 '12 at 11:09
  • Sorry, I thought when I had the variable I can do it by myself, but it isn't working for me at the moment! This is the string I put in the String: http://subversion/svn/Plugins/CustomLogin/trunk/ and this is what I want from the output: CustomLogin – Danny Gloudemans Mar 23 '12 at 11:58
  • Then you should try to remove the unwanted text by replacing _exactly_ the right strings. `subversion/svn/Pulgins/` and not `.../dotCMSPlugins/` – jeb Mar 23 '12 at 12:11
  • Hmm that is not the problem, I copied from the wrong file ;-) This is the link: http://subversion/svn/dotCMSPlugins/CustomLogin/trunk/ So it is the same! – Danny Gloudemans Mar 23 '12 at 12:21
  • I see the fault! First you had "myVar=!myVarmyVar:, just one myVar to much.. I copied it so there was the mistake! Thanks alot! :) Danke! – Danny Gloudemans Mar 23 '12 at 12:29
2

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%
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • This is a very nice way that I want to use. Only one problem. I need both, the full URL and the 'fifth token'. So I thought this is the best way: for /f "tokens=* delims= " %%p in (plugins.txt) do (set "Var=%%p" for /F "tokens=5 delims=/" %%c in (%%p) do (set "myVar=%%c") DO SOME ACTIONS ) But he can't find the fifth token in the second for. – Danny Gloudemans Mar 26 '12 at 07:08
  • @Gynnad: The %%p must be enclosed in quotes. Also, I suggest you to use "delims=" instead of "tokens=* delims= " to get the entire line: `for /F "delims=" %%p in (plugins.txt) do (set "Var=%%p" for /F "tokens=5 delims=/" %%c in ("%%p") do (set "myVar=%%c") DO SOME ACTIONS)` – Aacini Mar 26 '12 at 18:36