1.

Solve : Batch Files That Create Dir's w/ Today's Date?

Answer»

Hi All,

I'm trying to write a batch file that will create a directory named with TODAY's date and time: i.e., mmddyyyyhourmin.  I'm aware of the existence of the %time% and %date% variables.  But, %date% is of the format: Thu mm/dd/yyyy.  And, %time% is formated: hh:min:sec.msec.  How can I extract the pieces I need to form the string I need to pass to mkdir: i.e., mkdir mmddyyyyhourmin?

Thanks.
Quote

I need to pass to mkdir: i.e., mkdir mmddyyyyhourmin?

Well, I recommend yyyymmdd so your names SORT like this
200612311104
200701010015
etc.

It is normally a mistake to put year last, but you can easily patch my code below if your management is stupid and insists.

If the BAT file is just for your own use, fine. Otherwise, be aware that %date% and %time% are not defined in WindowsNT DOS (and maybe others)

I had a solution that required 'date /t', but learned that other systems don't have that command.

So I don't know a solution that is portable. This one will work on your system. The SET DATE and TIME are just to give me test data as %date% normally returns nothing for me.

Mac

Code: [Select]echo off
set date=10/26/2006
set time=11:04:30:22
cls
echo Debugging for date=%date% and time=%time%
echo.
for /F "tokens=1 delims=/" %%i in ('echo %date%') do (set dmm=%%i)
for /F "tokens=2 delims=/" %%i in ('echo %date%') do (set ddd=%%i)
for /F "tokens=3 delims=/" %%i in ('echo %date%') do (set dyy=%%i)
for /F "tokens=1 delims=:" %%i in ('echo %time%') do (set thh=%%i)
for /F "tokens=2 delims=:" %%i in ('echo %time%') do (set tmm=%%i)
echo The directory name will be %dyy%%dmm%%ddd%%thh%%tmm%
QBasicMac: Windows 2000 / XP / 2003 / Vista have built-in DATE and TIME environment variables.  I'm not sure about Windows NT.  I would strongly recommend against replacing these system variables with set time= or set date=.  

The format of %date% and %time% depends on your regional settings.  If yours is set for U.S. English this should work.  For mmddyyyyhourmin (as requested) you can make a directory with:
Code: [Select]set hour=%time:~0,2%
if %hour% LSS 10 set hour=0%hour:~-1%
md %date:~-10,2%%date:~-7,2%%date:~-4%%hour%%time:~3,2%
Like QbasicMac, I personally prefer to use the year first.  So for the format of yyyymmddhourmin you would replace the MD line with:
Code: [Select]md %date:~-4%%date:~-10,2%%date:~-7,2%%hour%%time:~3,2%
The reason I do the HOUR seperately is because by default a single digit hour (1-9) is padded with a space. Quote
I would strongly recommend against replacing these system variables with set time= or set date=.  

Oh, definitely!! I thought I made it clear that I did that only for debugging my NT script where they are not reserved WORDS. I guess I failed.

Right! Do not put SET DATE in your script, OP, especially if you WANT the current date rather than a constant.

Mac

For a more "portable" solution. You can use Perl/Python or other scripting languages.
Eg in Python
Code: [Select]import time,os
date = time.strftime("%Y%d%m%H%M%S" ,time.localtime()) ## date = 20062710110431
os.mkdir(date) #make directory with  format 20062710110431

Of course, this assumes you know other languages besides batch


Discussion

No Comment Found