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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 was originally published on Monitis Blog, you can check it  here . If you have responsibility for software in production, I bet you’d like to know more about it. I don’t mean that you’d like an extra peek into the bowels of the sour…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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