Computing.Net > Forums > Programming > Batch: Copy+Save Files w Duplicate File Names

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch: Copy+Save Files w Duplicate File Names

Reply to Message Icon

Name: ChampionSleeper
Date: May 28, 2009 at 11:45:08 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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
)



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 28, 2009 at 11:54:03 Pacific
Reply:

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


0

Response Number 2
Name: ChampionSleeper
Date: May 28, 2009 at 12:32:40 Pacific
Reply:

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?


0

Response Number 3
Name: Mechanix2Go
Date: May 28, 2009 at 12:49:02 Pacific
Reply:

Nothing obvious. Better post your code.


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

M2


0

Response Number 4
Name: ChampionSleeper
Date: May 28, 2009 at 12:51:01 Pacific
Reply:

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\
)
)


0

Response Number 5
Name: Mechanix2Go
Date: May 28, 2009 at 12:56:23 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: ChampionSleeper
Date: May 28, 2009 at 13:02:01 Pacific
Reply:

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?


0

Response Number 7
Name: Mechanix2Go
Date: May 28, 2009 at 13:15:38 Pacific
Reply:

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


0

Response Number 8
Name: ChampionSleeper
Date: May 28, 2009 at 13:31:31 Pacific
Reply:

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?


0

Response Number 9
Name: Mechanix2Go
Date: May 28, 2009 at 13:43:01 Pacific
Reply:

%%~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


0

Response Number 10
Name: ChampionSleeper
Date: May 28, 2009 at 13:47:40 Pacific
Reply:

Well, I am not sure why the echo version didn't work, but your new code does work! Thanks for all your help.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch: Copy+Save Files w Duplicate File Names

Batch copy unmatching files www.computing.net/answers/programming/batch-copy-unmatching-files/17123.html

Batch file name save www.computing.net/answers/programming/batch-file-name-save/16553.html

Batch file: Adding to file names www.computing.net/answers/programming/batch-file-adding-to-file-names/9098.html