Discussion Locked

Batch file to create a dated directory

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I would like to create a batch to run with the NT scheduler to create a directory each day, and then copy a Database I export each day into the folder.
~kind of a supplement for my Tape backups, for a fairly compact mission-critical database.

Basically I want the script to create a folder with the given days date, at the specified time, and copy the DB inside each day.

Anyone help? my scripting and Dos aren't quite as sharp as I would like sometimes.

11 replies

by rorycarlin

Try this:

date /t > today.txt
munge munge.txt today.txt
set /p today= < today.txt
md "%today%"

munge.txt will have to look like this :

"/" "."

You can find munge.exe in the NT4 resource kit. It's need to change the / to . in the datestring as the md command interprets / as a delimiter to indicate a subdirectory.

Hope this helps.

by shanghai sam

All sounds good but the script is erroring with "the system could not find the environment option that was entered".

Seems like your set command isn't drawing the data from the today.txt file. ~how can I get it to show me the value of the VAR today so I can be sure?

The solution to the original problem was still incomplete though as I need a variable for the location in which I will be copying the database into e.g.:
copy data.mdb c:\backup\%today%

Thanks, appreciate the help

by karlww

If you don't have the resource kit you can try creating a batch file like this to set some environment variables and then call it from within another batch file.

SET_NOW_ENV.CMD
===============
@for /f %%i in ('date /t') do @set NOW_DOW=%%i
@for /f "tokens=2,3,4* delims=/ " %%i in ('date /t') do @set NOW_DAY=%%j
@for /f "tokens=2,3,4* delims=/ " %%i in ('date /t') do @set NOW_MONTH=%%i
@for /f "tokens=2,3,4* delims=/ " %%i in ('date /t') do @set NOW_YEAR=%%k

by shanghai sam

Appreciate the effort but I do have Munge.exe from the NT kit.

points are still there though if I can get a complete solution

thanks

by rorycarlin

Apologies. I tested this script on Win2k and it worked fine. It wont work on NT4 as the set command doesn't support the /p switch which prompts for a value and therefore allows redirection of the type command's output. If I can see any other way round it I'll let you know. Alternatively you could try the script on a win2k machine.

by rorycarlin

I think I've got a script that works with NT4:

date /t>today.txt
munge munge.txt today.txt
@for /f "delims==," %%i in (today.txt) do set today=%%i
md c:\backup\"%today%"

munge.txt still contains :
"/" "."

Obviously you can replace c:\backup\ with the path of your choice.

Hope this helps.

by ngunity

Thankyou the above worked to create the dated directory. I still have a problem with the second half of my Question:

I can't get the syntax to copy the file into the new directory though. I have tried putting the following into the script:
Copytoday.txt c:\mydocu~1\%today%

as a test to see if I can copy to the directory specified with the date variable. It will echo:
copy today.txt c:\mydocu~1\mon 10.29.2001
~which is the created directory which does exist. But I keep getting syntaxerrors. I have tried using " to get around the 8.3 convention but I keep getting syntax errors and can't copy into the dir.

I think you deserve the points but if you could finalise the answer it would be very helpful, thanks.

by rorycarlin

Ok, here's a variation on the previous script:

date /t>today.txt
munge munge.txt today.txt
@for /f "delims==," %%i in (today.txt) do set today="%%i"
md c:\backup\%today%
copy today.txt c:\backup\%today%

Make sure that there are no trailingspaces on any of the lines if you copy and paste it. Munge.txt has another line to remove spaces from the string so it looks like this:

"/" "."
" " ""

Hope this helps.

by shanghai sam

Munging the space out of the date so it read "wed31.10.2001" or the like seems to have worked

Thankyou for your help, appreciate you sticking with it until the problem was completely solved

This discussion is closed to new replies.