0

I have the following code to search for a specific file name (ratings.zil) across multiple folders and copy them to a new folder:

for /R %f in (ratings.zil) do @IF EXIST %f copy "%f" "C:\here"

But when the file copies to the new folder it overwrites instead of appending a number at the end of each ratings.zil – i.e. ratings(1).zil, ratings(2).zil. Is there a way to add a loop to the above code that will append a number after each file?

This question was originally marked as a duplicate, except the answer for the duplicate only works when you’re copying a file within the same folder.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Greeny
  • 3
  • 3
  • You could perhaps link the former question, so one could use the code from there... Anyway, what have you tried yourself to solve that issue? – aschipfl Nov 20 '18 at 09:12
  • @aschipfl OP delete the previous question. – Gerhard Nov 20 '18 at 09:13
  • @greeny, have a look here at [DBenham](https://stackoverflow.com/questions/5248393/windows-batch-file-to-copy-and-keep-duplicates)'s answer, you would just need to ammend the code to add your `ratings.zil` instead. – Gerhard Nov 20 '18 at 09:14
  • Possible duplicate of [Windows batch file to copy and keep duplicates](https://stackoverflow.com/questions/5248393/windows-batch-file-to-copy-and-keep-duplicates) –  Nov 20 '18 at 09:17
  • @GerhardBarnard, thanks - I'll try...I'm not a programmer so that looks entirely confusing to me. Do the double % signs signify something that needs to be filled in with whatever is between the %%'s? i.e. %source% means I put the source path there instead of %source%? – Greeny Nov 20 '18 at 09:22
  • I will try and help you quick.. – Gerhard Nov 20 '18 at 09:24
  • @GerhardBarnard, Thank you!!! That works perfectly, I appreciate you taking the time to help me out! – Greeny Nov 20 '18 at 09:43
  • anytime, glad I could help, but cannot take all the credit here as I took the answer from dbenham as I did not have enough time to write my own answer.. So as you do not have the relevant reputation yet, I will upvote his answer on your behalf.. – Gerhard Nov 20 '18 at 09:45
  • @GerhardBarnard, excellent - cheers! – Greeny Nov 20 '18 at 09:53

1 Answers1

0

Here is a slightly ammended version of DBenhams answer.

@echo off
setlocal disableDelayedExpansion

if "%~2" equ "" echo Error: Insufficient arguments>&2&exit /b 1
set "source=%~f1"
set "target=%~f2"

md "%target%"
set /a cnt=0
for /r "%source%" %%F in (ratings.zil) do if "%%~dpF" neq "%target%\" (
  if exist "%%F" (
  if exist "%target%\%%~nxF" (
    set /a cnt+=1
    set "full=%%F"
    set "name=%%~nF"
    set "ext=%%~xF"
    setlocal enableDelayedExpansion
    copy "!full!" "!target!\!name!(!cnt!)!ext!" >nul
    endlocal
  ) else copy "%%F" "%target%" >nul
 )
)

In order to run this, you need to save the file as something like myRename.cmd then simply open cmd.exe and run it as:

myRename.cmd "C:\Source of files" "D:\Destination"

If you perfer to place this in a set directory and have a static destination folder and be able to just double click it, then this will do:

@echo off
setlocal disableDelayedExpansion

set "target=C:\here"

md "%target%"
set /a cnt=0
for /r %%F in (ratings.zil) do if "%%~dpF" neq "%target%\" (
  if exist "%%F" (
  if exist "%target%\%%~nxF" (
    set /a cnt+=1
    set "full=%%F"
    set "name=%%~nF"
    set "ext=%%~xF"
    setlocal enableDelayedExpansion
    copy "!full!" "!target!\!name!(!cnt!)!ext!" >nul
    endlocal
  ) else copy "%%F" "%target%" >nul
 )
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43