Need BAT Help. Copy from source to dest, rename if exist.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vip2cool
Posts: 2
Joined: 17 Apr 2009 08:45

Need BAT Help. Copy from source to dest, rename if exist.

#1 Post by vip2cool » 17 Apr 2009 08:47

Hi,

I'm trying to do recursive copy of a SRC folder to a DEST folder.
SRC and DEST both have the SAME folder structures (that is they have the same subdirectories, no matter how deep)

if file exist in DEST, rename it, then copy the SRC file to DEST
if file does not exist in DEST, then simply copy the SRC file to DEST

eg. SRC\subfolder1\subfolder2\file1.txt to DEST\subfolder1\subfolder2\file1.txt

the file1.txt (from DEST\subfolder1\subfolder2\) will be rename to file1.txt.bak, THEN file1.txt (from SRC\subfolder1\subfolder2\) will be copy to location DEST\subfolder1\subfolder2\

eg. SRC\subfolder3\subfolder4\file2.txt to DEST\subfolder3\subfolder4\

the file2.txt (from SRC\subfolder3\subfolder4\) will be copy to location DEST\subfolder3\subfolder4\


currently i have the this code, but it has problem, as that i can't get the name of the current folder, see below to better understand:

- - - - - - -

@echo off & setLocal EnableDelayedExpansion

set SRC="C:\main folder\"
set DEST="E:\main folder\"

pushd !SRC!

:: loop all directories
for /f "tokens=* delims=" %%p in ('dir/b/s/ad') do (

pushd %%p

set SUBDIR=%%p <--------PROBLEM (don't need the full path, only need folder name?)

:: a list of files
for /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
if exist !dest!\!SUBDIR!\%%a ren !dest!\!SUBDIR!\%%a %%a.bak
copy %%a !dest!\!SUBDIR!\

echo File name: %%a

)
popd
)

- - - - - - -

can someone help me fix my code, or if you can create some code from scratch that can do what i need, that'll be great too!

Thanks
mike

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 17 Apr 2009 13:29

I hope someone comes up with something better, because what I've got gets a little "messy".

I'm basically going to exploit the command:

dir /a-d /b /s

to go through each file one by one, and then mangle the variables (that's the messy part) for the backup and copy.

SRC and DEST cannot have quotes. SRCNUM is the number of characters in the SRC variable. (There are ways to automate this -- search around here for count variable length or something like that.)

Code: Select all

@echo off
setLocal EnableDelayedExpansion

set SRC=C:\main folder
set SRCNUM=14
set DEST=E:\main folder


for /f "usebackq" %%a in (`dir /a-d /b /s "%SRC%"`) do (
   set file=%%a
   if exist "%DEST%!file:~%SRCNUM%!" call :destrename "%DEST%!file:~%SRCNUM%!"
   copy "%%a" "%DEST%!file:~%SRCNUM%!"
)
goto :eof

:destrename
if exist "%~1.bak%" (
   echo Backup file already exists.  Add your code here
   echo to overwrite or delete or error out or whatever
   echo you want to do in this event.
   pause
)
ren %1 "%~nx1.bak"
goto :eof
[/code]

vip2cool
Posts: 2
Joined: 17 Apr 2009 08:45

#3 Post by vip2cool » 20 Apr 2009 07:19

appreciate your help, thank you!

Post Reply