Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
I need help of a script that can take a source directory (containing subdirs and files) and copy to the destination directory of the same name (subdirs are the same name too)!
For each file:
If it is an existing file, rename the existing file by adding a .bak extension, and copy the file to the destination directory/subdir.
If it is not an existing file, just simply copy the file to the destination directory/subdir.Thanks alot
mike

XP is not DOS. [Nor is DOS XP.]
If I follow your plan, something like this:
===============================
:: copy src dest; ren if exist@echo off & setLocal EnableDelayedExpansion
set /p src=src?
set /p dest=dest?
pushd !src!for /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
if exist !dest!\%%a ren !dest!\%%a %%~Na.bak
copy %%a !dest!\
)
=====================================
If at first you don't succeed, you're about average.M2

Hi M2
Thanks, I tried it out, but it did not recursively do the same to files within the subdirectories... can you help me fix it?
Thanks again,
mike

@echo off & setLocal EnableDelayedExpansion
set /p src=src?
set /p dest=dest?
pushd !src!for /f "tokens=* delims=" %%d in ('dir/b/s/ad') do (
pushd %%dfor /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
if exist !dest!\%%a ren !dest!\%%a %%~Na.bak
copy %%a !dest!\
))
=====================================
If at first you don't succeed, you're about average.M2

Hi M2
Thanks, but it still didn't work,
if you just create...
a source folder: C://abc/1.txt, abc/a/2.txt, abc/a/3.txt
a dest folder: E://abc/1.txt, abc/a/2.txtthe result would be:
E://abc/1.txt.bak, abc/a/2.txt.bak, abc/a/3.txtcan you make sure the code can give that result?
Thanks again
mike

Hi mike
Are you saying you want double extensions, like:
1.txt.bak
=====================================
If at first you don't succeed, you're about average.M2

Hi M2
Yes, that's right. If a file does not exist, just copy as-is (under whatever subdirectory it is in)
Thanks again
mike

BTW, the path separator in DOS and Winders is \ not /.=================
:: copy src dest; ren if exist@echo off & setLocal EnableDelayedExpansion
set /p src=src?
set /p dest=dest?
pushd !src!for /f "tokens=* delims=" %%d in ('dir/b/s/ad') do (
pushd %%dfor /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
if exist !dest!\%%a ren !dest!\%%a %%a.bak
copy %%a !dest!\
))
=====================================
If at first you don't succeed, you're about average.M2

Hi M2,
something is wrong with the first for loop.
subdirectories is not working at all.
if source is:
C://abc/0.txt
C://abc/a/1.txt
C://abc/b/2.txt
and dest is:
E://abc/0.txt
E://abc/a/1.txt
E://abc/b/i want the result to be:
E://abc/0.txt.bak + (new) 0.txt
E://abc/a/1.txt.bak + (new) 1.txt
E://abc/b/2.txt (new)Thanks again
mike

I say again for emphasis:
"the path separator in DOS and Winders is \ not /"
And \\ is no go.
Beyond that, src & dest are meant to be DIRECTORIES not files.
I just tried it on c:\a and c:\b
And it worked as advertised.
=====================================
If at first you don't succeed, you're about average.M2

Ok, that's not what i have problem with.
if you look at this line:
if exist !dest!\%%a ren !dest!\%%a %%a.bak
copy %%a !dest!\it always goes to the !dest! folder.. but i want to go under whatever SUBDIRECTORY the source folder contain. That is:
if source is c:\a\SUBDIR\1.txt, i want the destination to be c:\b\SUBDIR\1.txt.bat
Right now the code is moving all the files under c:\b, but not under its SUBDIRECTORIES
Thanks
mike

Hi mike,
Sorry, I'm lost. Do a TREE:
===============
C:\temp\-\aaa>tree c:\files
Folder PATH listing for volume 3G-80GSEAGA
Volume serial number is 0006FE80 49B0:39E9
C:\FILES
├───ID
├───DOXPIX
├───ebay
├───USPTO
├───linx
├───SIM
├───Cd-keys for all Software
├───X
├───add
│ └───23
├───logs
└───$
=====================================
If at first you don't succeed, you're about average.M2

Hi M2,
see below, all listed directories has files. I want files from folder4 of the source to go to folder4 of the destination, folder4a to folder4a, folder5 to folder5.
Source:
Folder PATH listing
Volume serial number is 6806-ABBD
C:\BAT SOURCE\C8
├───4
│ └───4a
└───5Destination:
Folder PATH listing
Volume serial number is 6806-ABBD
C:\BAT DEST\C8
├───4
│ └───4a
└───5
Thanks
mike

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |