3

I'd like to copy one filetype from sub directories, placing the copies into one folder, renaming them so they don't overwrite. Rename it but keep the file extension. I don't care what it is renamed to, as long as each copy has a unique name so they don't overwrite.

Here is my code now. It does everything I'd like except it overwrites files with same name:

Code:

set dSource=C:\Users\P Ditty\Documents\SH3\data\cfg\Careers
set dTarget=C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR
set fType=*.clg
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
copy  /v "%%f" "%dTarget%\" 2>nul
)

Thank you.

machiavelli
  • 92
  • 1
  • 1
  • 9
  • rename to what exactly? If you had a file C:\Users\P Ditty\Documents\SH3\data\cfg\Careers\myfile.clg, what would you want it to look like in your destination? – ty733420 Feb 22 '13 at 21:44
  • Anything. As long as it keeps the file extension. So numerical or even by date. As long as they don't overwrite either. So each file is unique. – machiavelli Feb 22 '13 at 21:45

1 Answers1

1

To avoid the duplicate problem by retaining original directory structure:

set dSource=C:\Users\P Ditty\Documents\SH3\data\cfg\Careers
set dTarget=C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR
set fType=*.clg

xcopy/s/i "%dSource%\%fType%" "%dTarget%"
ty733420
  • 894
  • 7
  • 13
  • So if the creation dates are the same, then they will overwrite? Is it possible to make it numerical then? – machiavelli Feb 22 '13 at 22:13
  • You could pick a random number and pray it isn't already there, but I would suggest sticking with dates and just using more precision. Makes it easier in the long run. I've updated my answer to include precision down to the minute...as long as you don't run it more than once per minute, you'll be okay :) – ty733420 Feb 22 '13 at 22:18
  • The files that they are copying are not the same file in the same sub directory being changed over time. They are different files in different sub directories with the same filename. So making it by the minute still overwrites. – machiavelli Feb 22 '13 at 22:23
  • I see, so there are dupes in your original subdirectories. Any reason you don't want the backup folder to contain the same directory structure as the original? – ty733420 Feb 22 '13 at 22:24
  • random number may be best for this case, because the files with the same filename that are being copied wont number more than 10 files. – machiavelli Feb 22 '13 at 22:25
  • Could make the same structure, that would work too. Might be easier. I think I can do that on my own. 1 sec. – machiavelli Feb 22 '13 at 22:26
  • Now I just need to figure this out and I'm good to go!: http://stackoverflow.com/questions/15033351/search-for-a-list-of-words-in-a-file-then-find-those-words-on-another-replacing – machiavelli Feb 22 '13 at 22:34
  • could you do the random number? – machiavelli Feb 22 '13 at 22:59
  • I can't do the subdirectories because I can't get this code to search subdirectories: findstr /s /c:"ClassName=!ln:~6!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\*.clg" >"C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\null" && – machiavelli Feb 22 '13 at 23:00
  • The *.clg only searches in that folder, not the subdirectories of that folder. So the random number method would be best. – machiavelli Feb 22 '13 at 23:02