FOR command - help please

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kabalman
Posts: 3
Joined: 04 May 2010 10:38

FOR command - help please

#1 Post by kabalman » 04 May 2010 10:48

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.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: FOR command - help please

#2 Post by jeb » 04 May 2010 10:57

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: FOR command - help please

#3 Post by aGerman » 04 May 2010 11:02

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]

kabalman
Posts: 3
Joined: 04 May 2010 10:38

Re: FOR command - help please

#4 Post by kabalman » 05 May 2010 10:11

Thanks a lot. That worked absolutely fine.

Post Reply