Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

# Windows 7: How to get the current date automatically?

 19 Jan 2015 #1 sujith123 64 bit 3 posts 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 System Specs
 .
 19 Jan 2015 #2 Bugija w7x64 2 posts 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 System Specs
 19 Jan 2015 #3 Pyprohly Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan 722 posts In a house 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. My System Specs
 .
 20 Jan 2015 #4 sujith123 64 bit 3 posts 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 System Specs
 20 Jan 2015 #5 Pyprohly Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan 722 posts In a house 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 System Specs
 21 Jan 2015 #6 sujith123 64 bit 3 posts 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 System Specs
 21 Jan 2015 #7 Pyprohly Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan 722 posts In a house 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 System Specs