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

format date mmddyy in batch file

976 views
Skip to first unread message

pleasehelp

unread,
Jul 5, 2007, 2:31:19 PM7/5/07
to
Batch file help!!!
I need to make a variable in a batch file that will take the previous
days date and format it to:

mmddyy

I then need to use that in a file name(.zip)

for example if today is 7/5/2007, then I need a variable that produces
"A070407.zip"

How can I do this?

PLEASE HELP!

Timo Salmi

unread,
Jul 5, 2007, 4:09:06 PM7/5/07
to
pleasehelp <sto...@hotmail.com> wrote:
> I need to make a variable in a batch file that will take the previous
> days date and format it to:
> mmddyy

http://groups.google.com/group/alt.msdos.batch/msg/8c267951d6988661

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

ras...@yandex.ru

unread,
Jul 6, 2007, 12:14:48 AM7/6/07
to

@echo off
echo wscript.echo date-1>tmp.vbs
for /f %%a in ('cscript tmp.vbs //Nologo') do set "yesterday=%%a"
for /f "tokens=1-3 delims=/" %%a in ("%yesterday%") do (
set mm=%%a
set dd=%%b
set yy=%%c)
echo %mm%/%dd%/%yy:~-2% & DEL tmp.vbs

pleasehelp

unread,
Jul 6, 2007, 10:39:06 AM7/6/07
to
Timo - thank you for your very detailed files. I have read through
them, but can not make sense of them. I have very limited programing
experience. However, I did try for hours. You obviously are very good
at this and if possible I would apprecaite if you could assist me with
a script with includes the parts you mentioned above. I know it might
be much, but I would greatly appreciate it!


Ras-
Thank you for your help too. I did make sense of your script. It was
helpful and I understand the parsing that you are doing, except I
don't understand why there are spaces. for example the date is
showing 07 05 07 vs 070507. Do you know why?

thanks

foxidrive

unread,
Jul 6, 2007, 11:39:47 AM7/6/07
to
On Fri, 06 Jul 2007 07:39:06 -0700, pleasehelp <sto...@hotmail.com> wrote:

>Ras-
>Thank you for your help too. I did make sense of your script. It was
>helpful and I understand the parsing that you are doing, except I
>don't understand why there are spaces. for example the date is
>showing 07 05 07 vs 070507. Do you know why?

Try this:

@echo off
echo wscript.echo date-1>tmp.vbs
for /f %%a in ('cscript tmp.vbs //Nologo') do set "yesterday=%%a"
for /f "tokens=1-3 delims=/" %%a in ("%yesterday%") do (
set "mm=%%a"
set "dd=%%b"
set "yy=%%c"
)

if "%mm:~1,1%"=="" set mm=0%mm%
echo %yy:~-2%/%mm%/%dd%& DEL tmp.vbs


yymmdd is a good format for sorting by date on a PC so alter that if you
need to.

foxidrive

unread,
Jul 6, 2007, 11:47:32 AM7/6/07
to
On Fri, 06 Jul 2007 07:39:06 -0700, pleasehelp <sto...@hotmail.com> wrote:

>Ras-
>Thank you for your help too. I did make sense of your script. It was
>helpful and I understand the parsing that you are doing, except I
>don't understand why there are spaces. for example the date is
>showing 07 05 07 vs 070507. Do you know why?

I made a boo boo with the date segments and this might be locale dependant.
This works here (with WSH giving d/mm/yyyy)

@echo off
echo wscript.echo date-1>tmp.vbs
for /f %%a in ('cscript tmp.vbs //Nologo') do set "yesterday=%%a"
for /f "tokens=1-3 delims=/" %%a in ("%yesterday%") do (

set "dd=%%a"
set "mm=%%b"
set "yy=%%c"
)
if "%dd:~1,1%"=="" set dd=0%dd%


echo %yy:~-2%/%mm%/%dd%& DEL tmp.vbs


And here is another script with more options:

:: DateTime using WSH
:: datetime.bat V4
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: This uses Windows Scripting Host to set variables
:: to the current date/time/day/day_number
:: for Win9x/ME/NT/W2K/XP etc
:: Thanks go to Todd Vargo for his scripting
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
set TmpFile="%temp%.\tmp.vbs"
echo> %TmpFile% n=Now
echo>>%TmpFile% With WScript
echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2)
echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2)
echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2)
echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2)
echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1)
echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7)
echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
echo>>%TmpFile% End With
cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
call "%temp%.\tmp.bat"
del "%temp%.\tmp.bat"
del %TmpFile%
set TmpFile=
set stamp=%year%-%month%-%day%_%hour%.%min%.%sec%


echo The year (YYyy) is "%year%"
echo The year (yy) is "%yr%"
echo The month is "%month%"
echo The day (%dow%) is "%day%"
echo The full weekday name is "%dow2%"
echo.
echo ISO 8601 Day-Of-Week number is "%iso%"
echo.
echo The hour is "%hour%"
echo The minute is "%min%"
echo The second is "%sec%"
echo.

echo The date and time stamp is "%stamp%"
echo.
echo time (hhmmss) (%hour%%min%%sec%)
echo.
echo date A (yyyymmdd) (%year%%month%%day%)
echo date B (mmddyyyy) (%month%%day%%year%)
echo date C (ddmmyyyy) (%day%%month%%year%)
echo.
echo date D [yymmdd] [%yr%%month%%day%]
echo date E [mmddyy] [%month%%day%%yr%]
echo date F [ddmmyy] [%day%%month%%yr%]
:: datetime.bat
::::::::::::::::::::::::::::::::::::::::::::::::::::::::

pleasehelp

unread,
Jul 6, 2007, 11:55:39 AM7/6/07
to
foxidrive

I agree with your yymmdd naming convention, except I'm trying to
download a file named sMMDDYY.zip (s070507.zip).

I modified your code to this.....

@echo off
echo wscript.echo date-1>tmp.vbs
for /f %%a in ('cscript tmp.vbs //Nologo') do set "yesterday=%%a"
for /f "tokens=1-3 delims=/" %%a in ("%yesterday%") do (
set "mm=%%a"
set "dd=%%b"
set "yy=%%c"
)
if "%mm:~1,1%"=="" set mm=0%mm%

echo %mm%%dd%%yy:~-2%& DEL tmp.vbs

yet, I still get a weird result. I get 07 507 vs 070507. Do you know
why?

thanks!

pleasehelp

unread,
Jul 6, 2007, 11:59:06 AM7/6/07
to
GOT IT! THANK YOU!!!!

@echo off
echo wscript.echo date-1>tmp.vbs
for /f %%a in ('cscript tmp.vbs //Nologo') do set "yesterday=%%a"
for /f "tokens=1-3 delims=/" %%a in ("%yesterday%") do (
set "mm=%%a"
set "dd=%%b"
set "yy=%%c"
)
if "%mm:~1,1%"=="" set mm=0%mm%

if "%dd:~1,1%"=="" set dd=0%dd%

Dr J R Stockton

unread,
Jul 6, 2007, 11:53:52 AM7/6/07
to
In alt.msdos.batch message <1183660279.7...@57g2000hsv.googlegr
oups.com>, Thu, 5 Jul 2007 11:31:19, pleasehelp <sto...@hotmail.com>
posted:

>I need to make a variable in a batch file that will take the previous
>days date and format it to:
>
>mmddyy
>
>I then need to use that in a file name(.zip)
>
>for example if today is 7/5/2007, then I need a variable that produces
>"A070407.zip"

Rather a bad example, with D=Y.

That format (FFF) does not comply with ISO 8601.

NOWMINUS, via sig line 3, could generate its components, and will
generate the logical and standard YYMMDD in envvar with
NOWMINUS f1 j0 b3 Eenvvar
For later use in off-topic systems such as NT & XP, see NOWMINUS.TXT.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Richard Bonner

unread,
Jul 12, 2007, 9:01:58 AM7/12/07
to
pleasehelp wrote:
> Batch file help!!!
> I need to make a variable in a batch file that will take the previous
> days date and format it to:

> mmddyy

> I then need to use that in a file name(.zip)

*** XSET can alter date formats and place that format into a variable.
There are also utilities that can alter and do calculations to dates. One
that comes to mind is FDATE.

One or more links to these can be found in my "DOS Websites" Directory
at:

http://www.chebucto.ca/~ak621/DOS/Websites.html


Richard Bonner
http://www.chebucto.ca/~ak621/DOS/

0 new messages