I think this batch file question should be easy

Posted on 2002-07-15
Last Modified: 2010-04-13
I want to back up a single file (a backup file made by msbackup.exe) to a networked harddrive on a daily or weekly basis. I would like to write a batch file to rename and move the temporary backup file off the local computer and onto the networked drive. Finally, I want to be able to move it to a folder which gets created each transfer and named with today's date.


1. create c:\backuptemp.bkf with a scheduled task
2. create z:\date_of_backup directory (or even ##-##-## as the name of the folder)
3. move c:\backuptemp.bkf z:\date_of_backup\backupfile.bkf

can someone help me write this batch file? basically I need help understanding how a batch file can get to the date and store it to a variable. I know 'date' and 'temp' keywords exist but not how to use them. Your help is appriceated.

Question by:AJMan
LVL 63

Accepted Solution

SysExpert earned 100 total points
ID: 7154650
The following should be enough to get you started.

You might also look into scripting languages etc.
WSH, Kixtart etc.

From: czpczp    Date: 06/25/2001 12:59PM PST Time date in batch file name directory by date
     The following script does *exactly* what you're looking for.  It will use the Time command
   (not time/t 'cause it doesn't give the seconds which is what you want), and parses the output string
 to extract the time in reverse order SEC:MIN:HR *extracting* the semi-colons.  THEN, it creates a directory
     *including* the client's name.  I did extensively test the script using varying times without having
      any problems.

     Cut and paste (verbatim) the script below into file with a BAT extension.  The following creates the
       directory on the root of the current drive (last line) -- you can alter where the directory is created
        depending on your needs (i.e. Md F:\LOG\%UserName%_%DirTemp%).

 Rem  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  MakeDir.Bat ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                 @echo off

                 REM ** the following line gets the current time into a variable
                 echo.|time >holdstr

     REM ** The following 5 For statements parse the holdstr var to extract the time in reverser order
                 for /f "eol=E tokens=1,2,3*" %%a IN (holdstr) do echo %%a%%b%%c%%d  >holdstr
                 for /f "tokens=1,2* delims= " %%a IN (holdstr) do echo %%b  >holdstr
                 for /f "tokens=1,2* delims=." %%a IN (holdstr) do echo %%a  >holdstr
                 for /f "tokens=1,2,3 delims=:" %%a IN (holdstr) do echo %%c%%b%%a  >holdstr
                 for /f "tokens=1,2* delims= " %%a IN (holdstr) do set DirTemp=%%a%%b

                 REM ** remove temp file
                 del holdstr

                 Md \%UserName%_%DirTemp%

      Rem  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  MakeDir.Bat ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I hope this helps !
LVL 95

Expert Comment

by:Lee W, MVP
ID: 7155183
Well, I've pushed this a number of times, but...

I wrote a little 16 bit program that creates directories in the format you choose (with 8 character limits) that creates a directory and changes into it.  The batch file I would use with my utility is:

mdate /c /f
copy c:\backuptemp.bkf .

The mdate program is what I wrote and by default, it creates a directory based on the current date in the format YYYYMMDD.  /C changes into the created directory (allowing the following copy line) and /F forces the creation of the directory - if it finds another directory there using the current date, it appends a .001, etc up to .999

But sysexperts solution should work fine as well.
LVL 24

Expert Comment

ID: 7155829
1) for scheduling, either AT (command line) or WinAT (Windows) does fine

2) for daily backup, I've found no need to create a separate directory for each run for each day. One time creation for a slot for each of the seven days of the week is more than enough.

2a) Retaining anything older than a week is overkill for a daily

2b) by not changing name, the name retains meaningful information AND permits self-cleaning through overwriting of the previous run, whether a week old or an earlier botch-up for that day (bear in mind these require space that will quickly grow, otherwise, to point of eating up the server's drive). Date of backup is assessed by both the time-stamp of the file and the content of a log file I copy along with it (that can and does serve other purposes)

2c) tools are available for more of this, I've run Kixtart (see SysExpert comment) successfully in this respect

3) moving a file is trivial enough, but for a file such as this, critical, big, and bandwidth consumptive, you should include some additional error checking and validation of the transfer.

4) also validate 'restore' options.  You may find... how shall we say, that you'd really prefer a backup utility that is easier to use more effectively more often, that also has scheduling capability built in

5) consider also the use of pkzip, or one of its clones. by adding compression, the file(s) cost less to transfer and store, and other nomenclature possibilities are available

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now