1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Automatically include the previous date in a batch file?

Discussion in 'DOS/Other' started by mujeeb, Dec 2, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. mujeeb

    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...
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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%
     
  3. mujeeb

    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?
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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
     
  5. mujeeb

    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...
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    You gotta at least try!
    You can see where the day is being set in the For Loop. Edit that Set statement.
     
  7. mujeeb

    mujeeb Thread Starter

    Joined:
    Dec 2, 2011
    Messages:
    7
    Hi,
    I tried but no luck :(
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    I like to teach people to fish.
    Show me what you tried to add the Zero to the beginning of the day.
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    I will give you a hint. Change this line of code.

    Code:
    set day=%%b
     
  10. mujeeb

    mujeeb Thread Starter

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

    set day="0"%%b
     
  11. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,731
    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.
     
  12. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1029325