I think this batch file question should be easy

Posted on 2002-07-15
Medium Priority
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 300 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 96

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

765 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