Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

create a new directory based upon the date

143 views
Skip to first unread message

Michael Martinell

unread,
Apr 12, 2000, 3:00:00 AM4/12/00
to
I need to create a new directory every day based upon the date and then FTP
some files into it. I have tried to create the directory by the following
commands

%comspec% /e:2048/c for %%v in (1 2) do prompt $q$d$_ | find/v "$" > d.txt
set test
type d.txt > test
md test

I think that there is something wrong with my variables, but cannot track
down what it is. Any help would be appreciated.


Phil Robyn

unread,
Apr 12, 2000, 3:00:00 AM4/12/00
to
Michael Martinell wrote:

This will create a directory named YYYY-MM-DD:

@echo off
for /f "tokens=1,2,3,4* delims=/ " %%i in ('date /t') do md %%l-%%j-%%k


Tom Lavedas

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to

Yes, in the US, but what about in Europe. I have taken to using this
instead (when I give advice on the net, anyway) ...

for /f "tokens=2" %%a in ('date/t') do set _Today=%%a
set _T=MM DD
for /f "tokens=1" %%a in ('echo.^|date 1/13/2000') do set _T=DD MM
date %_Today%
call :2nd %1 %_T%
goto :EOF
:2nd
set %2=%_Today:~0,2%
set %3=%_Today:~3,2%
set YYYY=%_Today:~6,4%
set DateName=%YYYY%%MM%%DD%

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/

Phil Robyn

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to
Tom Lavedas wrote:

Hi, Tom,

You're right; I'm always forgetting that the date and time formats are
(usually but not always) different. But wouldn't you want to

... 'echo.^|date 13/1/2000' ...

and

call :2nd %_T%

and

:2nd
set %1=%_Today:~0,2%
set %2=%_Today:~3,2%

instead?

Phil Robyn
Univ. of California, Berkeley


Tom Lavedas

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to

No, Phil, the FOR part is correct as it stands. The only time the
statement after the DO is executed is when the 1/13/2000 date format is
invalid (i.e. no MONTH 13), thus the reversal of the DD and MM
parameters. The logic is a little trick - I only figured it out
empirically myself.

However, you are right about the other part. I pulled that snippet of
code out of a working procedure and forgot I was passing a 'real' input
argument as %1 in the original, and failed to make the adjustment.

Phil Robyn

unread,
Apr 13, 2000, 3:00:00 AM4/13/00
to
Tom Lavedas wrote:

May I suggest a modified version, inspired by your excellent example above?

@echo off
::
:: sets datename to yyyymmdd according to whether the system date format
:: is dd/mm/yyyy or mm/dd/yyyy
::
setlocal
set scriptname=%~n0


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

for /f "tokens=* delims=" %%? in ('echo.^|date 13/1/2000 ^| find /v /c "$^&Z^@^!$"') do set /a
rc=%%?
set /a rc-=1
date %_Today%
if %rc% EQU 0 (
rem The date format on this system must be dd/mm/yyyy
call :setdate dd mm
) else (
rem The date format on this system must be mm/dd/yyyy
call :setdate mm dd
)
endlocal&set %scriptname%=%DateName%&goto :EOF

:setdate


set %1=%_Today:~0,2%
set %2=%_Today:~3,2%

set YYYY=%_Today:~6,4%
set DateName=%YYYY%%MM%%DD%

goto :EOF
:EOF


Regards,

Michael Jerkovic

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
Phil, Tom

I've often used this bit of code to return date information independent
of regionial settings. It is someting I had to develop as my the company
I work for is multinationial.

The reason this works is because date command will display something
like:

The current date is: Fri 14/04/2000
Enter the new date: (dd-mm-yy)

The useful bit there is the dd-mm-yy. From this I know that the 14 is dd
04 is mm and 2000 is yy. So after running the following, %dd% will be
the day of the month, the %mm% will be the month and the %yy% will be the
year.

for /f "tokens=2-4 skip=1 delims=(-)" %%i in ('echo.^|date') do (
for /f "tokens=1-4 delims=/ " %%m in ('date /t') do (
set day=%%m
set %%i=%%n
set %%j=%%o
set %%k=%%p
)
)

so echo %yy%%mm%%dd% will give me 20000414

Michael

steve hardy

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
good idea.

-sh

Tom Lavedas

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
Michael Jerkovic wrote:
>
> Phil, Tom
> Michael

Very, very, very cute. I LOVE it! Must admit I never would have
thought of that. The only teensy, tiny tweak I might apply would be to
add a couple more Y's after the %%k variable to name that variable YYYY,
instead of YY (Y2k and all that ;-)

Tom Lavedas

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
> > Tom Lavedas
> > -----------
> > http://www.pressroom.com/~tglbatch/
>

First, why is this 'better' and second, I don't quite follow the logic.
I guess I don't understand the syntax of the second SET like, i.e. ...

set /a rc-=1

Is that a C like shorthand for ...

set /a rc=%rc% - 1

? I must admit to not trying out this code.

Of course, all of this is a mote point since Mr. Jerkovic has already
eaten our lunch :-))
--

Phil Robyn

unread,
Apr 14, 2000, 3:00:00 AM4/14/00
to
Tom Lavedas wrote:

I don't know that it is 'better' per se; I just thought that it might be easier to
understand, since it doesn't rely on an undocumented peculiarity. The
logic is, try to set the date to '13/1/2000' and count the messages that
result from this attempt (rc stands for 'record count'). If the system
accepts a date of 13/1/2000, the 'date' command will return zero messages,
and thus that system's date format must be dd/mm/yyyy. If, on the other
hand, that system's date format is mm/dd/yyyy, then attempting to set the
date to 13/1/2000 will generate an error message, yielding a non-zero
record count.

>
> I guess I don't understand the syntax of the second SET like, i.e. ...
>
> set /a rc-=1
>
> Is that a C like shorthand for ...
>
> set /a rc=%rc% - 1

Yes; it is acceptable NT syntax. Also,

set /a rc = rc - 1

is legitimate (no need to use '%rc%').

>
>
> ? I must admit to not trying out this code.
>
> Of course, all of this is a mote point since Mr. Jerkovic has already
> eaten our lunch :-))

Well, I want to thank both you and Mr. Jerkovic for your fine solutions.
What would be really nice is to also fold in some time format checking
code (I think somebody posted an attempt a few weeks ago) to take care
of discerning both date and time formats in one convenient package!

Tone

unread,
Apr 16, 2000, 3:00:00 AM4/16/00
to
Wow, excellent, thread..........

Should rename this group batch wars........


V informative

Tone.

simon sheppard

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
One problem with your batch (and all the other replies so far) is that
using delims=/ makes the batch region specific

Czechoslovakia, France,Norway,Denmark, Hungary, Canadian-French, Poland
all use either 03.01.94 or 03-01-1994 as the default Date Delimiter.

In any case, users in any country can change the date delimiter to anything
they like.

This version avoids the problem...

@echo off
::
:: GETDATE2.cmd
::
:: Returns the date independent of regional settings

FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (
FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (
SET v_first=%%G
SET v_second=%%H
SET v_third=%%I
SET v_all=%%A
)
)
:s_next
CALL :s_set_vars %v_first% %v_second% %v_third% %v_all%
GOTO :eof

:s_set_vars
SET v_all_date=%4

SET %1=%v_all_date:~0,2%
SET %2=%v_all_date:~3,2%
SET %3=%v_all_date:~6,4%

ECHO Year: [%yy%] Month: [%mm%] Day: [%dd%]

You can also get a similar result using NOW.exe from the resource kit...

@echo off
::
:: GETDATE.cmd
::
:: Returns the date independent of regional settings
:: Requires NOW.exe in the resource kit

SETLOCAL
FOR /f "tokens=2,3,5 delims= " %%G IN ('now') DO SET v_month=%%G & SET
v_day=%%H & SET v_year=%%I

IF %v_month%==Jan SET v_month=1 & GOTO s_done
IF %v_month%==Feb SET v_month=2 & GOTO s_done
IF %v_month%==Mar SET v_month=3 & GOTO s_done
IF %v_month%==Apr SET v_month=4 & GOTO s_done
IF %v_month%==May SET v_month=5 & GOTO s_done
IF %v_month%==Jun SET v_month=6 & GOTO s_done
IF %v_month%==Jul SET v_month=7 & GOTO s_done
IF %v_month%==Aug SET v_month=8 & GOTO s_done
IF %v_month%==Sep SET v_month=9 & GOTO s_done
IF %v_month%==Oct SET v_month=10 & GOTO s_done
IF %v_month%==Nov SET v_month=11 & GOTO s_done
IF %v_month%==Dec SET v_month=12 & GOTO s_done

:s_done
ECHO The date is
ECHO Day: [%v_day%]
ECHO Month: [%v_month%]
ECHO Year: [%v_year%]

Simon Sheppard
______________

Michael Jerkovic <ma...@nospam.optusnet.com.au> wrote in message
news:8F16DB...@203.2.75.243...

Michael Jerkovic

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Simon

thanks for the info on those other date delimiters, most usefull.

The simplest way around extra delimiters is just to add them the delims=
syntax in the for command. The following should handle dates using ".",
"-" and "/" delimitors.


@echo off
for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (


for /f "tokens=1-4 delims=/ " %%m in ('date /t') do (
set day=%%m
set %%i=%%n
set %%j=%%o
set %%k=%%p
)
)

echo Year: [%yy%] Month: [%mm%] Day: [%dd%] Day of the Week: [%day%]


Michael


simonUNDERS...@hotmail.com (simon sheppard) wrote in
<8dirdo$42a$1...@lure.pipex.net>:

Michael Jerkovic

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Woops

changed the delims on the first for loop, but not on the second.

Try this instead...

@echo off
for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (

for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (


set day=%%m
set %%i=%%n
set %%j=%%o
set %%k=%%p
)
)
echo Year: [%yy%] Month: [%mm%] Day: [%dd%] Day of the Week: [%day%]

Michael

ma...@nospam.optusnet.com.au (Michael Jerkovic) wrote in
<8F1BA4...@203.2.194.51>:

simon sheppard

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Here's a shorter version that allows any delimiter (you do sometimes get
people typing in # and all kinds of stuff as a delimiter)

@echo off
::
:: GETDATE2.cmd
::
:: Returns the date independent of regional settings

FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (
FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (

SET v_all_date=%%A

SET v_%%G=%v_all_date:~0,2%
SET v_%%H=%v_all_date:~3,2%
SET v_%%I=%v_all_date:~6,4%
)
)
echo Year: [%v_yy%] Month: [%v_mm%] Day: [%v_dd%]

Michael Jerkovic <ma...@nospam.optusnet.com.au> wrote in message

news:8F1BA8...@203.2.194.51...

Michael Jerkovic

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Simon

I like, nice improvement. Regional settings dialog only offers "." "-"
and "/" as delimiters, but you never known what users will do!

Michael


simonUNDERS...@hotmail.com (simon sheppard) wrote in

<8djss1$lbq$1...@lure.pipex.net>:

Michael Jerkovic

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Simon

I just dug up another bit of code which you may be interested in using with
your NOW.exe version of getdate.cmd


To translate the 3 letter month moniker to its equivalent month number, you
could try placing this subroutine in your batch file:

:GetMthNo
set mm=
for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dev) do (
set /a mm=mm+1
if "%%a"=="%1" goto :EOF
)
goto :EOF

and call this subroutine from elsewhere in the batch file with something
like:

call :GetMthNo Oct

then "echo %mm%" would display 10.

Not as easy to understand as your code, but if you like it terse, there it
is...

Michael

simonUNDERS...@hotmail.com (simon sheppard) wrote in

<8dirdo$42a$1...@lure.pipex.net>:

>You can also get a similar result using NOW.exe from the resource kit...
>
> @echo off
> ::
> :: GETDATE.cmd
> ::

> :: Returns the date independent of regional settings

simon sheppard

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
oops - that last version was bugged
here's what I meant to do

@echo off
::
:: GETDATE.cmd
::
:: Returns the date independent of regional settings

FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (


FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (
SET v_first=%%G
SET v_second=%%H
SET v_third=%%I
SET v_all=%%A
)
)

SET %v_first%=%v_all:~0,2%
SET %v_second%=%v_all:~3,2%
SET %v_third%=%v_all:~6,4%

ECHO Year: [%yy%] Month: [%mm%] Day: [%dd%]

Previous (less elegant) version..

@echo off
::
:: GETDATE.cmd
::
:: Returns the date independent of regional settings

FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (


FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (
SET v_first=%%G
SET v_second=%%H
SET v_third=%%I
SET v_all=%%A
)
)
:s_next
CALL :s_set_vars %v_first% %v_second% %v_third% %v_all%
GOTO :eof

:s_set_vars
SET v_all_date=%4

SET %1=%v_all_date:~0,2%
SET %2=%v_all_date:~3,2%
SET %3=%v_all_date:~6,4%

ECHO Year: [%yy%] Month: [%mm%] Day: [%dd%]

> Michael Jerkovic <ma...@nospam.optusnet.com.au> wrote in message
> news:8F1BA8...@203.2.194.51...
> > Woops
> >
> > changed the delims on the first for loop, but not on the second.
> >
> > Try this instead...
> >
> > @echo off
> > for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
> > for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
> > set day=%%m
> > set %%i=%%n
> > set %%j=%%o
> > set %%k=%%p
> > )
> > )
> > echo Year: [%yy%] Month: [%mm%] Day: [%dd%] Day of the Week: [%day%]
> >
> > Michael
> >

<snipped.. other versions>

simon sheppard

unread,
Apr 19, 2000, 3:00:00 AM4/19/00
to
Michael Jerkovic <ma...@nospam.optusnet.com.au> wrote in message
news:8F1BF3...@203.2.75.243...

> Simon
>
> I just dug up another bit of code which you may be interested in using
with
> your NOW.exe version of getdate.cmd
>
>
> To translate the 3 letter month moniker to its equivalent month number,
you
> could try placing this subroutine in your batch file:
>
> :GetMthNo
> set mm=
> for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dev) do (
> set /a mm=mm+1
> if "%%a"=="%1" goto :EOF
> )
> goto :EOF
>
> and call this subroutine from elsewhere in the batch file with something
> like:
>
> call :GetMthNo Oct
>
> then "echo %mm%" would display 10.
>
> Not as easy to understand as your code, but if you like it terse, there it
> is...
>
> Michael
>
<snip>

I like it,
but I like the version that doesn't require resource kit utils even better
:)

Simon Sheppard
______________

0 new messages