![]() |
|
19 Jan 2015 | #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 System Specs![]() |
. |
|
19 Jan 2015 | #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 System Specs![]() |
19 Jan 2015 | #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% 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" |
My System Specs![]() |
. |
|
20 Jan 2015 | #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 System Specs![]() |
20 Jan 2015 | #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: But you asked for... Code:
XCOPY C:\Test D:\Test /d:01/21/2015 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% 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 |
|
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 |
|
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![]() |
![]() |
Thread Tools | |
Similar help and support threads | ||||
Thread | Forum | |||
Navigation Pane - Automatically expand to current folder How to Turn On or Off Expand to Current Folder in Navigation Pane in Windows 7 This will show you how to have automatically expand to current folder turned on or off in the Windows 7 Navigation Pane folder tree while you are navigating in the main Windows Explorer pane. If you open a... |
Tutorials | |||
IE 11 Automatically Closes After Current Security Update As many people heard recently there was a new exploit which targeted Internet Explorer and Adobe Flash Player, Microsoft put a rush on a new security update to fix the issue. I went to Automatic Updates and it wasn't on the list so I proceeded to download it manually. I installed the security... |
Browsers & Mail | |||
How to create a current date folder using batch file? Hi I am using following batch file for cleaning the desktop and also for taking backup of desktop: @echo off set curr_date=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2% mkdir "%USERPROFILE%\My Documents\backup\%curr_date%_backup" |
General Discussion | |||
Current date on Windows MonthCalendar not bolding in Win 7 OS Gregarious greetings, I work as a .NET Developer. I work on a Windows 7 PC that has Virtual PC/Windows XP mode. My C# code in the Visual Studio 2005 Win app on the Virtual PC successfully bolds dates on a System.Windows.Forms.MonthCalendar control based on certain logic that pertains to... |
General Discussion | |||
Automatically Expand to Current Folder: half-finished? For starters, let me state that: 1. I've searched all over Win7 forums on this site and others, and haven't found a solution. 2. I've seen the tutorials on this site. I know how to access the options. So please spare me any FAQish advice and basic tutorial links. ;) What I'd like to... |
General Discussion |
Our Sites |
Site Links |
About Us |
Find 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 20:23. |
![]() ![]() ![]() ![]() ![]() |