Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have looked through many batch file tutorials and web pages trying to figue out how I can use a batch file to create a directory with the current date and then copy some files to that directory.
Any idea's/suggestions would be greatly appreciated. Thanks

This may not be as trivial as it sounds, for a BATCH file at least.
The variable %date% can be used to output the current date but, the format of the output is where the problem starts.
The output from the command %date% would look like this: 08/09/2005
As you may know, the "/" character cannot be used in folder names. So using the output of %date% for a folder name would actually make a folder named something like "08" (without the quotes).
You would need to replace all assurances of / with a period, or similar legal char.

This batch create directory for example:
08.09.2005
Replace the echo Command against the xcopy.
@echo offdate /t >date.txt
FOR /f "tokens=2 skip=2 delims= " %%a in ('find "." date.txt') do (mkdir %%a
echo %%a)
hope this helps
uli

Uli, I tried the code above and had to change the find "." to find " " to get the variable to contain the date, but it is still coming up in the xx/xx/xxxx format.
Thanks for your suggestions.

Hi uli,
That will work for 2k/xp, but not 9x.
In Nt there is a built-in %date%, so you just need to accomodate the layout.
My date layout is:
Thu 08-09-2005
So I can use this:
xcopy somefile %date:~10,4%%date:~7,2%%date:~4,2%\
[xcopy creates the directory for you]
###
I DOS and DOS based winders [3x 9x] it takes a little more doing.This will get DAY, DATE & TIME into vars:
:: DDT-DOS.bat
echo.|date>datevar.bat
echo.|time>timevar.batecho set DATEvar=%%5>the.bat
call datevarecho set DAYvar=%%4>the.bat
call datevarecho set TIMEvar=%%4>the.bat
call timevar
:: doneAs pointed out by Cache, there are some possible snags, not least is illegal chars.
Another is the verbage which is spewed out by the DATE and TIME commands.
It will vary widely with, among other things, language, version, codepage and regiomal settings.
To get a nice neat YYYYMMDD as my NT example, you need debug.
HTH
If at first you don't succeed, you're about average.M2

Oh , sorry.
My NT4 date layout is Fr 09.09.2005 (european Version).
I don´t use the command line from Win9*.Hi M2,
my not really working solution is a bit bumpy, not as elegant as yours. ;-)
I try to do to much with the For loop. But I do my best to learn.
Thanks a lot. :-))Uli

Really just use this....
md c:\dirname\%Date:~-4, 4%%Date:~-10, 2%%Date:~-7, 2%
copy filename c:\dirname\%Date:~-4, 4%%Date:~-10, 2%%Date:~-7, 2%

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |