Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-10
12
Medium Priority
?
1,106 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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