Batch to rename and copy a new file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sL0w
Posts: 1
Joined: 24 Apr 2013 02:48

Batch to rename and copy a new file

#1 Post by sL0w » 24 Apr 2013 03:00

hello guys,

I have a problem and I hope you can solve it for me :)

I have I file that is located here:
C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames.ora

I want to rename it by batch and copy a new one in the same folder.

My batch ist looking like this

@echo on
rename "C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames.ora" "C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames_old.ora"
copy tnsnames.ora C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames.ora
pause

But the rename don't work... I always get a syntax error...

Need some help pls.

Greets sL0w

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch to rename and copy a new file

#2 Post by foxidrive » 24 Apr 2013 04:17

Try this:

EDITED: to use Move instead of rename


Code: Select all

@echo on
move /y "C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames.ora" "C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames_old.ora"
copy /b "tnsnames.ora" "C:\Oracle\product\11.2.0\client_1\network\admin"
pause

terus
Posts: 14
Joined: 26 Apr 2013 22:40

Re: Batch to rename and copy a new file

#3 Post by terus » 26 Apr 2013 23:00

I tend to use a backup folder personally. But the same method works for renaming the file. Here's what I personally use.

Edited to fix quotation misplacement, set better flag, & explain a little bit.

Code: Select all

@ECHO OFF
SET "backup=C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames_old.ora"
SET "original=C:\Oracle\product\11.2.0\client_1\network\admin\tnsnames.ora"
SET "new=C:\Downloads\tnsnames.ora" or where ever you may have it
REM setting flag /Y with XCOPY will overwrite existing file without prompt
@ECHO ON
XCOPY /Y %original% %backup%
XCOPY /Y %new% %original%
REM if you want the new file removed after copying use the following
DEL %new%
@PAUSE


Also, if you aren't using a backup folder "COPY" would work better than "XCOPY".
To use a folder instead just add that to the backup path in variable.

Code: Select all

SET "backup=C:\path~\Backup\tnsnames.ora"

Alternatively you can do without the variables & just use the paths directly. I tend to be a little variable crazy. Parameters have a tendency of being used often & are subject to change. I found a variable to be the easiest way to deal with that.

Post Reply