My "for" doesn't work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ITask
Posts: 2
Joined: 03 Feb 2013 08:25

My "for" doesn't work

#1 Post by ITask » 03 Feb 2013 08:41

Hallo, I have a problem:.
The file A:\text1.txt contains a strings like
? A:\abcd.abc
I have to copy a strings to another file A:\r\xcopy.txt
in the form like this:
XCOPY A:\abcd.abc A:\st\abcd.abc
My solution

FOR /F "tokens=2" %%i in (A:\text1.txt) do (
set oldstr=%%i
set newstr=%oldstr:A:=A:\st\%
echo XCOPY %%i %newstr% 1>> A:\r\xcopy.txt
)

doesn't work; A:\r\xcopy.txt contains now the strings like

XCOPY A:\abcd.abc

What is wrong??http://www.dostips.com/forum/posting.php?mode=post&f=3#

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: My "for" doesn't work

#2 Post by abc0502 » 03 Feb 2013 09:01

enable the delayed expansion and use ! ! instead of % % for variables

Code: Select all

Setlocal EnableDelayedExpansion
FOR /F "tokens=2" %%i in (A:\text1.txt) do (
set oldstr=%%i
set newstr=!oldstr:A:=A:\st!
echo XCOPY %%i !newstr!
)
pause

add "1>> A:\r\xcopy.txt" at the echo command to make redirect the result to your file.
Note, you will have to remove the last "\" here "%oldstr:A:=A:\st\%"

BTW, what this number "1>> A:\r\xcopy.txt" doing ? .. is it part of your code.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: My "for" doesn't work

#3 Post by foxidrive » 03 Feb 2013 09:15

Or use this:

Code: Select all

@echo off
FOR /F "tokens=1,*" %%a in (A:\text1.txt) do (
>> A:\r\xcopy.txt echo XCOPY %%b %%~db\st%%~pb
)



If you have long filename elements than try this:

Code: Select all

@echo off
FOR /F "tokens=1,*" %%a in (A:\text1.txt) do (
>> "A:\r\xcopy.txt" echo XCOPY "%%b" "%%~db\st%%~pb"
)

ITask
Posts: 2
Joined: 03 Feb 2013 08:25

Re: My "for" doesn't work

#4 Post by ITask » 04 Feb 2013 08:38

It's solved!
Thank you for help!

Post Reply