Automatically include the previous date in a batch file?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

mujeeb

Thread Starter
Joined
Dec 2, 2011
Messages
7
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

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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.
Code:
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.
Code:
set d=%ydate1:~2,2%
 

mujeeb

Thread Starter
Joined
Dec 2, 2011
Messages
7
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

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
I will go one step further but let you add the zero.
Code:
@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

Thread Starter
Joined
Dec 2, 2011
Messages
7
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

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
You gotta at least try!
You can see where the day is being set in the For Loop. Edit that Set statement.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
I like to teach people to fish.
Show me what you tried to add the Zero to the beginning of the day.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
I will give you a hint. Change this line of code.

Code:
set day=%%b
 

mujeeb

Thread Starter
Joined
Dec 2, 2011
Messages
7
I set as below and its working fine... :) Thank You verymuch...

set day="0"%%b
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top