Need BAT Help. Copy from source to dest, rename if exist.
Posted: 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
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