cas: DOS/Windows batch programming help?

blakerwry

Storage? I am Storage!
Joined
Oct 12, 2002
Messages
4,203
Location
Kansas City, USA
Website
justblake.com
Hey, I'm looking for a way to use a batch file to create directories based on the current date.... this is an extention of a batch file that creates a nightly backup of a friend's files.

i tried:
Code:
mkdir %date$

however the %date% command returns slashes (ex: 1/29/2003) that will not work with mkdir.

I need some way to tokenize the information returned form %date% and replace the slashes with dashes (ex: 1/29/2003 becomes 1-29-2003)... or just use another commadn that returns usable data.

I want to keep the existing batch file cause it's what's currently in place, easy to modify, single file solution, and easy to read/understand.


if i cant end up doing all this with a batch file i will probably try to make a c++ .exe file that will take the input from %date% and format it correctly... err... actaully...possibly even make it do must of the grunt work and just have the .bat be a "front-end". But i would like to keep this a single file solution if possible... anybody got some ideas for me?



Here's the current batch file... keep in mind that I am giving this to a computer user with limited DOS exposure who will need to make changes occasionally.
Code:
::***************************************
:: Backup script v0.9 1/29/2003 12:23am *
:: Blake Hudson  [email]blake@anime-jennie.com[/email] *
::***************************************
cls
@ECHO OFF

:: change to the source directory

c:
cd\car2000


:: create a new folder with the date on the x: drive (to be implemented later)



:: copy files from current directory to x:\  (later x:\%date%\)

@ECHO :
@ECHO :

@ECHO Copying *.DBF....
xcopy *.DBF x:\ /VYFH
@ECHO :
@ECHO :

@ECHO Copying *.CDX....
xcopy *.CDX x:\ /VYFH

@ECHO :
@ECHO :

@ECHO Copying CAR.D*....
xcopy car.d* x:\ /VYFH
 

Tannin

Storage? I am Storage!
Joined
Jan 15, 2002
Messages
4,448
Location
Huon Valley, Tasmania
Website
www.redhill.net.au
I have something at the office that should do the trick, I think. It's something I hacked from something else to create backups named for the day they were created, but I think it could be hacked once again to do what you want. I'll take a look at it in the morning.
 

Fushigi

Storage Is My Life
Joined
Jan 23, 2002
Messages
2,890
Location
Illinois, USA
Dunno about other Windows versions, but under XP Pro:

mkdir %date:~4,2%-%date:~7,2%-%date:~-4%

BTW, Windows XP defaults the %date% var to dow mm-dd-yyyy (dow=Day of Week; Wed for today).

- Fushigi
 

blakerwry

Storage? I am Storage!
Joined
Oct 12, 2002
Messages
4,203
Location
Kansas City, USA
Website
justblake.com
well, i decided to be a wuss and get a .exe to do the job for me.... I am testing this on winXp... but it should be compatible with win2k and XP at the least so that means cmd.exe.

Fushigi... on my winXP SP1 system
%date% returns "01/29/2003" ... i dont know how yours does any different... is your date format in regional settings different from mine perhaps?

I'll try the other thing you listed.... it doesnt work correctly now, but maybe i can mod it.

Merc:
i tried using FIND... all i could do with it was to strip out entire lines of output that conatined a search character... so if a line contained a "/" the entire line would be gone... instead of just the "/"... maybe it was too late at night for me but i dont know if FIND can do what i want.
 

blakerwry

Storage? I am Storage!
Joined
Oct 12, 2002
Messages
4,203
Location
Kansas City, USA
Website
justblake.com
yup fushigi, regional setttings were the discrepency between my sytem and yours..... that's bad.... because my system is set to English(Canadian) and is customized so measurement and time/date format appear the same as English (US)....

you are set to English(US) which is unfortunately diff from my customized canadian

And the person who is using this program is on a Chinese version of windows 2k....

maybe i should just stick with the .exe i found...



here's a copy of my code if you're intersted
Code:
::***************************************
:: Backup script v1.1 1/29/2003 12:37pm *
:: Blake Hudson  [email]blake@anime-jennie.com[/email] *
::***************************************
@ECHO OFF
cls

::****************************Change Log********************************
::                                                                     *
::- 1.1 - 1/29/2003  1:37pm                                            *
::  Added: command line options 1, 2, 3, h                             *
::  Added: error checking command line options                         *
::  Changes: code optimization                                         *
::                                                                     *
::- 1.0 - 1/29/2003 12:26pm                                            *
::  Added: dated backups, files are now placed into X:\%date%          *
::  Required: FDATE.EXE                                                *
::                                                                     *
::- v 0.9 - 1/28/2003 11:30pm (Initial Relase)                         *
::  Offered basic backup of .DBF, .CDX and CAR.D* files to x:\         *
::                                                                     *
::**********************************************************************


::uncomment this line to see the details of the program at work
::@ECHO ON




:: Get date into the environmental variable variable %date1%
::
:: Date Formats:
:: date1: January 29, 20003    (Easiest to read, but can't be organized alphabetically and includes blank spaces)
:: date2: 01-29-2003 (default) (Easy to read, better alphabetical ordering)
:: date3: 2003-01-29           (Hardest to read, best alphabetical ordering)

if (%1)==(/h) goto help
if (%1)==() goto date2
if (%1)==(/1) goto date1
if (%1)==(/2) goto date2
if (%1)==(/3) goto date3
goto help

:return



:: change to the source directory

c:
cd\car2000


:: create a new folder with the date on the x: drive

mkdir x:\"%date1%"

:: copy files from current directory to x:\  

@ECHO.
@ECHO.

@ECHO Copying *.DBF....
xcopy *.DBF "x:\%date1%" /VYFH
@ECHO. 
@ECHO.

@ECHO Copying *.CDX....
xcopy *.CDX "x:\%date1%" /VYFH

@ECHO. 
@ECHO.

@ECHO Copying CAR.D*....
xcopy car.d* "x:\%date1%" /VYFH

@ECHO. 
@ECHO.

@ECHO Backup Completed.
goto end




::CONTINUATION OF DATE FORMATING

:date1
FDATE /Ff /Atoday /O"mn zd, ccyy" /P"@SET DATE1=" >JUNKTEMP.BAT
call JUNKTEMP.BAT
del  JUNKTEMP.BAT
goto return

:date2
FDATE /Ff /Atoday /Omm-dd-ccyy  /P"@SET DATE1=" >JUNKTEMP.BAT
call JUNKTEMP.BAT
del  JUNKTEMP.BAT
goto return

:date3
FDATE /Ff /Atoday /Occyy-mm-dd  /P"@SET DATE1=" >JUNKTEMP.BAT
call JUNKTEMP.BAT
del  JUNKTEMP.BAT
goto return


:help
@ECHO Usage: "Backup.bat <option>"
@ECHO.
@ECHO Valid Options:
@ECHO   /1 Use long date format when creating a backup (EX: January 29, 2003)
@ECHO   /2 Use numerical date format (default) (EX: 01-29-2003)
@ECHO   /3 Use alphabetizable numerical date format (EX: 2003-1-29)
@ECHO   /h See the help screen
@ECHO.

:end
 

blakerwry

Storage? I am Storage!
Joined
Oct 12, 2002
Messages
4,203
Location
Kansas City, USA
Website
justblake.com
here's my completed(?) code... source and destination can easily be modified using global vriables now..... all that needs to be changed to customize this are the file types you wish to copy...

Code:
::****************************************
:: Backup script v1.5 1/29/2003  4:42pm  *
:: Blake Hudson  [email]blake@anime-jennie.com[/email]  *
::****************************************

::****************************Change Log********************************
::                                                                     *
::- 1.5 - 1/29/2003  4:42pm                                            *
::  Added: explanations and syntax of global variables                 *
::  Added: Always creates output folder if it does not exist           *
::  Added: Program title and source/destination display before copy    *
::  Added: Command line option /C to display change log                *
::  Changed: Upper and lower case command line options work now        *
::                                                                     *
::- 1.4 - 1/29/2003  4:00pm                                            *
::  Added: global variables to make updating source/destination easier *
::                                                                     *
::- 1.3 - 1/29/2003  3:00pm                                            *
::  Added: ability to dump to x:\ again                                *
::  Changed: default now dumps to x:\                                  *
::  Changed: Fixed minor spelling errors                               *
::                                                                     *
::- 1.2 - 1/29/2003  2:00pm                                            *
::  Added: Compatibility with win9x and DOS (command.com)              *
::  Changed: Help now includes an example                              *
::                                                                     *
::- 1.1 - 1/29/2003  1:37pm                                            *
::  Added: command line options 1, 2, 3, h                             *
::  Added: error checking command line options                         *
::  Changed: code optimization                                         *
::                                                                     *
::- 1.0 - 1/29/2003 12:26pm                                            *
::  Added: dated backups, files are now placed into X:\%date%          *
::  Required: FDATE.EXE                                                *
::                                                                     *
::- v 0.9 - 1/28/2003 11:30pm (Initial Release)                        *
::  Offered basic backup of .DBF, .CDX and CAR.D* files to x:\         *
::                                                                     *
::**********************************************************************

@ECHO OFF
cls
@ECHO ****************************************
@ECHO * Backup script v1.5 1/29/2003  4:42pm *
@ECHO * Blake Hudson  [email]blake@anime-jennie.com[/email] *
@ECHO ****************************************
@ECHO.
@ECHO Type "Backup /h" for options
@ECHO.

::un-comment the following line to see the details of the program at work
::@ECHO ON


::********************<GLOBAL VARIABLES>**********************
::Set the source drive in the format c:
SET DRIVE=C:

::Set the source folder in the format car2000   (if the folder is c:\car2000)
SET SOURCE=CAR2000

::Set the output folder in the format X: or X:\folder\subfolder
SET OUTPUT=X:\

::Set the PATH
SET PATH=%PATH%;c:\
::********************</GLOBAL VARIABLES>*********************


::change to root directory
%drive%
cd\





::*****************<PROCESS COMMAND LINE VARIABLES>*********************
:: Get date into the environmental variable variable %date1%
::
:: Date Formats:
:: nodate: no folder creation is used... files just goto %output%
:: date1: January 29, 2003    (Easiest to read, but can't be organized alphabetically and includes blank spaces)
:: date2: 01-29-2003          (Easy to read, better alphabetical ordering)
:: date3: 2003-01-29          (Hardest to read, best alphabetical ordering)

if (%1)==(/h) goto help
if (%1)==(/H) goto help
if (%1)==(/c) goto changelog
if (%1)==(/C) goto changelog
if (%1)==() goto nodate
if (%1)==(/1) goto date1
if (%1)==(/2) goto date2
if (%1)==(/3) goto date3
goto help
:return
::*****************</PROCESS COMMAND LINE VARIABLES>*********************





::********************<DATE>**********************

:: change to the source directory
cd\%source%

:: create a new folder on the output drive
mkdir "%output%\%date1%"

@ECHo Source = %Drive%%Source%\      Destination = %output%%date1%

:: copy files from current directory to output folder
@ECHO.
@ECHO.
@ECHO Copying *.DBF....
xcopy *.DBF "%output%%date1%" /V /Y /F /H /I
@ECHO. 
@ECHO.
@ECHO Copying *.CDX....
xcopy *.CDX "%output%%date1%" /V /Y /F /H /I
@ECHO. 
@ECHO.
@ECHO Copying CAR.D*....
xcopy car.d* "%output%%date1%" /V /Y /F /H /I
@ECHO. 
@ECHO.
goto end
::********************</DATE>*********************






::*******************<NODATE>*********************

:nodate

:: change to the source directory
cd\%source%

::make sure that the destination folder exists
mkdir "%output%


@ECHo Source = %Drive%\%Source%\      Destination = %output%

:: copy files from current directory to %output%
@ECHO.
@ECHO.
@ECHO Copying *.DBF....
xcopy *.DBF "%output%" /V /Y /F /H /I
@ECHO. 
@ECHO.
@ECHO Copying *.CDX....
xcopy *.CDX "%output%" /V /Y /F /H /I
@ECHO. 
@ECHO.
@ECHO Copying CAR.D*....
xcopy car.d* "%output%" /V /Y /F /H /I
@ECHO. 
@ECHO.
goto end
::******************</NODATE>*********************





::*******************<DATE(cont.)>*********************

:date1
FDATE /Ff /Atoday /O"mn zd, ccyy" /P"@SET DATE1=" >JUNKTEMP.BAT
call JUNKTEMP.BAT
del  JUNKTEMP.BAT
goto return

:date2
FDATE /Ff /Atoday /Omm-dd-ccyy  /P"@SET DATE1=" >JUNKTEMP.BAT
call JUNKTEMP.BAT
del  JUNKTEMP.BAT
goto return

:date3
FDATE /Ff /Atoday /Occyy-mm-dd  /P"@SET DATE1=" >JUNKTEMP.BAT
call JUNKTEMP.BAT
del  JUNKTEMP.BAT
goto return
::******************</DATE(cont.)>*********************




::*******************<HELP>*********************

:help
@ECHO Usage: "Backup.bat <option>"
@ECHO.
@ECHO Example: "Backup.bat /1"
@ECHO.
@ECHO Valid Options:
@ECHO      No options will will dump to x:\ without creating a folder
@ECHO   /1 Use long date format when creating a backup (EX: January 29, 2003)
@ECHO   /2 Use numerical date format (default) (EX: 01-29-2003)
@ECHO   /3 Use alphabetizable numerical date format (EX: 2003-1-29)
@ECHO   /h See the help screen
@ECHO   /c See the change log
@ECHO.
goto end
::******************</HELP>*********************



::*******************<CHANGE LOG>*********************

:changelog
@ECHO ****************************Change Log*********************************
@ECHO *- 1.5 - 1/29/2003  4:42pm                                            *
@ECHO *  Added: explanations and syntax of global variables                 *
@ECHO *  Added: Always creates output folder if it does not exist           *
@ECHO *  Added: Program title and source/destination display before copy    *
@ECHO *  Added: Command line option /c to display change log                *
@ECHO *                                                                     *
@ECHO *- 1.4 - 1/29/2003  4:00pm                                            *
@ECHO *  Added: global variables to make updating source/destination easier *
@ECHO *                                                                     *
@ECHO *- 1.3 - 1/29/2003  3:00pm                                            *
@ECHO *  Added: ability to dump to x:\ again                                *
@ECHO *  Changed: default now dumps to x:\                                  *
@ECHO *  Changed: Fixed minor spelling errors                               *
@ECHO *                                                                     *
@ECHO *- 1.2 - 1/29/2003  2:00pm                                            *
@ECHO *  Added: Compatibility with win9x and DOS (command.com)              *
@ECHO *  Changed: Help now includes an example                              *
@ECHO *                                                                     *
@ECHO *- 1.1 - 1/29/2003  1:37pm                                            *
@ECHO *  Added: command line options 1, 2, 3, h                             *
@ECHO *  Added: error checking command line options                         *
@ECHO *  Changed: code optimization                                         *
@ECHO *                                                                     *
@ECHO *- 1.0 - 1/29/2003 12:26pm                                            *
@ECHO *  Added: dated backups, files are now placed into X:\"date"          *
@ECHO *  Required: FDATE.EXE                                                *
@ECHO *                                                                     *
@ECHO *- v 0.9 - 1/28/2003 11:30pm (Initial Release)                        *
@ECHO *  Offered basic backup of .DBF, .CDX and CAR.D* files to x:\         *
@ECHO *                                                                     *
@ECHO ***********************************************************************
goto end
::*******************</CHANGE LOG>*********************


:end
@ECHO Backup Completed.
@pause
 

Cliptin

Wannabe Storage Freak
Joined
Jan 22, 2002
Messages
1,206
Location
St. Elmo, TN
Website
www.whstrain.us
Fushigi said:
Dunno about other Windows versions, but under XP Pro:

mkdir %date:~4,2%-%date:~7,2%-%date:~-4%

BTW, Windows XP defaults the %date% var to dow mm-dd-yyyy (dow=Day of Week; Wed for today).

- Fushigi

I have never seen the system variables used that way (although I understand it now). Do you have a link to a resource that dicussed its use?
 

Fushigi

Storage Is My Life
Joined
Jan 23, 2002
Messages
2,890
Location
Illinois, USA
Cliptin said:
I have never seen the system variables used that way (although I understand it now). Do you have a link to a resource that dicussed its use?
From the DOS, er, command prompt, enter HELP SET and take a read. I didn't know it existed either before this morning.

- Fushigi
 

Fushigi

Storage Is My Life
Joined
Jan 23, 2002
Messages
2,890
Location
Illinois, USA
blakerwry said:
yup fushigi, regional setttings were the discrepency between my sytem and yours..... that's bad.... because my system is set to English(Canadian) and is customized so measurement and time/date format appear the same as English (US)....

you are set to English(US) which is unfortunately diff from my customized canadian

And the person who is using this program is on a Chinese version of windows 2k....
It also works on W2K but not on W98.

How about this:

mkdir %date%
if errorlevel == 1 mkdir %date:~-10,2%-%date:~-7,2%-%date:~-4%

- Fushigi
 

Cliptin

Wannabe Storage Freak
Joined
Jan 22, 2002
Messages
1,206
Location
St. Elmo, TN
Website
www.whstrain.us
This is the funtional equivalent of Fushigi's code:
Code:
for /F "tokens=2-4 delims=/ " %I in ("%date%") do @md %I-%J-%K

As is this:
Code:
for /F "tokens=2-4 delims=/ " %I in ('date /t') do @md %I-%J-%K

Maybe the second is more universal.
 

blakerwry

Storage? I am Storage!
Joined
Oct 12, 2002
Messages
4,203
Location
Kansas City, USA
Website
justblake.com
I decided to go with the FDATE.exe file because depending on the regional settings the Day of week may or may not be displayed... the month and day posistions might be switched... there may or may not be leading zeros... there may be 2 or 4 number years... and the delimiters might go from dashes to slashes.... the FDATE.exe works in DOS, win9x, and winNT based OS's with the same usable output.
 
Top