Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. IT & Infrastructure
  4. Formatting date from bat file

Formatting date from bat file

Scheduled Pinned Locked Moved IT & Infrastructure
question
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dybs
    wrote on last edited by
    #1

    I haven't had much experience working with batch files, so hopefully this will be a simple solution. I'm trying to get parse the output of the Windows date command from the mm/dd/yyyy format to the ddMMMyyyy format (i.e. 01/13/2010 to 13Jan2010). I'm attempting to do this with the following batch file:

    FOR /F "tokens=2,3,4 delims=/ " %%D IN ('date /T') DO (
    IF %%D == 01 (
    set month=JAN
    ) ELSE IF %%D == 02 (
    set month=FEB
    ) ELSE IF %%D == 03 (
    set month=MAR
    ) ELSE IF %%D == 04 (
    set month=APR
    ) ELSE IF %%D == 05 (
    set month=MAY
    ) ELSE IF %%D == 06 (
    set month=JUN
    ) ELSE IF %%D == 07 (
    set month=JUL
    ) ELSE IF %%D == 08 (
    set month=AUG
    ) ELSE IF %%D == 09 (
    set month=SEP
    ) ELSE IF %%D == 10 (
    set month=OCT
    ) ELSE IF %%D == 11 (
    set month=NOV
    ) ELSE IF %%D == 12 (
    set month=DEC
    )
    echo %%E%month%%%F
    )

    Usually this gives me an empty string for the %month% variable and I the last echo gives me 132010. However, any of the following changes can get it to work for a little while:

    IF %%D == "01" (

    or

    echo %%D
    IF %%D == 01 (

    or

    echo %%D
    IF %%D == "01" (

    but I can re-run the bat file with these changes later, and then it gives me 132010 instead of 13Jan2010 again! I've tried this in XP, Vista, and Windows PE (where it will actually be used), and I get the same inconsistent results. What am I doing wrong? Thanks, Dybs

    The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

    L 1 Reply Last reply
    0
    • D dybs

      I haven't had much experience working with batch files, so hopefully this will be a simple solution. I'm trying to get parse the output of the Windows date command from the mm/dd/yyyy format to the ddMMMyyyy format (i.e. 01/13/2010 to 13Jan2010). I'm attempting to do this with the following batch file:

      FOR /F "tokens=2,3,4 delims=/ " %%D IN ('date /T') DO (
      IF %%D == 01 (
      set month=JAN
      ) ELSE IF %%D == 02 (
      set month=FEB
      ) ELSE IF %%D == 03 (
      set month=MAR
      ) ELSE IF %%D == 04 (
      set month=APR
      ) ELSE IF %%D == 05 (
      set month=MAY
      ) ELSE IF %%D == 06 (
      set month=JUN
      ) ELSE IF %%D == 07 (
      set month=JUL
      ) ELSE IF %%D == 08 (
      set month=AUG
      ) ELSE IF %%D == 09 (
      set month=SEP
      ) ELSE IF %%D == 10 (
      set month=OCT
      ) ELSE IF %%D == 11 (
      set month=NOV
      ) ELSE IF %%D == 12 (
      set month=DEC
      )
      echo %%E%month%%%F
      )

      Usually this gives me an empty string for the %month% variable and I the last echo gives me 132010. However, any of the following changes can get it to work for a little while:

      IF %%D == "01" (

      or

      echo %%D
      IF %%D == 01 (

      or

      echo %%D
      IF %%D == "01" (

      but I can re-run the bat file with these changes later, and then it gives me 132010 instead of 13Jan2010 again! I've tried this in XP, Vista, and Windows PE (where it will actually be used), and I get the same inconsistent results. What am I doing wrong? Thanks, Dybs

      The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It might be misinterpreted, as the set command gladly accepts a space as a last part of a variable-name. DOS might interpret the request like this;

      IF "%%D "=="01"(

      How about something like the batch below?

      set d=%Date:~3,2%
      if "%d%" == "01" (
      set month=JAN
      )
      else if "%d%" == "02" (
      set month=FEB
      )
      ..
      echo %month%

      I are Troll :suss:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups