Page 1 of 1

FOR command - help please

Posted: 04 May 2010 10:48
by kabalman
Hi

I have a file temp.txt with the following string content
C:\folder\file1.txt

I need to extract the string file1.txt from temp.txt and store the value in a variable. I did this using a batch script with the following commands

set "DirPath=C:\folder\"
for /f "tokens=* delims=%DirPath%" %%a in (temp.txt) do echo %%a

I just get the string "xt" output and not file1.txt. What am I doing wrong?

I am assuming that the delimiter is C:\folder\ and that should give %%a as file1.txt

Any help will be greatly appreciated.

Re: FOR command - help please

Posted: 04 May 2010 10:57
by jeb
Hi kabalman,

the delims is a list of single characters, so you get "C" ":" "\" "f" "o" ...

So nearly all of your string seems to be a delimiter.

try to use

Code: Select all

for /f "tokens=*" %%a in (temp.txt) do echo %%~nxa


Then you use the standard delims (space and tab).
In %%a is "C:\folder\file1.txt
and %%~nx stands for
n - name of he file
x - extension of the file

jeb

Re: FOR command - help please

Posted: 04 May 2010 11:02
by aGerman
Why so complicated?

Code: Select all

for /f "delims=" %%a in (temp.txt) do echo %%~nxa

n is for name and x is for extension
Have a look to the help (for /?) and you will find more options.

Regards
aGerman

[edit]@jeb - da hat wohl einer rechts überholt :wink: [/edit]

Re: FOR command - help please

Posted: 05 May 2010 10:11
by kabalman
Thanks a lot. That worked absolutely fine.