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.

Solved: Date format in bat file

Discussion in 'DOS/Other' started by bikey33, Jun 21, 2009.

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

    bikey33 Thread Starter

    Joined:
    Jun 20, 2009
    Messages:
    3
    Hi,
    I need to have a date reformatted so that I can see if a file is in a directory for the current date.

    When I run the following:
    @echo off > %temp%\# & setLocal EnableDelayedExpansion
    echo %date%

    I get this:
    2009-06-20

    I need it to be 2009/06/20

    Once i have that I know how to do the rest. I have searched and tried many examples that parse the date out to reformat and nothing seems to work, so I am back to square one.

    Your help would be tremendously appreciated!

    Thanks
     
  2. devil_himself

    devil_himself

    Joined:
    Apr 7, 2007
    Messages:
    4,910
    Code:
    @echo off
    for /f "tokens=1-3 delims=./-" %%f in ("%date%") do (
        set _today=%%f/%%g/%%h
        echo %_today%
     )
    
     
  3. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    If the delimiter will always be a -:
    Code:
    Set _Today=%date:-=/%
    Echo %_Today%
    If you want it to be 20090620, use this:
    Code:
    Set _Today=%date:-=%
    Echo %_Today%
    Jerry
     
  4. bikey33

    bikey33 Thread Starter

    Joined:
    Jun 20, 2009
    Messages:
    3
    Hi there,
    That worked great! I just needed to put echo %_today%
    outsided of the parenthesis instead of in. Thank you so much. Now I have another problem, as when I tried to implement it on the server (I was using a test one), the current date comes up as:
    The current date is: Sun 06/21/2009.

    Is there a way to get rid of the SUN? I can change other variables around (set _today=%%h-%%f-%%g) easily thanks to you :), but don't know what to do about that Day variable.

    Thanks,
    Bikey33
     
  5. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    Code:
    @Echo Off[COLOR=DarkRed]
    If "%Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)[/COLOR]
    For /F "Tokens=%_NumTok% Delims=./[COLOR=Red]-[/COLOR] [COLOR=Red]"[/COLOR] %%f In ("%date%") Do Set _Today=%%f/%%g/%%h
    )
    Echo %_Today%
    Added the line in Red and also added a space after Delims.

    This will get the date regarless of your Regional settings, and sets the _fdate variable to YYYYMMDD. You can then extract the year, month, and day from _fdate and put it together anyway you want:
    Code:
    @Echo Off
    Set _Date=%date%
    If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
    :: Default Delimiter of TAB and Space are used
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
    For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
    IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
    IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
    IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
    Set _Today=%_fdate:~4,2%-%_fdate:~6,2%-%_fdate:~0,4%
    Echo %_Today%
    HTH

    Jerry
     
  6. ghostdog74

    ghostdog74

    Joined:
    Dec 7, 2005
    Messages:
    146
    to use date independent of regional settings,you can use vbscript
    Code:
    strtoday = Now
    yr = Year(strtoday)
    mth = Month(strtoday)
    dy = Day(strtoday)
    If Len(mth) <2 Then 
    	mth="0"&mth
    End If
    If Len(dy) <2 Then 
    	dy="0"&dy
    End If
    thedate = yr &"-"&mth&"-"&dy
    WScript.Echo thedate
    
    save the above as myscript.vbs and in your batch
    Code:
    for /f ..... ( 'cscript /nologo myscript.vbs' ) do (
      .....
    )
    
    (Or you can do your task entirely in vbscript)
     
  7. bikey33

    bikey33 Thread Starter

    Joined:
    Jun 20, 2009
    Messages:
    3
    Hi, Sorry it took so long to reply. Had to focus on other issues and could not test this right away. I used the resolution from The Outcast and it worked great. Thank you so much!
     
  8. TheOutcaste

    TheOutcaste

    Joined:
    Aug 7, 2007
    Messages:
    9,028
    You're Welcome!

    If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) :)

    [​IMG]

    Jerry
     
  9. 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!

Thread Status:
Not open for further replies.

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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice