#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Posts
    5
    Rep Power
    0

    Date format windows shell script


    Hello, I wrote a small batch file that will print today and yesterday date to be used in anther application. problem is with yesterday date, if the date is between 1 to 9 it will print it with out "0", i mean like "2008119" instead of printing it as "20081109".

    this is the script:

    Code:
    @echo off
    
    REM Set Date format
    FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
    FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
    FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
    FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
    SET date=%yyyy%%mm%%dd%
    echo today is %date%
    
    REM define yesterday date
    set /A dd=%dd%-1
    set Pdate=%yyyy%%mm%%dd%
    echo yesterday was %Pdate%
    
    exit
    any idea ?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Posts
    5
    Rep Power
    0
    for work around i used if-else:


    Code:
    @echo off
    
    REM Set Date format
    FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
    FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
    FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
    FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
    SET date=%yyyy%%mm%%dd%
    echo today is %date%
    
    REM define yesterday date
    set /A dd=%dd%-1
    
    IF %dd% LSS 10 (
    
    		set Pdate=%yyyy%%mm%0%dd%
    
    	) ELSE (
    		set Pdate=%yyyy%%mm%%dd% 
    		
    	)
    	
    	echo yesterday was %Pdate%
    
    exit

IMN logo majestic logo threadwatch logo seochat tools logo