Batch File: How to create directory with today's date ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to update a batch file I currently have for backing up an Oracle
database. It is currently something like:
call ShutdownOracleService.bat
call exportdata.bat
call ZipFilesToBackup.bat
call RestartOracleService.bat

It works fine but always dumps the files into the same directory. I want to
improve on this and start by creating a directory for today's date so each
night the backup runs its files will be stored in a folder. I want to do the
following:
rem ### NEW FUNCTIONALITY ###
mkdir TodaysDate 'ie: 2005-02-08
cd TodaysDate
rem ### END NEW FUNCTIONALITY ###
call ShutdownOracleService.bat
call exportdata.bat
call ZipFilesToBackup.bat
call RestartOracleService.bat

Can anyone tell me how to go about making this directory? I am pretty
simple when it comes to batch files. I was going to convert this nice simple
BAT file to a VBS, but I think that is overkill.

Thank You
 
cnuk said:
(snip)
I want to improve on this and start by creating a directory for
today's date so each night the backup runs its files will be
stored in a folder. I want to do the following:
rem ### NEW FUNCTIONALITY ###
mkdir TodaysDate 'ie: 2005-02-08
cd TodaysDate
rem ### END NEW FUNCTIONALITY ###
Hi

It is easy to create a small temporary VBScript file on the fly in
your batch file:

--------------------8<----------------------
@echo off
echo D = Now : WScript.Echo Year(D) ^& "-" ^& _ >%tmp%\today.vbs
echo Right(100+Month(D),2) ^& "-" ^& Right(100+Day(D),2) >>%tmp%\today.vbs

for /f "tokens=1" %%a in (
'cscript.exe //Nologo %tmp%\today.vbs') do set today=%%a

del %tmp%\today.vbs
echo Todays date: %today%
--------------------8<----------------------


You can do it with "pure" batch file as well, using e.g the batch date
time functions from Ritchie Lawrence batch library available at
http://www.commandline.co.uk/lib
 
Hello, cnuk:
On Tue, 8 Feb 2005 07:19:04 -0800: you wrote...

c> I am trying to update a batch file I currently have for backing up an
c> Oracle database. It is currently something like:
c> call ShutdownOracleService.bat
c> call exportdata.bat
c> call ZipFilesToBackup.bat
c> call RestartOracleService.bat
c>
c> It works fine but always dumps the files into the same directory. I
c> want to improve on this and start by creating a directory for today's
c> date so each night the backup runs its files will be stored in a folder.
c> I want to do the following:
c> rem ### NEW FUNCTIONALITY ###
c> mkdir TodaysDate 'ie: 2005-02-08
c> cd TodaysDate
c> rem ### END NEW FUNCTIONALITY ###
c> call ShutdownOracleService.bat
c> call exportdata.bat
c> call ZipFilesToBackup.bat
c> call RestartOracleService.bat
c>
c> Can anyone tell me how to go about making this directory? I am pretty
c> simple when it comes to batch files. I was going to convert this nice
c> simple BAT file to a VBS, but I think that is overkill.
c>
c> Thank You

@echo off
setlocal enableextensions
for /f "tokens=1-2" %%a in ("%DATE%") do for /f "tokens=1-3 delims=-/.," %%d
in ("%%b") do echo c:\%%f%%d%%e

would create c:\20050208 if you replaced echo with md. Test it with echo
first to see that it's correct for your systems's time.date settings. (Note
the for line is the last line adn one long line - it'll wrap here.) If you
really want the hyphens in then there add them between the vars like do
echo c:\%%f-%%d-%%e to get something like this c:\2005-02-08

Regards, Paul R. Sadowski [MVP].
 
You guys are awesome! Thank you. My only question is where did you guys
pickup all these powerful tips for batch files? Books or just years of
experience?

Thanks Again
 
cnuk said:
You guys are awesome! Thank you.

You're welcome.

PS: in case anyone is wondering why I am taking credit without having posted
in this thread, it was my suggestion in the m.p.s.vbscript newsgroup that
send my fellow Cannuck (or so I would guess?) to this here newsgroup :-)
My only question is where did you guys
pickup all these powerful tips for batch files? Books or just years of
experience?

newsgroups plus experience for me.

/Al
 
Hello, cnuk:
On Tue, 8 Feb 2005 17:51:02 -0800: you wrote...

c> You guys are awesome! Thank you. My only question is where did you
c> guys pickup all these powerful tips for batch files? Books or just
c> years of experience?

Finding my own solutions. Reading other people's questions and the answers
given to them - learning never stops. And asking for help from others like
Torgeir who posted above who not only answered some questions from me but
has posted lots of keen stuff in the WSH, VBS and WMI groups (and probably
more that I don't know).

Regards, Paul R. Sadowski [MVP].
 
Back
Top