Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

batch file for backing up files


  • Please log in to reply
8 replies to this topic

#1 hispaladin

hispaladin

  • Members
  • 245 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Middle of a corn field
  • Local time:05:40 AM

Posted 08 April 2013 - 09:52 AM

I am trying to make a batch file that will copy a file to a backup location and rename the backup to include the date in the file name.  Anyone have any ideas on how to do this?  I am thinking it shouldn't be to hard but I am having no luck so far.  



BC AdBot (Login to Remove)

 


#2 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:40 AM

Posted 08 April 2013 - 05:02 PM

Hello.

 

You can use the %DATE% environment variable. You may need do edit the string slightly if your date includes slashes or other illegal characters for filenames.

 

With Regards,

The Panda



#3 hispaladin

hispaladin
  • Topic Starter

  • Members
  • 245 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Middle of a corn field
  • Local time:05:40 AM

Posted 09 April 2013 - 08:45 AM

Thank you, after much digging and research I found what was needed.  While I still had to tweak things to get what I wanted I did finally get the finished product that i was hoping for.  Here is the command line I used to make the backups with a date and time stamp in such a way that they will not overwrite each other when it is used again the next day.

 

for /F "tokens=1-4 delims=/ " %%A in ('echo %date%') do rename "p:\grain tickets\backups\grntkden.fil" grntkden%%B%%C%%D.filbk
for /F "tokens=1-4 delims=:." %%A in ('echo %time%') do rename "p:\grain tickets\backups\grntkden*.filbk" grntkden????????-%%A%%B.fil
I have a command before this to make copies of the files in the backup folder then this command (tweaked for each file) runs after that and renames the copies with the date stamp.


#4 Crazy Cat

Crazy Cat

  • Members
  • 808 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Lunatic Asylum
  • Local time:10:40 PM

Posted 05 May 2013 - 12:05 AM

Use Xcopy command-line: http://technet.microsoft.com/en-us/library/cc771254(v=ws.10).aspx
 

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. ― Albert Einstein ― Insanity is doing the same thing, over and over again, but expecting different results.

 

InternetDefenseLeague-footer-badge.png


#5 hispaladin

hispaladin
  • Topic Starter

  • Members
  • 245 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Middle of a corn field
  • Local time:05:40 AM

Posted 06 May 2013 - 08:29 AM

While xcopy is a very useful tool it does not do what I needed it to do.  I needed a way to copy the files over and date stamp them so that we can tell when the copy was made easily (just looking at the file properties is a fine way to do that for someone who knows a little about computers but for some of our users it is a bit much to ask)  The batch file I showed above copies the files and then renames them with a date and time stamp in the filename.



#6 AceInfinity

AceInfinity

  • Members
  • 30 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Canada
  • Local time:03:40 AM

Posted 24 May 2013 - 09:56 PM

Look into robocopy. :) You might like to use that instead.


mvp.png
Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer


#7 hispaladin

hispaladin
  • Topic Starter

  • Members
  • 245 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Middle of a corn field
  • Local time:05:40 AM

Posted 28 May 2013 - 10:25 AM

Well unless I am completely missing something the only thing that will really help me with is copying the files to the backup location but it would not really help with adding a date\time stamp to the file name.  While copying all the files in a single step would clean up the code some the real headache was adding the date\time stamp to the file name.  Is there a way to do this with robocopy that I am just not seeing?  As it is there are only 10 files that are being copied so it is not an overly long or bulky script though it would come in handy when adding new locations to the list.  As it is, unless there is a way to use it to add the stamps to the file name I will just stay with what I have.  Why change what works well if the new will not work that much better.  



#8 annaharris

annaharris

  • Members
  • 12 posts
  • OFFLINE
  •  
  • Gender:Female
  • Location:USA
  • Local time:04:10 PM

Posted 30 May 2013 - 07:54 AM

Thanks PropagandaPanda for sharing the information on creating a batch file.

 



#9 Crazy Cat

Crazy Cat

  • Members
  • 808 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Lunatic Asylum
  • Local time:10:40 PM

Posted 01 July 2013 - 09:35 PM

Good resource of Windows Batch Scripting.

http://ss64.com/nt/
http://en.wikibooks.org/wiki/Windows_Batch_Scripting
 

Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. ― Albert Einstein ― Insanity is doing the same thing, over and over again, but expecting different results.

 

InternetDefenseLeague-footer-badge.png





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users