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
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%
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%