Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From a source directory structure (multiple folders), I need to copy all csv files to a single target directory. I found a command to do this, though files of the same name exist within different folders of the source structure, causing obvious issues when copied to one folder.

How can the files with duplicate names be renamed during the copy please (ideally: report.csv, reportcopy2.csv etc)? The job currently only copies a single instance of each file. Thanks for help.

share|improve this question

This is a slight work around - I know you wanted to name differently but I wondered if this will work for you.

I would suggest you rename on copy to something like Foldername.Filename.csv.

Use something like

echo f | xcopy /f /y srcfile destfile


I have tried for a few hours, I don't think what you want is possible with the CMD prompt or bat files.

This is what I have

set sDir=C:\Documents and Settings\drook\Desktop
set dDir="C:\Documents and Settings\drook\Desktop\Folder\"

cd C:\Documents and Settings\drook\Desktop\
FOR /F %%a in ("*.txt") DO (     
    xcopy "%%a" "C:\Documents and Settings\drook\Desktop\Folder\"
    cd C:\Documents and Settings\drook\Desktop\Folder\
    ren "%%a" "newName-%dir%.txt"
    cd C:\Documents and Settings\drook\Desktop\


It fails on the rename because it ignores the variable. So where I'm showing newName-%dir% (where dir is the variable) it would fail on newName-%%a as well...

Sorry, I don't think it is possible.

Having said that, this makes it look like it is possible: windows-batch-file-to-copy-and-keep-duplicates

share|improve this answer
Thanks Dave. At the moment I am using - – Nick Aug 29 '12 at 10:43
Thanks Dave. At the moment I am using FOR /R "src" %%a in ("*.csv") DO xcopy /d /y "%%a" "dest". Unsure how to integrate a name-change. Thanks – Nick Aug 29 '12 at 10:50
A bit more info - around 500 files from 3000 will be copied initially with perhaps groups of four or five with the same. – Nick Aug 29 '12 at 10:57

This is similar to aflat's answer:

setlocal enableDelayedExpansion
set counter=1
for /r src %%F in (*.csv) do (
    copy %%F dest\%%~nF-!counter!%%~xF
    set /a counter=counter+1
share|improve this answer

This should do it:

@echo off
set "sourcePath=c:\source"
set "targetPath=c:\temp\target"
set "pattern=*.csv"
set prev=""
set count=0

for /f  "tokens=1,2 delims=?" %%F in ('^(for /r "%sourcePath%" %%N in ^("%pattern%"^) do ^@echo %%~nxN?%%N?^)^|sort') do (
  call :copyFile "%%F" "%%G"
  set prev="%%F"

goto :eof

  if /I %prev%==%1 (set /a count+=1) else (set count=0)
  if %count%==0 (set sufix=) else (set sufix=Copy%count%)
  echo copy "%~2" "%targetPath%\%~n1%sufix%%~x1%" 
goto :eof

What it does:

  • Lists all files matching pattern (most inner for) formatted as name?path
  • Sorts the list (by file name)
  • Parses sorted list line by line (outer for) and for each line (file)
    • if file name appears first time, copies it unmodified
    • if file name repeats, adds suffix and count_nr to a copy

As it is now it will just echo copy command instead of executing it, so you can safely run it and check it's output. When you're ready, just remove echo from
echo copy "%~2" "%targetPath%\%~n1%sufix%%~x1%"

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .