Hi,

I have a batch file to create a folder with today’s date using %date:~0,2%-%date:~3,2%-%date:~6,4%

I need a command to detect yesterday’s date but how can this be done?

I’m thinking something on the lines of:

IF EXIST c:\temp%date:~0,2%-%date:~3,2%-%date:~6,4% GOTO yes

Of course that command detects if a folder with today’s date exists…not yesterdays.

Thanks

Chris

4 Spice ups

I’m 99% certain you can’t do that in pure batch file.

I could send you an .EXE file but then I’d be putting you in the position of accepting an executable file from some random guy on the web.

Do you have acces to any compilers ?

I could then ship you the source code which you could verify as safe.

It may be that you should use the at command…

It looks like you have some daily task to execute and you could schedule this using at

Edit:

if date==29/05/2012 set day=28/05/2012
if date==30/05/2012 set day=29/05/2012
if date==31/05/2012 set day=30/05/2012
if date==01/06/2012 set day=31/05/2012
if date==02/06/2012 set day=01/06/2012
if date==03/06/2012 set day=02/06/2012
if date==04/06/2012 set day=03/06/2012
if date==05/06/2012 set day=04/06/2012
if date==06/06/2012 set day=05/06/2012
if date==07/06/2012 set day=06/06/2012
if date==08/06/2012 set day=07/06/2012
if date==09/06/2012 set day=08/06/2012
if date==10/06/2012 set day=09/06/2012
if date==11/06/2012 set day=10/06/2012
if date==12/06/2012 set day=11/06/2012
if date==13/06/2012 set day=12/06/2012
if date==14/06/2012 set day=13/06/2012
if date==15/06/2012 set day=14/06/2012
if date==16/06/2012 set day=15/06/2012
if date==17/06/2012 set day=16/06/2012
if date==18/06/2012 set day=17/06/2012
if date==19/06/2012 set day=18/06/2012
if date==20/06/2012 set day=19/06/2012
if date==21/06/2012 set day=20/06/2012
if date==22/06/2012 set day=21/06/2012
if date==23/06/2012 set day=22/06/2012
if date==24/06/2012 set day=23/06/2012
if date==25/06/2012 set day=24/06/2012
if date==26/06/2012 set day=25/06/2012
if date==27/06/2012 set day=26/06/2012
if date==28/06/2012 set day=27/06/2012
if date==29/06/2012 set day=28/06/2012
if date==30/06/2012 set day=29/06/2012
if date==01/07/2012 set day=30/06/2012
if date==02/07/2012 set day=01/07/2012
if date==03/07/2012 set day=02/07/2012
if date==04/07/2012 set day=03/07/2012
if date==05/07/2012 set day=04/07/2012
if date==06/07/2012 set day=05/07/2012
if date==07/07/2012 set day=06/07/2012
if date==08/07/2012 set day=07/07/2012
if date==09/07/2012 set day=08/07/2012
if date==10/07/2012 set day=09/07/2012
if date==11/07/2012 set day=10/07/2012
if date==12/07/2012 set day=11/07/2012
if date==13/07/2012 set day=12/07/2012
if date==14/07/2012 set day=13/07/2012
if date==15/07/2012 set day=14/07/2012
if date==16/07/2012 set day=15/07/2012
if date==17/07/2012 set day=16/07/2012
if date==18/07/2012 set day=17/07/2012
if date==19/07/2012 set day=18/07/2012
if date==20/07/2012 set day=19/07/2012
if date==21/07/2012 set day=20/07/2012
if date==22/07/2012 set day=21/07/2012
if date==23/07/2012 set day=22/07/2012
if date==24/07/2012 set day=23/07/2012
if date==25/07/2012 set day=24/07/2012
if date==26/07/2012 set day=25/07/2012
if date==27/07/2012 set day=26/07/2012

Thanks Dominic,

Yes it will be a batch file that runs everyday and checks for a filename (sorry, I said folder originally!) with yesterday’s date. If that file exists it uses the GOTO command to then send a specific email via another command

Apologies if I am being thick here but couldn’t you declare a variable as Date-1 in some way?

Yes, I was thinking that…but I’m not 100% on the usage of the code. Then I thought, if it’s 01-01-2012 yesterday’s date would be 30-04-2012 so the DAY, MONTH and YEAR would need to be checked as all three would change to a previous day, month, year.

I can send you the spreadsheet that generated that ugly solution.

Thanks!

It’ll get virus checked so don’t worry about a secure risk :slight_smile:

Thanks

Chris

As you are running this everyday, then when you create the file for today why not also pipe the current date to yesterday.txt then when you need the date tommorrow you can pult it with a for command similar to below

for /f %%y in (yesterday.txt) do if exist %%y goto yes

the forfiles command may also do what you need but that works on the files modified date rather then the filename.

see Forfiles - Batch process multiple files - Windows CMD - SS64.com

Seems sensible Ross!

How do you ‘pipe’ to a .txt file?

echo %date:~0,2%-%date:~3,2%-%date:~6,4% > yesterday.txt

the single ‘>’ will overwrite the contents

Hi Ross,

That works for me. I’m trying to understand your original post though:

for /f %%y in (yesterday.txt) do if exist %%y goto yes

So, I’d need to check for c:\temp\29-05-2012.zip and if that files exists GOTO the line :yes

How would your code look?

Thanks

chris

Incidentally I’ve manipulated your other post as:

echo %date:~0,2%-%date:~3,2%-%date:~6,4%.zip > c:\yesterday.txt

Chris

Batch file as follows, you may need to alter the program flow depending on exactly what you want to do.

:start
REM Do the check on yesterdays file
REM reads the content of yesterday.txt and store it in varible &&y
REM checks for the file named %%y and if it exists jump to lable :yes
for /f %%y in (yesterday.txt) do if exist %%y goto yes

:today
REM Create todays file
commands for whatever it is you want to do here that creates %date:~0,2%-%date:~3,2%-%date:~6,4%.zip

REM Create yesterday.txt with todays file name to check tomorrow
echo %date:~0,2%-%date:~3,2%-%date:~6,4%.zip > yesterday.txt
:goto end

:yes
commands for whatever it is you want to do here on yesterdays file

REM jump back to label today to create file etc.
goto today
:end

Chris3966 wrote:

Yes, I was thinking that…but I’m not 100% on the usage of the code. Then I thought, if it’s 01-01-2012 yesterday’s date would be 30-04-2012 so the DAY, MONTH and YEAR would need to be checked as all three would change to a previous day, month, year.

Ah, good point - although perhaps there is a smart way to rewind a single day from the Date variable but if there is, I wouldn’t know.

I like Ross’s idea of piping todays date into a file “Yesterday” and using it tomorrow, kinda reminds me of the lines in a Marillion song called Ricochet - “Yesterday starts tomorrow, tomorrow starts today”!

Hi Mate

Have a look here DOS Batch - Date and Time

second script is something very similar to what you looking for.

Hope this helps,

Is it possible to use PowerShell? PowerShell makes getting yesterday’s date very simple.

$yesterday = (get-date).adddays(-1)

No need to worry about a new month or year or even leap year.

if today is 1/1/2012, $yesterday will be 12/31/2011 or if today is 3/1/2012 $yesterday will be 2/29/2012.

Hi all,

Thanks to all for the input. Ross’s response sorted it so many thanks!

Cheers

Chris

This guy seems to have done extensive work on the subject:

http://www.robvanderwoude.com/datetiment.php#Yesterday