Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a file at:

c:\source\hello.txt

That I want to backup as I modify it often, to here:

c:\backups\source\????\hello.txt

How can I create such a .bat file to do this?

I want the script to somehow create a sub-folder with the current date and time, so that when I view the folders it will be ordered so I can get the latest or go back in time easily.

share|improve this question
up vote 4 down vote accepted

Getting a main idea from here:

@echo off
set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%

if not exist "c:\backups\source\%dateseed%" mkdir "c:\backups\source\%dateseed%"
cd "c:\backups\source\%dateseed%"
copy "c:\source\hello.txt" .

It will create a folder named yyyymmdd_hhmmss (hh in 24-hours format).

You can also check copy /? for further options.

If you're working with files or folders other than on C: make sure you add a line to change to that drive before attempting to make a folder or change dir into one.

share|improve this answer

how to make your script, to copy 1 file into 2 different destination. but the file is not copied on folder 2.

What I did is:

@echo off
set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
set dateseed=%date:~10,4%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%

if not exist "c:\backups\source\%dateseed%" mkdir "c:\backups\source\%dateseed%"
cd "c:\backups\source\%dateseed%"
copy "c:\source\hello.txt" .

if not exist "c:\backups2\source\%dateseed%" mkdir "c:\backups\source\%dateseed%"
cd "c:\backups2\source\%dateseed%"
copy "c:\source\hello.txt
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .