none
Output from same script on two different platforms produce different outputs. RRS feed

  • Question

  • I'm running a command prompt script on two different platforms (Windows 7 desktop and a Microsoft Server 2008 R2 Enterprise platform) and the output the script produces is different on each platform.  Specifically; the script is

           For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
           For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
           echo %mydate%>> log.txt
           echo %mytime%>> log.txt

    and the output from the Windows 7 desktop is:
           2014-07-31
           0249 PM

    While the output from the 2008 server is:
           ECHO is on.
           0249 PM

    Thursday, July 31, 2014 8:33 PM

Answers

  • The best answer is not to rely on string parsing in cmd.exe and instead use PowerShell scripting. PowerShell uses .NET, which has a DateTime object that is not subject to string parsing limitations.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by DaveFrag224 Friday, August 1, 2014 7:37 PM
    Thursday, July 31, 2014 8:52 PM
    Moderator

All replies

  • The best answer is not to rely on string parsing in cmd.exe and instead use PowerShell scripting. PowerShell uses .NET, which has a DateTime object that is not subject to string parsing limitations.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by DaveFrag224 Friday, August 1, 2014 7:37 PM
    Thursday, July 31, 2014 8:52 PM
    Moderator
  • You should be using %date% and %time% and not the date command.

    echo %date% %time%

    You can very easily string parse these as the output is the same on all systems as long as the culture is the same.  The date command output can vary from OS to OS.


    ¯\_(ツ)_/¯

    Thursday, July 31, 2014 9:11 PM
  • Hello Bill -

    I went with your PowerShell suggestion and the script appears to be running as expected.  Thanks.

    "for /f %%a in ('powershell -Command "Get-Date -format yyyy-MM-dd__HHmm"') do set date"


    Friday, August 1, 2014 7:37 PM
  • No, actually what I was trying to say is to write the whole script in PowerShell.

    What you're doing there is getting a date and converting it to a string. The whole idea is not to use strings at all.


    -- Bill Stewart [Bill_Stewart]

    Friday, August 1, 2014 8:03 PM
    Moderator
  • --- works on all platforms with same culture and locale.

    echo %date:~10,4%-%date:~4,2% %date:~7,2%__%time:~0,2%%time:~3,2%


    ¯\_(ツ)_/¯


    • Edited by jrv Friday, August 1, 2014 8:34 PM
    Friday, August 1, 2014 8:34 PM
  • Bill -

    Sorry I misunderstood your suggestion.  Since I'm not familiar with coding in PowerShell, can you suggest any sites that may help me?

    Thanks.

    - Dave

    Monday, August 4, 2014 3:51 PM
  • Hi Dave,

    There's some very good learning resources here:

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    Also, there's a script repository full of examples you can learn from and tweak to meet your needs here:

    http://gallery.technet.microsoft.com/scriptcenter

    As an additional suggestion, I'd highly recommend upgrading PowerShell on Win7 and WS2008R2 to v4. v2 was okay, but v4 makes life much easier (make sure you read the system requirements first though, there's still a few incompatibilities):

    http://www.microsoft.com/en-us/download/details.aspx?id=40855


    Don't retire TechNet! - (Don't give up yet - 12,950+ strong and growing)

    Monday, August 4, 2014 3:57 PM