0

I am trying to acquire the contents of the file with a datestamp in the file name for yesterday (WH.FBTBT20130214.csv). I created a date stamp variable for today but cannot find anything in google that tells me how to backdate my datestamp one day utilizing the same format. Here is the code that I created in my batch file:

@echo off

REM ***** 20130215 MS Define DateStamp variable *****
Set DateStamp=%date:~10,4%%date:~4,2%%date:~7,2%
Set DateStamp=%DateStamp%
REM Set DateStamp=20130212

echo DateStamp: %DateStamp%

Any help/direction would be greatly appreciated. Thank you.

Melinda
  • 1,501
  • 5
  • 25
  • 58
  • possible duplicate of [Date arithmetic in dos scripting](http://stackoverflow.com/questions/355425/date-arithmetic-in-dos-scripting) – dbenham Feb 15 '13 at 15:36
  • Another possible duplicate: http://stackoverflow.com/questions/11891335/how-to-get-yesterdays-date-in-dos. I imagine there are others. – dbenham Feb 15 '13 at 15:37

2 Answers2

0

I found the answer. Just in case anyone is interested:

Set /A DateStamp=(%date:~10,4%%date:~4,2%%date:~7,2%)-1

echo DateStamp: %DateStamp%

Melinda
  • 1,501
  • 5
  • 25
  • 58
  • 1
    That will not work. What do you think happens if today's date is January 1, 2014? 20140101 becomes 20140100, but you want 20131231. – dbenham Feb 15 '13 at 15:32
0

You can't always just subtract 1 from the date and get yesterday's date. Say it's March 1. What's yesterday? March 0? Doesn't work that way. A better way is to use vbscript's Date() object and subtract a day from it.

@echo off
setlocal
REM ***** 20130215 MS Define DateStamp variable *****
echo d = DateAdd^("d", -1, Date^(^)^)>yesterday.vbs
echo wscript.echo DatePart^("yyyy", d^) ^& "/" ^& DatePart^("m", d^) ^& "/" ^& DatePart^("d", d^)>>yesterday.vbs
for /f "tokens=1-3 delims=/" %%I in ('cscript /nologo yesterday.vbs') do (
    set Year=%%I
    if %%J LEQ 9 (set Month=0%%J) else set Month=%%J
    if %%K LEQ 9 (set Day=0%%K) else set Day=%%K
)
set DateStamp=%Year%%Month%%Day%
del /q yesterday.vbs
echo %DateStamp%
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Thanks for the info. I did not realize that was the way BATCH worked. I appreciate the correction. I'll try vbscript. Haven't worked with it in a good while. – Melinda Feb 15 '13 at 15:56