Computing.Net > Forums > Disk Operating System > BATCH HELP: Copy files with rename!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

BATCH HELP: Copy files with rename!

Reply to Message Icon

Name: vip2cool
Date: April 9, 2009 at 14:13:57 Pacific
OS: Windows XP
Subcategory: General
Comment:

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




Response Number 1
Name: Mechanix2Go
Date: April 10, 2009 at 03:57:58 Pacific
+1
Reply:

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



Response Number 2
Name: vip2cool
Date: April 13, 2009 at 06:32:09 Pacific
+1
Reply:

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



Response Number 3
Name: Mechanix2Go
Date: April 13, 2009 at 08:30:25 Pacific
+1
Reply:

@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 %%d

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



Response Number 4
Name: vip2cool
Date: April 13, 2009 at 09:43:34 Pacific
+1
Reply:

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.txt

the result would be:
E://abc/1.txt.bak, abc/a/2.txt.bak, abc/a/3.txt

can you make sure the code can give that result?

Thanks again
mike



Response Number 5
Name: Mechanix2Go
Date: April 13, 2009 at 09:51:48 Pacific
+1
Reply:

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



Related Posts

See More



Response Number 6
Name: vip2cool
Date: April 13, 2009 at 10:20:50 Pacific
+1
Reply:

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



Response Number 7
Name: Mechanix2Go
Date: April 13, 2009 at 13:07:21 Pacific
+1
Reply:


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 %%d

for /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



Response Number 8
Name: vip2cool
Date: April 13, 2009 at 13:18:45 Pacific
+1
Reply:

Hi M2

still did not work.. can you test it on your side?

Thanks again
mike



Response Number 9
Name: vip2cool
Date: April 13, 2009 at 13:32:19 Pacific
+1
Reply:

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



Response Number 10
Name: Mechanix2Go
Date: April 13, 2009 at 15:13:30 Pacific
+1
Reply:

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



Response Number 11
Name: vip2cool
Date: April 14, 2009 at 06:56:21 Pacific
+1
Reply:

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



Response Number 12
Name: Mechanix2Go
Date: April 14, 2009 at 22:55:10 Pacific
+1
Reply:

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



Response Number 13
Name: vip2cool
Date: April 15, 2009 at 09:20:36 Pacific
+1
Reply:

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
└───5

Destination:

Folder PATH listing
Volume serial number is 6806-ABBD
C:\BAT DEST\C8
├───4
│ └───4a
└───5


Thanks
mike



Response Number 14
Name: vip2cool
Date: April 17, 2009 at 06:28:29 Pacific
+1
Reply:

Hi anyone else know how to do this?

Thanks



Response Number 15
Name: vip2cool
Date: April 21, 2009 at 11:45:20 Pacific
+1
Reply:

I think i'm good for now, thanks for all the help!



Reply to Message Icon

DOS player is MPXPlay picture on CD-R wont play...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Google Ads



Results for: BATCH HELP: Copy files with rename!

Copying files with a space www.computing.net/answers/dos/copying-files-with-a-space/16475.html

More batch help www.computing.net/answers/dos/more-batch-help/11371.html

copying files with long filenames www.computing.net/answers/dos/copying-files-with-long-filenames/4586.html