Batch: Copy+Save Files w Duplicate File Names

May 28, 2009 at 11:45:08
Specs: Windows XP
I am trying to copy files listed in a text list (full paths) to a new directory. I would like to copy all of the listed files, storing any files with duplicate file names in a second directory. Here is the code I am attempting to use. Right now, when it runs it creates the two folders /newfolder/seconds and then terminates. Can anyone help? Thanks,
Paul

if not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\seconds
for /f "tokens=*" %%a in (files2move.txt) do (
if exist newfolder\%%a goto :else
copy "%%a" newfolder\
goto :endif
:else
copy "%%a" newfolder\seconds
:endif
)


See More: Batch: Copy+Save Files w Duplicate File Names

Report •


#1
May 28, 2009 at 11:54:03
Problem is the goto busts the for loop. Try this:

=============================
for /f "tokens=*" %%a in (files2move.txt) do (
if exist newfolder\%%a (
copy "%%a" newfolder\seconds\
) else (
copy "%%a" newfolder\
)
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
May 28, 2009 at 12:32:40
Thanks for your help. This new code still doesn't seem to work either. It is still overwriting the files in newfolder\ & nothing is copied to newfolder\seconds. In a test run I found the duplicate (2nd file) in newfolder\ (the original was overwritten).

It seems that the code doesn't see %%a as existing in newfolder\.

Does it matter that %%a contains the full path? In the example below, does it check if file1.txt is in newfolder\ or is it looking for g:\folder1...\file1.txt?

ex: If i want these two files to be moved as
g:\folder1\folder2\file1.txt --> newfolder\
g:\folder1\folder2\folder3\file1.txt --> newfolder\seconds\

Any ideas?


Report •

#3
May 28, 2009 at 12:49:02
Nothing obvious. Better post your code.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
May 28, 2009 at 12:51:01
Sorry, here you go:

if not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\seconds
for /f "tokens=*" %%a in (files2move.txt) do (
if exist newfolder\%%a (
copy "%%a" newfolder\seconds\
) else (
copy "%%a" newfolder\
)
)


Report •

#5
May 28, 2009 at 12:56:23
Still dunno. Even if your list has spaces, the " should handle it. How about a few lines from files2move.txt ?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
May 28, 2009 at 13:02:01
Thanks for your continued quick responses!

Here is the current contents of files2move.txt:
C:\Documents and Settings\paul.phelps\test\file1.txt
C:\Documents and Settings\paul.phelps\test\test2\file1.txt

I can tell that only the 2nd file has overwritten the first in newfolder\ by the contents of the file (I put different text inside the two files).

Is there any way to test/troubleshoot the

if exist newfolder\%%a

portion of the code?


Report •

#7
May 28, 2009 at 13:15:38
Try this:

===========================
@echo off & setLocal EnableDelayedExpansion

if not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\seconds

for /f "tokens=*" %%a in (files2move.txt) do (
if exist newfolder\%%~Na%%~Xa (
echo copy "%%a" newfolder\seconds\
) else (
echo copy "%%a" newfolder\
)
)
==================
I think you'll see where I went wrong.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#8
May 28, 2009 at 13:31:31
Still not working.

Code:

@echo off & setLocal EnableDelayedExpansion

if not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\seconds

for /f "tokens=*" %%a in (files2move.txt) do (
if exist newfolder\%%~Na%%~Xa (
echo copy "%%a" newfolder\seconds\
) else (
echo copy "%%a" newfolder\
)
)

Result:

copy "C:\Documents and Settings\paul.phelps\test\file1.txt" newfolder\
copy "C:\Documents and Settings\paul.phelps\test\test2\file1.txt" newfolder\

What exactly does %%~Na%%~Xa do?


Report •

#9
May 28, 2009 at 13:43:01
%%~Na%%~Xa puts out the filename.ext with no path, which is what we need.

=====================
@echo off & setLocal EnableDelayedExpansion

if not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\seconds

for /f "tokens=*" %%a in (files2move.txt) do (
if exist newfolder\%%~Na%%~Xa (
copy "%%a" newfolder\seconds\
) else (
copy "%%a" newfolder\
)
)
====================
I goota get some sleep. If this doesn't get it, tomorrow I'll create the source files.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#10
May 28, 2009 at 13:47:40
Well, I am not sure why the echo version didn't work, but your new code does work! Thanks for all your help.

Report •


Ask Question