Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

Automatically include the previous date in a batch file?


(!)

mujeeb's Avatar
mujeeb mujeeb is offline
Computer Specs
Junior Member with 7 posts.
THREAD STARTER
 
Join Date: Dec 2011
Experience: Intermediate
02-Dec-2011, 08:52 AM #1
Unhappy Automatically include the previous date in a batch file?
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's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
02-Dec-2011, 09:00 AM #2
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's Avatar
mujeeb mujeeb is offline
Computer Specs
Junior Member with 7 posts.
THREAD STARTER
 
Join Date: Dec 2011
Experience: Intermediate
02-Dec-2011, 09:23 AM #3
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's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
02-Dec-2011, 09:46 AM #4
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's Avatar
mujeeb mujeeb is offline
Computer Specs
Junior Member with 7 posts.
THREAD STARTER
 
Join Date: Dec 2011
Experience: Intermediate
02-Dec-2011, 10:19 AM #5
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's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
02-Dec-2011, 10:29 AM #6
You gotta at least try!
You can see where the day is being set in the For Loop. Edit that Set statement.
mujeeb's Avatar
mujeeb mujeeb is offline
Computer Specs
Junior Member with 7 posts.
THREAD STARTER
 
Join Date: Dec 2011
Experience: Intermediate
02-Dec-2011, 12:27 PM #7
Hi,
I tried but no luck
Squashman's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
02-Dec-2011, 12:48 PM #8
I like to teach people to fish.
Show me what you tried to add the Zero to the beginning of the day.
Squashman's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
02-Dec-2011, 01:37 PM #9
I will give you a hint. Change this line of code.

Code:
set day=%%b
mujeeb's Avatar
mujeeb mujeeb is offline
Computer Specs
Junior Member with 7 posts.
THREAD STARTER
 
Join Date: Dec 2011
Experience: Intermediate
03-Dec-2011, 05:53 AM #10
I set as below and its working fine... Thank You verymuch...

set day="0"%%b
Squashman's Avatar
Trusted Advisor with 19,648 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
03-Dec-2011, 12:43 PM #11
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.

Last edited by Squashman; 03-Dec-2011 at 01:37 PM..
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑