# Automatically include the previous date in a batch file?



## mujeeb (Dec 2, 2011)

Hi,

I added below script in my batch file but the date part gives wrong info,but both year & month giving correct...please help me to solve this problem...

echo wscript.echo ^(Date^(^)- 1^)>yesterday.vbs
for /f %%a in ('cscript //nologo yesterday.vbs') do set ydate1=%%a
del yesterday.vbs
set ydate1=%ydate1:/=%
set m=%ydate1:~0,2%
set d=%ydate1:~2,2%
set y=%ydate1:~-2,2%
set ydate2=%y%%m%%d%
echo yesterday was %ydate2%

pause

Here are the output which i got...

D:\mujeeb\software>set ydate1=12/1/2011
D:\mujeeb\software>del yesterday.vbs
D:\mujeeb\software>set ydate1=1212011
D:\mujeeb\software>set m=12
*D:\mujeeb\software>set d=12*
D:\mujeeb\software>set y=11
D:\mujeeb\software>set ydate2=111212
D:\mujeeb\software>echo yesterday was 111212
yesterday was 111212
D:\mujeeb\software>pause
Press any key to continue . . .

Here the date should have been shown 01 instead of 12...

Thanks In Advance...


----------



## Squashman (Apr 4, 2003)

Day is only 1 character as shown to you on the first set date. Your VBscript is only returning the day as 1 character. It is not providing the day to you with a leading Zero.

```
D:\mujeeb\software>set ydate1=12/1/2011
```
You then remove the Slashes so now you have a MMDYYYY format.
The day is only 1 character but your code is pulling for two characters.

```
set d=%ydate1:~2,2%
```


----------



## mujeeb (Dec 2, 2011)

Thank You For quick Reply !!!

I understand the problem now,but could you please let me know what changes i should make to provide the date part with leading Zero?


----------



## Squashman (Apr 4, 2003)

I will go one step further but let you add the zero. 

```
@Echo Off
REM Subtract x days from current date.

echo wscript.echo ^(Date^(^)- 1^)>yesterday.vbs
for /f "tokens=1,2,3 delims=/" %%a in ('cscript //nologo yesterday.vbs') do (
	set month=%%a
	set day=%%b
	set year=%%c
)
del yesterday.vbs
echo Yesterday was %month% %day% %year%
pause
```


----------



## mujeeb (Dec 2, 2011)

Dear Sir,

Could you please let me know how to set day with following Zero ?
Actually i need exactly in the below format...

YYmmdd
111202

Thank You...


----------



## Squashman (Apr 4, 2003)

You gotta at least try!
You can see where the day is being set in the For Loop. Edit that Set statement.


----------



## mujeeb (Dec 2, 2011)

Hi,
I tried but no luck


----------



## Squashman (Apr 4, 2003)

I like to teach people to fish.
Show me what you tried to add the Zero to the beginning of the day.


----------



## Squashman (Apr 4, 2003)

I will give you a hint. Change this line of code.


```
set day=%%b
```


----------



## mujeeb (Dec 2, 2011)

I set as below and its working fine...  Thank You verymuch...

set day="0"%%b


----------



## Squashman (Apr 4, 2003)

Well it will work fine until you get to a 2 digit day and then you will need to change the output of the day variable.


----------

