computing
  • 15

Solved Copy All Files, If Exists, Rename & Copy

  • 15

Hi, i was wondering if somebody can point me
in the right direction with a problem i have, my
problem is im trying to create a Batch Script
which first of all creates a new folder which will
be called ‘Finished’, it will then go into and
copy all files in sub folders into this new
‘Finished’, simple enough, but there will be a
likelyhood of files from different sub folders
which will have the exact same file name as
files which have already been copied to this
‘Finished’ folder, ive had a play around already
and ive noticed it just overwrites the file if they
have the same file name, which is what i dont
want, and thats where im stuck. i imagine id
want some kind of if statement where if a file
has the same file name as one that has
already been copied, add a (1), (2), (3) ect
ect.. to the end of that file name then copy it.

Ive created a test area with some test files and
this is the batch code ive come up with so far

md Finished
cd folder 1
copy * ..\Finished
cd ..\

cd folder 2
copy * ..\Finished
cd ..\

Im relatively new to creating Batch files but
have a better background on VBA, not sure if
theres a better way than batch script?

ANY help is much appreciated, Thanks.

Share

1 Answer

  1. Alright, try the following (hopefully nothing’s lost in
    translation):

    @echo off
    
    setlocal EnableDelayedExpansion
    
    set DestFolder1=Other
    set DestFolder2=Personal Details
    set DestFolder3=Emails
    set DestFolder4=CSV
    set max=4
    
    for /l %%i in (2, 1, %max%) do md "!DestFolder%%i!" 2>nul
    
    for /r %%f in (*) do (
      set CanCopy=true
      set folder=%%~dpf
      if /i "%%f" equ "%~dpnx0" (
        set CanCopy=false
      ) else (
        for /l %%i in (1, 1, %max%) do (
          if /i "%cd%\!DestFolder%%i!" equ "!folder!" (
            set CanCopy=false
          )
        )
      )
      if "!CanCopy!" equ "true" (
        set DestFolder=Other
        set folder=!folder:~0,-1!
        for /f "delims=" %%d in ("!folder!") do set folder=%%~nd
        for /l %%i in (2, 1, %max%) do (
          if /i "!folder!" equ "!DestFolder%%i!" (
            set DestFolder=!DestFolder%%i!
          )
        )
        if /i "!DestFolder!" equ "Other" md "Other" 2>nul
        if exist "!DestFolder!\%%~nxf" (
          call :GetNextFilename "!DestFolder!\%%~nxf" newfile
          copy "%%f" "!DestFolder!\!newfile!"
        ) else (
          copy "%%f" "!DestFolder!"
        )
      )
    )
    call :DeleteFolders
    goto :eof
    
    :DeleteFolders
    for /f "delims=" %%d in ('dir /b /a:d') do (
      set CanDeleteDir=true
      for /l %%i in (1, 1, %max%) do (
        if /i "!DestFolder%%i!" equ "%%d" (
          set CanDeleteDir=false
        )
      )
      if "!CanDeleteDir!" equ "true" rd /s /q "%%d"
    )
    goto :eof
    
    :GetNextFilename
    set n=0
    :loop
    set /a n+=1
    set %2=%~n1(%n%)%~x1
    if exist "%~p1!%2!" goto loop
    goto :eof

    • 0