Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AIX  NFS  question 5 108
PXE question 7 85
PERL - Find newest folder 12 149
URL to download Windows 10 Home 2 156
I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
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…
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.

809 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