Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Dec 2009
    Location
    Seattle, WA, USA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Batch File behavior in windows 8

    In Windows 7, the following works:

    @echo off
    @For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C


    @echo off
    ::set date var
    Set D=%%C%%A%%B
    )

    robocopy c:\users\bill f:\backup\%D%\


    I use this to set a directory name of the date I run the script for backups. ie: f:\backup\20150401

    However, in windows 8 and 8.1, it does not work. I don't have a Win 8 machine at the moment, so I can't troubleshoot. I tried to use this on a friends computer to provide an easy to retrieve backup process for him, but, the failure of the variable in the script ends up with f:\backup\username, not what I wanted to do.

    Any suggestions?

    Thanks!

    Bill

  2. #2
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,472
    Thanks
    34
    Thanked 197 Times in 177 Posts
    I don't have a Windows 8 machine either, but just looking at the BATch file I suspect that
    DATE /T
    doesn't produce what it used to do in Windows 7 - in your locale.
    Microsoft occasionally does this, to show that standards aren't all they're cracked up to be!

    Furthermore, I think you need to use REM instead of :: for comments within FOR loops.

    I have used the following for constructing the useful year, month and day variables:
    Code:
    @echo off
    :: obtain current DATE regardless of whether or not the operating system
    ::   gives the Day-of-the-Week as the first token from DATE /T
    ::   (works on NT4/W2K/WXP/W7/W10 to set variables %yy% (four digits), %mm% and %dd%)
    ::     by Ritchie Lawrence, modified by Garry Deane
    
    if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
    for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo.^|date') do (
      for /f "tokens=%toks% delims=.-/ "  %%e in ('date /t')     do (
        (set %%a=%%e) & (set %%b=%%f) & (set %%c=%%g)
      )
    )
    if 1%yy% LSS 200 set yy=20%yy%
    echo Date is: %dd%-%mm%-%yy%
    You can see that it's worked since before the Millennium!
    BATcher

    "The trouble with quotes on the internet is that you can never know if they are genuine."
    Abraham Lincoln
    

  3. #3
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,518
    Thanks
    54
    Thanked 1,038 Times in 966 Posts
    Here are a couple of responses from W8.1, exactly the same as W7 AFAICT.

    C:\Users\Paul>date
    The current date is: 02/04/2015
    Enter the new date: (dd-mm-yy)

    C:\Users\Paul>date /t
    02/04/2015

    The batch file works if you use this: tokens=1,2,3
    It should be the same in Windows 7 unless you have a white space at the beginning of the date response - I get one at the end, after year.

    cheers, Paul

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •