Solved

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

Posted on 2004-03-30
4
1,951 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now