Windows 7 Forums

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
 
 
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 SpecsSystem Spec
.
19 Jan 2015   #2
Bugija

w7x64
 
 

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 SpecsSystem Spec
19 Jan 2015   #3
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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 SpecsSystem Spec
.

20 Jan 2015   #4
sujith123

64 bit
 
 

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 SpecsSystem Spec
20 Jan 2015   #5
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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 SpecsSystem Spec
21 Jan 2015   #6
sujith123

64 bit
 
 

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 SpecsSystem Spec
21 Jan 2015   #7
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

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 SpecsSystem Spec
Reply

 How to get the current date automatically?




Thread Tools Search this Thread
Search this Thread:

Advanced Search




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
Won't automatically add 'Date modified' to downloads
Hi! First off, I kinda suck with computers, so I'm sorry if I make this more difficult than it should be. Also, this is kind of a silly question. When I used to download images, my computer would automatically add a 'date modified' thingy to it, so that I could sort through them easily by setting...
General Discussion
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 15:26.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App