Solved

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

Posted on 2009-07-10
12
1,100 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

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…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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