How to get the current date automatically?


  1. Posts : 3
    64 bit
       #1

    How to get the current date automatically?


    Hi All,

    I have the following Copy Command which copies files from one location to another

    XCOPY D:\Test C:\Test /i

    Now i would like to have the files copied from D to C everyday based on the date for today, i googled around and found this one which is working fine

    XCOPY D:\Test C:\Test /D:01-19-2015.

    The thing here is i want the date to be automated as i would like to schedule a job for this one which would copy the files daily automated .

    Can someone please help me with this?

    PLease let me know if you have any questions.

    Thanks
      My Computer


  2. Posts : 5
    w7x64
       #2

    Hi sujith

    try this
    xcopy "D:\Test" "C:\Test %DATE:~7,2% %DATE:~4,2% %DATE:~-4%" /y /i /h /s /e

    /s /e - recursive copy, including copying empty directories.
    /h - copy system and hidden files.
    /y - don't prompt before overwriting existing files.
      My Computer


  3. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #3

    Hello Sujith,

    In batch scripts there is a predefined environmental variable that expands to the current date. It is %DATE%.

    The variable includes an abbreviated weekday name at the beginning, so some string slicing needs to take place before implementing the variable into your command.
    Code:
    Xcopy D:\Test C:\Test /d:%DATE:~4%
    EDIT: The above will only work if your locale is set to display dates in the form of MM/DD/YYYY

    An alternate way of doing things, that does not require the %DATE% variable, would be to loop off the results of Forfiles.
    Code:
    Forfiles /p D:\Test /d 0 /c "XCOPY @PATH C:\Test"

    Now, to schedule a batch file--or any application/executable really--you'd use Task Scheduler.

    The following is a command that will create your described task to be scheduled for you. Execute the command in an elevated command prompt.
    Code:
    At 00:00 /every:M,T,W,TU,F,S,SU "C:\path\to\batch\script.bat"
    This will run the batch program for every day of the week at midnight.
    Last edited by Pyprohly; 20 Jan 2015 at 20:23.
      My Computer


  4. Posts : 3
    64 bit
    Thread Starter
       #4

    Hi Guys,

    Thanks a lot for your reply, I made it work using the following code

    set curr_date=%DATE:~4,2%/%DATE:~7,2%/%DATE:~10,4% & XCOPY C:\Test D:\Test /d:%curr_date%
      My Computer


  5. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #5

    Um... Have you tested that, Sujith? You might find it does not do what you want. In batch, if you're going to assign a variable and use the it immediately on the same line, you have to enable delayed expansion, or the variable will expand to an empty string.

    So the actual Xcopy command that you are triggering each time is:
    Code:
    XCOPY C:\Test D:\Test /d:
    Which means to append files from C:\Test to D:\Test, only if the source file is newer than than the destination file.
     
    But you asked for...
    Code:
    XCOPY C:\Test D:\Test /d:01/21/2015
    (The date should vary and be the current date^^) Which says to copy files changed on or after 01/21/2015 (the current date etc.).



    Sujith, to avoid an empty string value for "/d:", you should either put both of your commands on two separate lines:
    Code:
    set curr_date=%DATE:~4,2%/%DATE:~7,2%/%DATE:~10,4%
    XCOPY C:\Test D:\Test /d:%curr_date%
    Or enable and use delayed expansion, like as follows:
    Code:
    setlocal EnableDelayedExpansion
    set curr_date=%DATE:~4,2%/%DATE:~7,2%/%DATE:~10,4% & XCOPY C:\Test D:\Test /d:!curr_date!

    But because you are situated in the USA (where dates are in the form of MM/DD/YYYY), Sujith, I suggest simply using:
    Code:
    XCOPY D:\Test C:\Test /d:%DATE:~4%
      My Computer


  6. Posts : 3
    64 bit
    Thread Starter
       #6

    Hi Pyprohly,

    Thanks a lot for taking your valuable time and replying to me...i was using my old method in the batch file it was running fine but i was using it in some sql job and it was failing for no reason, but now i changed the code of my batch file to be using this in the end

    /d:%DATE:~4%


    and even my job is also running fine, Thanks a lot for your help.
      My Computer


  7. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #7

    You're most welcome.

    And speaking of welcome, welcome to SF. Be sure to make a visit should you need any assistance at all (regarding Windows that is ).
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 06:58.
Find Us