Batch Move Files From [list1] to [list2]

May 21, 2009 at 11:08:27
Specs: Windows XP
Here's a little batch I've been using to move files with full paths specified in a txt list (filelist.txt) to a folder called "files" on the C drive:

@echo off
for /F "delims=" %%i in (filelist.txt) do (
echo %%i
move "%%i" c:\files
)

The source files listed in filelist.txt are scattered throughout a dir on an external drive that has a large and complex subdir tree, but when this batch is run, everything is moved to a single, flat c:\files dir. It's great for what it does, but one problem is that if there are any duplicate file names in filelist.txt, the batch will overwrite any previously moved 1.doc (for example) with a 1.doc further down the list.

Anyone know how to tweak this bat so that instead of moving everything to the single c:\files dir, it will--as needed--replicate the dir structure on the destination drive as it moves the files? This will prevent the overwrites, of course, but it will also be useful to have the "cloned" dir structure from the external drive under the destination dir (not the entire tree, but just the dirs/subdirs that actually contain moved files specified in filelist.txt!).


See More: Batch Move Files From [list1] to [list2]

Report •


#1
May 24, 2009 at 09:13:06
Figured it out. In case anyone else is having a similar problem, I added a "destination path" column (tab delimited) to filelist.txt to the right of the "source files" column and populated it with the source file path with just the drive letter replaced. Token 1 (variable %%i) = the file (with path) to move, and token 2 (variable %%j) = the destination directory.

I could've added an ifexists to the md command so that it doesn't try to create the same dir more than once, but the batch continues to run on an "already exists" error, so that's not necessary. Also important: replace the "<tab>" in the code below with an actual tab in the text .bat file.

@echo off
for /F "tokens=1,2 delims=<tab>" %%i in (filelist.txt) do (
md "%%j"
echo %%i
move "%%i" "%%j"
)


Report •
Related Solutions


Ask Question