?
Solved

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

Posted on 2009-07-10
12
Medium Priority
?
1,102 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 2000 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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