Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file question

Status
Not open for further replies.

katbear

Programmer
Mar 14, 2007
270
US
Hi,

This isn't strictly a SQL Server question, but someone here probably knows the answer.

I need to use a batch file to move and rename the file.

For example, I would like to rename:

myBackup.BAK --> myBackup20070426.BAK

in one step.

Does anyone have the DOS commands to do this?

THANKS
 
use rename.

for example

rename c:\temp\file.txt file_new.txt

I think you can also use ren instead of rename.

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
If you want to put a datetimestamp on a file you do it like this.

rename c:\temp\myBackup.BAK myBackup%datetimestamp%.bak

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Hi Paul,

How do you set the variable %datetimestamp%?

That is what I'm confused about.

Thanks
 
You can use the DOS command FOR to take the output from the DOS command DATE (with the /T switch) to set the variable.

From a command prompt type "HELP FOR" and you'll get pages of useful info about the command.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
What about this?

set Day=%Date:~0,2%
set Mth=%Date:~3,2%
set Yr=%Date:~6,4%
ren test.txt test%Day%%Mth%%Yr%.txt

But it says my syntax is incorrect.
 
This is what I use in my bat file and it works ok.

set datestamp=%DATE:~-4%_%DATE:~4,-8%_%DATE:~7,-5%
set datestamp=%datestamp: =0%

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
I'm sorry, I found the examples extremely confusing.

Isn't there an easier way?
 
Create a bat file to rename your bak file. in the bat file you would have this.

Code:
set datestamp=%DATE:~-4%_%DATE:~4,-8%_%DATE:~7,-5%
set datestamp=%datestamp: =0% 

ren c:\path_to_bak\myBackup.bak myBackup%datestamp%.bak



- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Kat,
Here is my bat file.

Code:
set datestamp=%DATE:~-4%_%DATE:~4,-8%_%DATE:~7,-5%
rem set datestamp=%datestamp: =0%

ren c:\atemp\myBackup.bak myBackup%datestamp%.bak

Here are the results.

myBackup2007_04_26.bak

Is that the output you are looking for?

- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Hmmm, just realized something...

I should be renaming the file with the file's own MODIFIED DATE timestamp (not the current date).

Is this even possible?

Thanks
 
hmmm,
I think it is but I don't know how.

Let me google it and see what I find.


- Paul
10qkyfp.gif

- If at first you don't succeed, find out if the loser gets anything.
 
Nevermind... don't need to do this after all

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top