Copy data from paths in a txt file

Microsoft Windows xp professional w/serv...
May 24, 2010 at 23:12:39
Specs: Windows XP
I have created a batch file to copy data from folders listed in a text file, but it does not work. Please help.

The text file (List.txt) has the following information:
“D:\Documents and Settings\All Users\My Documents\Invoice”
“C:\Documents and Settings\All Users\Desktop\AntiVirus Software”

The batch file is:
@echo off
set NFolder=BU%date:~-10,4%%date:~-5,2%%date:~-2,2%
set drive=E:

for /f "tokens=1,2 delims=:" %%A in (c:\ List.txt) do (
set OldPath=%%A:~-2,1:%%B
set NewPath=%drive%:\%NFolder%%%B
copy %OldPath% %NewPath%
)

The expected result should be

Copy “D:\Documents and Settings\All Users\My Documents\Invoice” “E:\BU20100525\Documents and Settings\All Users\My Documents\Invoice”

Copy “C:\Documents and Settings\All Users\Desktop\AntiVirus Software” “E:\BU20100525\Documents and Settings\All Users\Desktop\AntiVirus Software”

But, I got…

Copy “C:\Documents and Settings\All Users\Desktop\AntiVirus Software” “E:\BU20100525\Documents and Settings\All Users\Desktop\AntiVirus Software”

Copy “C:\Documents and Settings\All Users\Desktop\AntiVirus Software” “E:\BU20100525\Documents and Settings\All Users\Desktop\AntiVirus Software”

The first %%B is dropped by the loop.

Please help.

Thanks.


See More: Copy data from paths in a txt file

Report •


#1
May 25, 2010 at 12:18:46
you need delayed expansion inside for-loops (see: set /?).
Also don't know how this works: %%a:~-2,1:%%b. On my system it gives:
c-2,1:\xxxxx
but if it's working on yours, well, let me know, i'd like to see how it works.
this might work for your objective:
@echo off &setlocal enabledelayedexpansion
set drive=
set Nfolder=
:etc etc
:: foll is all on the same line
for /f "tokens=*" %%a in (list.txt) do echo copy %%a "%drive%:\%Nfolder%%%~pnxa"
::------ end
(technically, this version does NOT need enabledelayed)

Report •

#2
May 26, 2010 at 00:43:37
Added &setlocal enabledelayedexpansion as suggested works. Thanks.

Report •
Related Solutions


Ask Question