Quantcast

Maximum PC

It is currently Tue Sep 30, 2014 10:57 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: need help with a batch file
PostPosted: Fri Apr 11, 2008 10:23 am 
8086
8086

Joined: Mon Mar 10, 2008 10:44 am
Posts: 24
Location: butt crack of missouri
I'm a noob to batch files so bear with me. :)
I am copying a sql backup to another server and my batch file looks something like this:
copy "D:\data\backup\sql\backup.bak" "\\server\backup\"
I have it setup to run as an automated task over night but I want the file to be renamed to include the date. Is there a switch that would enable me to do that?

*edit* now that I think of it couldn't I just have it generate a file like backup1 backup2 and so on? Then I could just clear out the old ones every week or so.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 7:51 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Just specify the date in the filename.

i.e.

copy "D:\data\backup\sql\backup.bak" "\\server\backup\backup_041108.bak"


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 5:25 am 
8086
8086

Joined: Mon Mar 10, 2008 10:44 am
Posts: 24
Location: butt crack of missouri
I would have to go in each day and edit the file though wouldn't I? I want something that will do it automatically.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 8:45 am 
Team Member Top 100
Team Member Top 100

Joined: Sun Jul 01, 2007 9:30 am
Posts: 1421
Here you go. You may want to change the naming convention & make at least 7 batch files (1 for each day of the week... Mon2300, Tues2300, Wens2300 and so on)
http://www.maximumpc.com/forums/viewtopic.php?t=68610

Why are you using \\server\backup\xxx
Map a network drive & use the drive letter "N:\backup\xxx"

Check out our F@H team too!


You could probably use ntbackup too. It should already be installed or you can download it from M$'s website.
Go to start, run & type ntbackup, press enter.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 3:58 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
Couldn't you do some variable substitution for the destination?

like this (this isn't the correct syntax -- I usually code bash scripts)
Code:
set DATE=`date +%m%d%Y`
cp source c:\backups\%DATE%.bak



That should help somewhat.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 7:39 am 
8086
8086

Joined: Mon Mar 10, 2008 10:44 am
Posts: 24
Location: butt crack of missouri
smartcat, that's what I am looking for now if someone could chime in with the syntax.
00john00, I've thought of that, but would like to avoid it if its possible.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 11:07 am 
Team Member Top 100
Team Member Top 100

Joined: Sun Jul 01, 2007 9:30 am
Posts: 1421
I would love to avoid it too if possible but there's no other option thats so simple/fool proof.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 1:16 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
Found how to get the date as a variable:

Code:
%date:~-4,4%%date:~-7,2%%date:~0,2%


Use example:
Code:
echo HI > %date:~-4,4%%date:~-7,2%%date:~0,2%bak.txt


From: http://support.microsoft.com/kb/555314


Top
  Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 6:39 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
I prefer

Code:
for /f "usebackq tokens=2-4 delims=/ " %A in (`date /t`) do (set newDate=%A%B%C)

xcopy \\server\dir\file.bak \\server2\dir\file_%newDate%.bak


enjoy!

*It should also be noted that you will need to use %%A, %%B, %%C instead of %A, etc. when inside a batch file or you will have syntax errors that will drive you crazy.*


Top
  Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 6:43 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
smartcat99s wrote:
Found how to get the date as a variable:

Code:
%date:~-4,4%%date:~-7,2%%date:~0,2%


Use example:
Code:
echo HI > %date:~-4,4%%date:~-7,2%%date:~0,2%bak.txt


From: http://support.microsoft.com/kb/555314


That is just plain ugly...


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.