We help IT Professionals succeed at work.

Creating a dir with the name current date

omavideniz
omavideniz asked
on
hi,
is it possible to create a directory with the name of current date within a batch file?
thanks in advance
Comment
Watch Question

Top Expert 2007

Commented:
This batch file does what you want. The only requirement is that you must enable command extensions. These enable via the /x parameter to CMD.EXE.

@echo off

for /f "tokens=2 delims= " %%f in ('date /t') do set DateStr=%%f

for /f "tokens=1 delims=/" %%f in ('echo %DateStr%') do set DateDD=%%f
for /f "tokens=2 delims=/" %%f in ('echo %DateStr%') do set DateMM=%%f
for /f "tokens=3 delims=/" %%f in ('echo %DateStr%') do set DateYY=%%f

set DatePath=%DateYY%-%DateMM%-%DateDD%

mkdir %DatePath%

set DateStr=
set DatePath=
set DateYY=
set DateMM=
set DateDD=

Good Luck,
Steve

Commented:

omavideniz,

It's possible and relatively simple to get the date using batch commands
and then apply its value for naming and creating files and directories, etc...
There are various methods, some dependent on the OS the script is run
and the required format of the date value... i.e, a format of 01-16-2002
will be truncated in DOS mode to the 8.3 filename convention, etc...
I'll post a couple of scripts that will hopefully do what you ask
or perhaps give you some ideas to perform that task...
 

Commented:

Was writing as SteveGTR was posting... Note that Steve's example
is specific to Windows NT and won't work under Windows 9.x or MS-DOS...
Please post back if this script works for you and if not,
under which OS you need the script to run.

btw, omavideniz... You have a couple of unlocked, ungraded questions
that you might want to close out...
 

Author

Commented:
hi,
SteveGTR's one seems to work but i'm looking for an example that will also work under Win98.
thanks

Commented:

omavideniz,

Here are some examples for Windows 9.x supporting long file names...
These also work in DOS mode but will use the 8.3 filename format.
And by using the command.com shell instead of cmd.exe
these can be made to work under NT if necessary.

These examples set an environment variable containing the current system date.
The variable is named %datevar% but can be changed to any valid name.
Once the variable is set, it can be called to create a new directory
that is given the current date as a name as set in the variable.
I'll include a way of using the variable after it's been set,
but the contents of the variable in these examples
are merely echoed to the screen...

This example sets a variable named  %datevar%
having the current system date in the format  01-16-2002
and echoes it to the screen:


::datevar.bat
@echo off
cls
echo.|date>~tempvar.bat
echo set datevar=%%4>current.bat
call ~tempvar.bat
del ~tempvar.bat
del current.bat
echo.
echo  The date is %datevar%

::::


This next example is the same as above
but by changing the replaceable parameter of %4 to %3
it returns just the day of the week, like  Wed  ...
The variable is named %dayvar% and is echoed to the screen:


::dayvar.bat
@echo off
cls
echo.|date>~tempvar.bat
echo set dayvar=%%3>current.bat
call ~tempvar.bat
del ~tempvar.bat
del current.bat
cls
echo.
echo  The day is %dayvar%

::::

You can combine the parameters in the two examples above
to obtain the entire string if needed, or you can do it another way...
This following example is a different method to set a variable
which returns the entire date (including the weekday)
in the format of  Wed 01-16-2002  ...


::curdate.bat
@echo off
cls
echo @prompt set datevar=$d>~tmpvar1.bat
%comspec%/e:2048/c~tmpvar1.bat>~tmpvar2.bat
call ~tmpvar2.bat
for %%x in (~tmpvar*.bat) do del %%x
cls
echo.
echo The current date is %datevar%

::::


Once the variable is set, you can refer to it in the batch script
by enclosing the value with percent signs and then use it
to name a directory or for whatever purpose is necessary...

For example, to create a directory (within the current directory)
with the name of the variable's string value, you can do:

md %datevar%

To specify a path, you can do like:

md c:\%datevar%

To determine whether the directory already exists before attempting to create it,
you can test for the Nul device like this:

if not exist c:\%datevar%\nul md c:\%datevar%


Putting it all together and using the first example
to get and set a variable to the date in the format of  01-16-2002
and then create a directory named with that value would be like:


@echo off
cls
echo.|date>~tempvar.bat
echo set datevar=%%4>current.bat
for %%x in (call del) do %%x ~tempvar.bat
del current.bat
cls
if not exist c:\%datevar%\nul md c:\%datevar%

::::

Note that the mentioned formats are dependent on regional and country settings.
Hope these are what you need... Please post back if you want more about it...

 
Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013

Commented:
Some time ago I wrote a QuickBasic program that does what I believe you are asking for.  I called it MDATE - here's a brief summary of it and how it works and if you want to check it out, you can download it from www.leewilbur.com/software.html

     Entering MDATE /? on the command line will give a limited version of
     these notes and instructions.

Example - MDATE DDMONYY
     Creates a directory "22MAR99".
     
Example - MDATE DDMONYY /C
     Creates a directory "22MAR99" and changes into it.

Example - MDATE DDMONYY /F
     Creates a directory "22MAR99" and if that directory already exists,
     it will instead create "22MAR99.001" etc.

Default Format:  YYYYMMDD
     19990322

Author

Commented:
hi rin1010,
i tried your batch files under WinXP but can't get anything valuable. also it would be better if i may set the name of directory in a format like dd_mm_yyyy.
thanks.

Commented:

hi omavideniz,

What is it you mean for Windows XP? The scripts and batch file examples I provided
were debugged, written for and tested successfully under Windows 98
(and earlier MS-DOS versions) as you specified. (Although they will work under XP.)
If you copy them exactly they will do as intended. Please try the last one I posted,
which I'll paste below... But when you save it, give the batch file another name.
That is, instead of calling it datevar.bat name it something like datename.bat or similar.
 

Commented:



::dirvar.bat
@echo off
cls
echo.|date>~tempvar.bat
echo set datevar=%%4>current.bat
for %%x in (call del) do %%x ~tempvar.bat
del current.bat
cls
if not exist c:\%datevar%\nul md c:\%datevar%

 

Commented:
There's basically two ways of achieving this. The smiple way or the longwinded (and often complicated way)....

I'll cut straight to the bone here. I'm a DOS fan and know several solutions for several DOS versions. I suspect you're not a DOS guru and therefore I would suggest you use and external command 'MKDDIR Stuff for DD_MM_YY'.

This would create a folder named 'Stuff for 28_02_99' if the date were 28 feb 1999.

Notice also the MKDDIR command with it's double-D.

This command must obviously reside in the path and a good location would be the DOS directory of the Command folder or equivelent....

Versions could be written to cater for 8.3 as well as long file names.

The 'DD_MM_YY' bit could have also been any combination of 'DD', 'MM' and 'YY' (or 'YYYY') including any combination of delimiters such as '-', '_', '/' or ' ' or whatever. This would make the date part customisable.

Now for the bad news..... Such a command does not exist. That's not to say it can't be whipped up in a flash though....


Commented:
oh, btw, rin1010 is usually good at his stuff if you want to go down the pure DOS route.

Me, in situation like yours, I like to KISS.
omavideniz:

You have many open questions:

http://www.experts-exchange.com/jsp/qShow.jsp?qid=20222327
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20261522
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20255833
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20248161
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20247596
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20247451
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20239442
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20235419
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20234355
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20230738
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20227630
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20210091
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20163508
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20163400
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20156064
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20151257
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20149551
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20107807
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20107283
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20096836
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20095000
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20094242
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20081432
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20068029
http://www.experts-exchange.com/jsp/qShow.jsp?qid=12040399
http://www.experts-exchange.com/jsp/qShow.jsp?qid=12040320

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS:  http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!


thanks!
amp
community support moderator

2/6

Author

Commented:
none of the answers really satisfied me. but at the moment i have the solution with debug.exe trick to get the date.
Please provide more detail on the solution so that we can refund your points and PAQ this question.

thanks!
amp
community support moderator

Author

Commented:
here is the batch file that commits the solution.

- getdate.bat -

@echo off

echo R CX > debugscript
echo D>>debugscript
echo W>>debugscript
echo q>>debugscript

echo set date_var=>>setdate.bat

rem cut off cr/lf from the end of setdate.bat.
debug setdate.bat<debugscript>NUL

date/t >> setdate.bat
call setdate.bat

del setdate.bat
del debugscript
Top Expert 2007

Commented:
That set's a environment variable to the date in the format DDD MM/DD/YYYY, but rin1010's solution does that. Anyway, you wanted a date that could be used as a file name and your solution doesn't solve that.

Author

Commented:
sorry, i forgot that portion of the file.

md %date_var%

will create the directory.
Commented:

omavideniz,

You specified needing an example that will work under Windows 98 ...
The Debug solution you provide contains syntax specific to Windows NT
and won't work under Windows 98 ...

I offered two methods that will work under Windows 98
as well as Windows NT and other MS operating systems
for getting the date, setting the variable and creating the directory...
SteveGTR provided one for doing it under NT ...

If you want to rely on an external such as Debug to do it,
perhaps try leew's utility, which additionally offers formatting options...

If you want a method similar to your Debug solution (that does it only under NT)
but doesn't require calling Debug you can do it using ascii machine code like:

echo XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>~$.com
~$ set CurDir=>~$.bat
date/t>>~$.bat
call ~$.bat
del ~$.*

With the above you're not limited to the hard coded length of the variable's name
as specified by the reference in hex... -i.e., if you change the variable's name
to anything not equaling eight characters, your script won't work as is...
Please post back if you need further info or help with your question...
 

Author

Commented:
all i want to do is not to use any external tool and not a complicated binary solution. otherwise i can able to write down a simple executable rather than a batch file solution. but i'm looking for a simple batch file solution, however i put it off so long for a batch file and now its not so meaningfull to select between a batch file or an executable. anyway i will accept your comment.