Solved

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

Posted on 2009-07-10
12
1,090 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
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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 11

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now