Wednesday, August 10, 2011

Removing Unnecessary Dots and Slashes from Date

When you want to use the current date as a part of a file's name, you want to see something like '01012011'. Usual 'date' variable gives you human-readable date with additional dots looking like this: '01.01.2011'. Or something like this: '01/01/2011'. It depends on the settings of your copy of Windows. Fortunately, it is not hard to remove unnecessary characters from the string with the date. All you need to do is to use the following construction:
echo %date:.=%
This code will remove dots from the string with the current date. Can you consider the string that allows you to remove slashes?..

4 comments:

  1. set d1=%date:~0,2%
    set d2=%date:~3,2%
    set d3=%date:~6,4%

    SET DATE2=%d1%_%D2%_%D3%

    ReplyDelete
  2. Even easier....

    set DATE2=%date:~0,2%_%date:~3,2%_%date:~6,4%

    ReplyDelete
  3. But it does not simply need to search for the slash to remove instead of dot?, like:
    echo %date:/=%
    in win7 and xp works.

    ReplyDelete
  4. echo %date:/=% Worked for me.

    --admin @ od46.com

    ReplyDelete

Translate