Solved

Batch: need to generate timestamp for 'today' AND for 'yesterday'

Posted on 2009-07-10
12
1,098 Views
Last Modified: 2012-05-07
Hello,
I need to generate 2 strings that i will pass on to a program - the first is the current date:
DDMMYY
Which is easy enough.

The other one is 'yesterday' - current day minus 1 - also in the same format:

DDMMYY

Is that possible at all in a batch-script ?

hope you can help, thanks.



0
Comment
Question by:thordk
[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
12 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 24822463
One very cheaty way... especially if this is running as a scheduled script daily is to have a file stored with the current date in each time the script finishes.... next time it will contain the previous date.

i.e.

@echo off
REM Get yesterday's date from file
set /p yesterday=<yesterday.txt
set today=your way of getting that from %date% or whatever
rem do your processing using %today% and %yesterday%
echo %today% > yesterday.txt

The other easy(ish) alternative is to use a VB Script:

date() -1 returns yesterdays date so a bit of formatting and you can return that in ddmmyyyy formats:

@echo off
echo wscript.echo right(100+day(date()),2) ^& right(100+month(date()),2) ^& year(date()) ^& "," ^& right(100+day(date() -1),2) ^& right(100+month(date() -1),2) ^& year(date() -1)>> "%temp%\yesterday.vbs"
for /f "tokens=1,2 delims=," %%a in ('cscript //nologo "%temp%\yesterday.vbs"') do set today=%%a & set yesterday=%%b
echo yesterday was %yesterday% and today is %today%

That returns today and yesterday using the same VBS line with a VBS file created on the fly in the temp dir.  You can pre-create the VBS if you like and just call it, changing the path in the for command.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24822474
Sorry I see you wanted YY not YYYY.  Just need to change

year(date() -1)  to   right(year(date() -1),2)
and
year(date()) to right (year(date(),2)

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24822486
If you are happy with your current method to get ddmmyy from a real date just get the raw date with:

@echo off
echo wscript.echo date() -1 >> "%temp%\yesterday.vbs"
for /f "tokens=1" %%a in ('cscript "%temp%\yesterday.vbs"') do set yesterday=%%a

Steve
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Closing Comment

by:thordk
ID: 31602029
Thanks alot
0
 
LVL 5

Expert Comment

by:karwak
ID: 24822591
This article may help as well ;-)...

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_21316459.html

Best regards
karwak
0
 

Author Comment

by:thordk
ID: 24822595
Thanks alot steve :)

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24822643
No problem, glad it helped. Steve
0
 

Author Comment

by:thordk
ID: 24822724
Sidequestion:

Could i easily change that code to generate the "previous timestamp" to:
-1 week
and also
-1 month ?

for 3 seperate batchjobs ?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24822909
of course.   It gets less messy if the VBS already exists but you just need to use date() -7 for 1 week for instance and DateAdd("M",-1,date()) to take off a month.  Lots of other options on DateAdd if you give it a google etc.   If stuck on getting this working post and will look later

Steve
0
 
LVL 12

Expert Comment

by:Serge Fournier
ID: 24822981
Set objshe = WScript.CreateObject("WScript.Shell")

a = now-1

dat01 = year(a) & "-" & month(a) & "-" & day(a)

msgbox(dat01)

'=== to run a program 0 = hidden
a = "notepad.exe"
objshe.Run a,8, false
0
 

Author Comment

by:thordk
ID: 24823445
Steve - thanks alot that works like a charm :)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 24823482
No problem!
Steve
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 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