Solved

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

Posted on 2004-03-30
4
1,953 Views
Last Modified: 2008-01-09
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

0
Comment
Question by:PCMENIAC
  • 3
4 Comments
 
LVL 4

Expert Comment

by:Da-Bu
ID: 10712513
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
 
LVL 4

Expert Comment

by:Da-Bu
ID: 10712815
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
 

Author Comment

by:PCMENIAC
ID: 10713336
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
 
LVL 4

Accepted Solution

by:
Da-Bu earned 500 total points
ID: 10713637
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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Occasionally Windows/Microsoft Updates will fail to update. We have found a code that will delete all temporary files and re-register all dll's related to Windows/Microsoft Updates! This works 99% of the time to get the updates working again! The…
Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question