Formating (Inserting Dashes) yesterday's date in batch file!!

Hello Guys,

My question is basically the next step into "juststeve's" problem where he wanted to get yesterday's date in batch file. K 2K gave an excellent solution (in form of a VBScript) which generates yesterday's date as follows:-
On 30/03/2004, the script generates: 20040329 (YYYYMMDD)format.

What I need to do is just to insert dashes in this date and generate a date in teh following format: YYYY-MM-DD.

I would really appreciate your help in this matter.

Here is the code for that VBScript:-
*************************************
@echo off

:: setup variables for script
setlocal

:: script file name should not change
set vbscript=%temp%\~%~n0.vbs
:: command to run script
set vbrun=cscript /NoLogo "%vbscript%"

::write script
>"%vbscript%" echo ' Temporary script for %0
>>"%vbscript%" echo NewDate = DateAdd("d", -1, NOW())
>>"%vbscript%" echo Wscript.StdOut.Write "SET yesterdayFilename=ex" ^&_
>>"%vbscript%" echo right( DatePart("yyyy", NewDate) , 2)  ^&_
>>"%vbscript%" echo right( "0" ^& DatePart("m", NewDate) , 2)  ^&_
>>"%vbscript%" echo right( "0" ^& DatePart("d", NewDate) , 2) ^& ".log"
:: fake pause here to let file finish writing
ping -n 1 127.0.0.1 -w 500 >NUL

:: Use  %vbrun% to get date
for /F "tokens=2 delims==" %%K in ('%vbrun%') do (
  set yesterdayFilename=%%K
  )

Regards,

PCMENIAC

PCMENIACAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Da-BuConnect With a Mentor Commented:
I belived that the error will occur so I posted the right line of code that is without parentheses; I posted this line of code
>>"%vbscript%" echo right "-" it does the same thing as  >>"%vbscript%" echo right( DatePart("yyyy", NewDate) , 4) ^& "-" ^&_

I'm glad that everything works now!!!
0
 
Da-BuCommented:
Try this...

::write script
>"%vbscript%" echo ' Temporary script for %0
>>"%vbscript%" echo NewDate = DateAdd("d", -1, NOW())
>>"%vbscript%" echo Wscript.StdOut.Write "SET yesterdayFilename=ex" ^&_
>>"%vbscript%" echo right( DatePart("yyyy", NewDate) , 2)  ^&_
>>"%vbscript%" echo right("-")
>>"%vbscript%" echo right( "0" ^& DatePart("m", NewDate) , 2)  ^&_
>>"%vbscript%" echo right("-")
>>"%vbscript%" echo right( "0" ^& DatePart("d", NewDate) , 2) ^& ".log"
0
 
Da-BuCommented:
I've added two new lines into existing code that should probably work... or if that doesn't work replace lines wiht this one
>>"%vbscript%" echo right "-"
0
 
PCMENIACAuthor Commented:
Thanks very much for your efforts Da-Bu, but unfortunately the code didnt work. It came out with the following error:" Microsoft VBScript compilation error: Cannot use parentheses when calling a sub"

But thanks anyways. I, eventually, managed to get what I need. Here is the code for the modified VBScript:

******************************************************************************************************

@echo off

:: setup variables for script
setlocal

:: script file name should not change
set vbscript=%temp%\~%~n0.vbs
:: command to run script
set vbrun=cscript /NoLogo "%vbscript%"

::write script
>"%vbscript%" echo ' Temporary script for %0
>>"%vbscript%" echo NewDate = DateAdd("d", -2, NOW())
>>"%vbscript%" echo Wscript.StdOut.Write "SET yesterdayFilename=" ^&_

>>"%vbscript%" echo right( DatePart("yyyy", NewDate) , 4) ^& "-" ^&_

>>"%vbscript%" echo right( "0" ^& DatePart("m", NewDate) , 2) ^& "-" ^&_

>>"%vbscript%" echo right( "0" ^& DatePart("d", NewDate) , 2)
:: fake pause here to let file finish writing
ping -n 1 127.0.0.1 -w 500 >NUL

:: Use  %vbrun% to get date
for /F "tokens=2 delims==/" %%K in ('%vbrun%') do (
  set yesterdayFilename=%%K)

echo %yesterdayFilename%

******************************************************************************************************
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.