Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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,
Paulif 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
)

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

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?

Nothing obvious. Better post your code.
=====================================
If at first you don't succeed, you're about average.M2

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

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

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.txtI 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?

Try this:
===========================
@echo off & setLocal EnableDelayedExpansionif not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\secondsfor /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

Still not working.
Code:
@echo off & setLocal EnableDelayedExpansion
if not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\secondsfor /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?

%%~Na%%~Xa puts out the filename.ext with no path, which is what we need.
=====================
@echo off & setLocal EnableDelayedExpansionif not exist newfolder\ md newfolder
if not exist newfolder\seconds md newfolder\secondsfor /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

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

![]() |
![]() |
![]() |

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