Create a Dir with date

J

Jim

Using Windows XP or Win 2003 Server.
Is there a way to make a batch file that will create a new folder with the
date?

For example, if it were this easy....,
MKDIR %DATE% would make a directory 11-13-2007

Thanks.
 
T

Tom Lavedas

Using Windows XP or Win 2003 Server.
Is there a way to make a batch file that will create a new folder with the
date?

For example, if it were this easy....,
MKDIR %DATE% would make a directory 11-13-2007

Thanks.

This is the FAQ of FAQs for batch oriented programming. A google
search should easily find a myriad of examples along the lines of ...

:: Batch to creare a DATE named folder at the current location
echo off
for /f "tokens=2-4 delims=/ " %%a in ('echo %date%') do mkdir %%c-%%a
%-%%b

I adjusted the US date format to make it the sortable yyyy-mm-dd
layout. Other locales would probably need to be %%c-%%b-%%a to yield
the same yyyy-mm-dd.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
 
M

Matt Williamson

Using Windows XP or Win 2003 Server.
Is there a way to make a batch file that will create a new folder with the
date?

For example, if it were this easy....,
MKDIR %DATE% would make a directory 11-13-2007

This is heavily dependent on your regional settings. What do you get when
you echo %date%? There are many ways to do it, but the most appropriate way
is to code for any possible date format. This requires a lot more code
though and is only necessary if you need to do it on many systems with
different regional formats. I generally do it this way using a short date
format of mm/dd/yyyy.

set dt=%date:/=-%&md %dt%

which just takes the input of %date% which is "11/14/2007" and replaces any
"/" with "-" to make it compatible with the characters windows accepts to
create a folder.

HTH

Matt
 
T

Tom Lavedas

That won't function; delayed expansion is sadly not a default behavior.

--
Dean Wells [MVP / Directory Services]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


This is heavily dependent on your regional settings. What do you get
when you echo %date%? There are many ways to do it, but the most
appropriate way is to code for any possible date format. This requires
a lot more code though and is only necessary if you need to do it on
many systems with different regional formats. I generally do it this
way using a short date format of mm/dd/yyyy.
set dt=%date:/=-%&md %dt%
which just takes the input of %date% which is "11/14/2007" and
replaces any "/" with "-" to make it compatible with the characters
windows accepts to create a folder.

Nor does it remove the day-name that %DATE% returns, even if it is
done as successive statements. Though it is a better way to remove
the problematic hyphens for the stated reason of locale independance.

Working with Mr. Williamson's approach I was able to find this
adaptation that does NOT need delayed expansion ...

for /f "tokens=2" %a in ("%date:/=-%") do md %a

It solves both problems and remains one (rather complicated) statement
at the command line - or a single statement in a batch procedure when
the percent signs are doubled.

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top