|
Answer» 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 mikeset SUBDIR=.%%~pp.Hi
When i add your code, Im getting:
The syntax of the COMMAND is incorrect.
Thanksoops, sorry, miss out this line: for /f "tokens=* delims=" %%p in ('dir/b/s/ad') do ( -- list directory
set subdir=.%%~pnxp
dont forget to put quotes around pathname: if exist "!dest!\!SUBDIR!\%%a" ren "!dest!\!SUBDIR!\%%a" "%%a.bak" copy "%%a" "!dest!\!SUBDIR!\" Hi,
see the bold below for the output.. i actually want SUBDIR to be folder "4", so i can attach folder "4" to DEST, so that when this line comes: !dest!\!SUBDIR!\ it will be something like: C:\Documents and Settings\lum\Desktop\bat dest\c8\4\
@echo off & setLocal EnableDelayedExpansion
set SRC="C:\Documents and Settings\lum\Desktop\bat source\c8" set DEST="C:\Documents and Settings\lum\Desktop\bat dest\c8"
pushd !SRC!
:: loop all directories for /f "tokens=* delims=" %%p in ('dir/b/s/ad') do (
pushd %%p
echo %%p <-----------C:\Documents and Settings\lum\Desktop\bat source\c8\4
set SUBDIR=.%%~pnxp
echo !SUBDIR! <------.\Documents and Settings\lum\Desktop\bat source\c8\4
:: 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 )Quote from: vip2cool on April 17, 2009, 08:41:04 AM 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\
this is your original question
ANYWAY, this only works on single folder (not working on getting 2,3 or 4 level directories) set SUBDIR=%%~nxp -- x SWITCH in case you have extension on foldername
and type for/? and look at the bottom for more information on available switches.
untested, already late, i can't get my brain working right set subdir=%%p set subdir=!subdir:%src%\=! echo !subdir! Yea, i'm actually need multiple levels of folder support.. something like below:
SRC\subfolder1\...\subfolderN ... SRC\subfolderN\...\subfolderN
DEST\subfolder1\...\subfolderN ... DEST\subfolderN\...\subfolderN
Thanksok, battery in full charge, brain in full working condition
Code: [Select]@echo off & setlocal enabledelayedexpansion
set SRC=C:\main folder\ set DEST=E:\main folder\ if not exist %dest% md %dest%
for /r "%src%" %%a in (*) do ( set d=%%~dpa set d=%dest%!d:%src%=!
echo %%a --^> !d! if exist "!d!%%~nxa" move "!d!%%~nxa" "%%~nxa.bak" && echo ren - %%~nxa.bak
if not exist "!d!" md "!d!" && echo md - !d! copy "%%a" "!d!" >nul && echo copy - %%~nxa
REM xcopy can auto create subdir, replacing the above 2 lines, but slower in execution ?? REM xcopy/i "%%a" "!d!" >nul && echo xcopy- %%~nxa ) Many thanks!
the code was almost working!
currently, all .bak goes to where the batch file is located, is it possible to leave the .bak files in the same directory it was from?
e.g. if textfile.txt already exisit in DEST, after running the batch, DEST will be: DEST=E:\main folder\folder1\folder2\textfile.txt <---new file copied from source DEST=E:\main folder\folder1\folder2\textfile.txt.bak <---old file originally from dest
Thanks again!!bug fix: replace this line: if exist "!d!%%~nxa" move "!d!%%~nxa" "%%~nxa.bak" && echo ren - %%~nxa.bak to if exist "!d!%%~nxa" move "!d!%%~nxa" "!d!%%~nxa.bak" && echo ren - %%~nxa.bak it's perfect! thanks for all the help!
|