Solved

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

Posted on 2004-03-30
4
1,957 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Hello I read in a discussion about a person who configured a very simple mirror RAID with two hard drives; the system and data were on the same partition. He asked how to repair the system as it was not booting up anymore. In his case running …
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.

730 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