• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Copy file based on date

cphillips

n00b
Joined
Mar 23, 2010
Messages
52
All

I have some files named for each day of the week (Monbackup.csv, Tuesbackup.csv, etc).

I want to copy Mondays file in the early hours on Tuesday morning to another server using a script - then the same for each day of the week.

Anyone know the best way to go about this? Am happy to use a batch file or PowerShell..

Thanks in advance.
 
Can you clarify; is the "date" of the file based on the name of it in its file name (i.e., your "Monbackup.csv"), or is it based on the save-date in its properties?

If its by the save-date in its properties, you can use the /mir option with Robocopy (I think if you're using XCOPY, you'd use /d (d for date) and then specify the date to use for that case by the entire option command "/d:MM-DD-YYY"). This will mirror a directory specified, copy the file to the new directory if it doesn't exist, and only overwrite the file if the source is newer than the copy.

You can do this in a batch file. You'd probably want something like:

ROBOCOPY sourcedirectory targetdirectory /option1 /option2 /option3

If you want to copy all folders and subfolders regardless of whether they're empty or not, you'd use the /e option. And to mirror and copy newest based on file property date, you'd use the /mir command. You can specify a log file output with the appropriate option. Exchange XCOPY with ROBOCOPY and /mir with /d:MM-DD-YYY as necessary.
 
In powershell it would be something like:

Code:
dir C:\Path\To\Directory\*backup.csv | ? {((Get-date)-$_.LastWriteTime).Totalhours -le 24 | copy $_ \\destination\server\path -verbose}

This is assuming the files are written on Monday less than 24 hours from the time you run the script on Tuesday and every day thereafter. There are other ways to do it. This is quick and dirty. The verbose flag is optional so you can see what is being copied. This is untested, but should work.
 
Keep in mind that "Get-date" cmdlet returns a DateTime object. Your code sample would have to be adjusted, so that it doesn't look 24 hours in the past but rather just the previous day from midnight to 11:59:59pm.
 
edit: after thinking about it some more - that wouldn't work quite right. If that process was followed the file would always be greater than 24 hours old, by however much time elapsed since the file was last written and the time the script was ran. So scratch the original code sample.
 
Last edited:
A more robust (and easier on the eyes) version in PowerShell:

Code:
#Get yesterday beginning time (as midnight)
$yday = (Get-Date -Hour 0 -Minute 0 -Second 0).AddDate(-1)

#Get today beginning time (as midnight)
$today = Get-Date -Hour 0 -Minute 0 -Second 0

#Set destination for file copy
$dest = "\\backupserver\share\name"

#Look for your file. As long as it's within the two dates we're good.
$csvFile = dir C:\Path\To\Backup\*backup.csv | where {(($_.LastWriteTime -ge $yday) -and ($_.LastWriteTime -le $today))}

#Make sure a file was returned. If $csvFile is empty it's going to puke.
if ($csvFile) { copy $csvFile $dest -verbose }
else { Write-Host "WTF, where's the file?" }

Still, not perfect, but is a little more succinct with which file could potentially be copied.

Crediting This Blog Post for reference.
 
Back
Top