XCOPY yesterday date parameter

October 13, 2011 at 13:20:59
Specs: Windows 7
I need to copy everyday automatically, the files that were created or changed yesterday into a new folder(no syncronization only copy), I am trying with XCOPY but in the Date parameter do not accept variables.
This is the script.

PowerShell $date = Get-Date; $date=$date.AddDays(-1); $date.ToString('MM-dd-yyyy')
xcopy C:\test C:\copy /D:$date /E

Any Idea?


See More: XCOPY yesterday date parameter

Report •

#1
October 13, 2011 at 13:47:08
Do you want the files that were modified only yesterday, or do you want all files that were modified since the script was last run?

How To Ask Questions The Smart Way


Report •

#2
October 13, 2011 at 13:51:40
I think if you change the last part of the first line to:

$date=$date.ToString('MM-dd-yyyy')

it will work better. Note however that this form of the xcopy command will also copy any files created today.


Report •

#3
October 13, 2011 at 14:38:21
The idea is to run the script every day, in the early morning( 1 am), so, I only should need news and changed files from yesterday. Anyway explain me what do you have in mind.

Report •

Related Solutions

#4
October 13, 2011 at 14:44:15
Thanks, but I need the files from yesterday, not only today files.

C:\>PowerShell $date = Get-Date; $date=$date.AddDays(-1); $date.ToString('MM-dd-
yyyy')
10-12-2011


C:\>xcopy C:\prueba C:\copias /D:'' /E
Invalid parameter - /D:''

C:\>pause
Press any key to continue . . .

The problem is when I use a variable in the parameter D:


Report •

#5
October 13, 2011 at 15:02:42
If you just want all modified files, XCOPY has a switch for that:
xcopy C:\test C:\copy /M /E

This also answers the important question of, "What do we do if the script didn't run last night?"

EDIT: Oh, you can't mix PowerShell and batch in that way. The $date variable is destroyed when powershell closes (at the end of the line), and CMD doesn't recognize PowerShell's variable syntax.

How To Ask Questions The Smart Way


Report •

#6
October 13, 2011 at 23:04:52
The $date variable is in the wrong format. The $date.ToDate() statement does nothing unless you assign the result to a variable. Try my original suggestion.

Report •

#7
October 14, 2011 at 07:02:48
Thank you guys, for clarification about powershell.
So, I only would need the script to get the date of yesterday.
Razor2.3, in this case maybe is not possible to use Archive parameter, let me explain you guys what is my problem, I have a folder in Texas with 1 million of documents, and I have the Backup in FL, I mean i am not going to use XCOPY to copy the folder completely again. On the other hand Is not possible syncronize because they are in differents LANs.
My unique choice is syncronize manually, I mean, identify Which objects changed yesterday in TX,afterwards move them to FL.
Thanks guys fo your assitance.

Report •

#8
October 14, 2011 at 08:23:08
Why are you so hung up on the word "yesterday?" You already said you're going to run this in the middle of the night (1 am, to be exact). Do you have people who'll keep these files in some sort of inconsistent state?

What happens if the server goes down Tuesday at 00:15? Do you tell your users on Wednesday, "Welp, sorry. I won't back any of that stuff up because it was made Monday, and yesterday was Tuesday?" What'll you do when management decides they want the updating done faster, and tell you to run your script 4 times a day? What'll you do when management decides the copies are eating too much bandwidth, and tell you to run your script only on weekends?

This isn't a personal attack or anything, I'm legitimately trying to find the difference between "files changed" and "files changed yesterday." One seems like a fair compromise given the realities of the described situation. The other seems like a fair compromise with added liability, and I'm not seeing an advantage.

How To Ask Questions The Smart Way


Report •

#9
October 14, 2011 at 08:34:23
Thanks Razor. The difference is that to use XCOPY with /M parameter I need to copy all folder(1 million documents) to the backup folder again and I cant do that again , or How Xcopy can to know what is already copied?

Report •

#10
October 14, 2011 at 08:36:12
or I am not understanding how XCOPY with /M works?

Report •

#11
October 14, 2011 at 08:50:37
Whenever someone writes to a file, the archive attribute is turned on. Running XCOPY /M copies only files with the archive attribute set, and then turns the attribute off. You end up coping only files that have been modified since the last time you ran XCOPY /M.

The first time you run it will end up with the entire directory structure copied. This is because you've never ran a backup on those files before. (Note: You can manually turn off the attribute before the first run.) After that, it'll be every file that has been modified since the last run.

EDIT:
Turn off archive attribute for a directory branch:

attrib c:\test\* -a /s

View files with archive attribute set:

dir /aa c:\test

How To Ask Questions The Smart Way


Report •

#12
October 14, 2011 at 09:00:56
Exactly because of that I can not run a XCOPY with /M.I dont want to copy the entere folder because is very big.
and I dont want to take the Risk to turn the all atributes off.
I really appreciate wheter somebody help me with the calculate of yesterday.
Thanks in advance.

Report •

#13
October 14, 2011 at 09:08:38
That command only turns off the archive attribute, the attribute that denotes if a file needs to be backed up. What else do you use the attribute for? In stock Windows, only the backup program cares about the attribute. Are you running a different backup on this directory?

How To Ask Questions The Smart Way


Report •

#14
October 14, 2011 at 09:25:49
Yes Sir. And I have some adtional implications, because of that I dont want to touch the original live folder.

Report •

#15
October 17, 2011 at 11:12:03
I think you're, as one of my coworkers would say, setting yourself up for failure. Still, I'm not the one who would have to perform the cleanup, so whatever.

There are some silly rules involving PowerShell and external programs. Thankfully, this case gets to ignore most of them.

Take your script, remove the word "powershell", rename it from a .BAT / .CMD to a .PS1, configure PowrShell to run scripts, and you should be good.

How To Ask Questions The Smart Way


Report •

Ask Question