0

I'm writing batch script which I'll use to copy files from location A to location B with rename of a source file from location A if the same file exists already in location B.

Currently Im using snippet from another topic here on stack but it doesnt work on files from subfolders, could anyone help me with code below so it work on all files and subdirectiories from both locations? Many thanks!

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET sourcedir="D:\TEST\FROM"
SET destdir="D:\TEST\TO"
SET /a count=0
for %%c in (%sourcedir%\*.*) do (
 CALL :select
 ECHO copy "%%c" "%destdir%\%%~nc_!count!%%~xc" /s
)

GOTO :EOF

:select
SET /a count+=1
IF EXIST "%destdir%\%%c" GOTO select
GOTO :eof
mhutnik
  • 3
  • 1
  • 3
  • What if... The source directory contains file.txt. The destination directory contains file.txt and file1.txt. Is this code supposed to rename it as file2.txt? – lit Nov 07 '18 at 20:00
  • Files in both location are name like this: date_text.txt and text stays the same, so the code supposed to rename only if filenames (dates) are matching exactly. Source is filled with files generated with another script and this code was meant to help in cases when copying is being done more than once a day (that's how it's possible that file with that day date may already exist in destination) – mhutnik Nov 08 '18 at 09:39

2 Answers2

1

Replace your for loop with the following for loop:

for /R "%sourcedir%" %%c in (*.*) do (what you like)

Also, why do you want the following piece of code?

copy "%%c" "%destdir%\%%~nc_!count!%%~xc" /s

Just copy "%%c" %destdir%

More generally you can write:

@ECHO OFF
SET sourcedir="D:\TEST\FROM"
SET destdir="D:\TEST\TO"
:: SET /a count=0
for /R "%sourcedir%" %%c in (*.*) do (
 :: SET /a count+=1
 IF NOT EXIST "%destdir%\%%c" (
    echo copy "%%c" %destdir%
 )
)

Hope you are fine with this, possible dublicate of Windows batch file with loop through subfolders

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • It's seems like it's above my coding skills, tried to put the extra loop in couple of ways but with no luck... Maybe I've started it wrong and it would be better to first compare source and destination and rename duplicates in source and use the use robocopy to move the files? – mhutnik Nov 08 '18 at 11:18
  • Followed yr advise but still no luck, now script iterates through source subdirectories but renames all files, even when destination folder is empty. Is the other IF ok then? – mhutnik Nov 09 '18 at 17:01
  • Basically I want content of source (with files and subfolders) copied over to destination but without overwriting in case of filename conflict - when same filename exist already in destination I want source filename changed with some prefix which can be static text (like *_duplicate.*) and don't need to be as %count% variable. Maybe I've based it on not quite right code to start with? When I think about it now it makes more sense to do renaming for conflicts first and then just simply copy all content from source? – mhutnik Nov 11 '18 at 16:26
  • It processes subfolders ok but all files are copied straight into %destdir% without their folder structure which I wanted to be same as in %sourcedir% – mhutnik Nov 17 '18 at 09:53
0

Sharing with what I managed to get so far, works for what I needed, however still not doing well for subfolders:

@ECHO OFF
SET "sourcedir= "
SET "destdir= "
SET "HH=%TIME:~0,2%"
SET "MM=%TIME:~3,2%"
SET "SS=%TIME:~6,2%"
SET "_Time=%HH%%MM%%SS%"

FOR /R "%sourcedir%" %%G IN (*.*) DO (
 IF EXIST "%destdir%\%%~nG%%~xG" (
    COPY /V /Z "%%G" "%destdir%\%%~nG_duplicate_%_Time%%%~xG"
 ) ELSE (
    COPY /V /Z "%%G"  "%destdir%\%%~nG%%~xG")
)
mhutnik
  • 3
  • 1
  • 3