Stuggi
Stuggi

Reputation: 215

Getting RoboCopy to copy only completed files

I have an issue targeting the right files with robocopy in a folder.

I'm putting together a PowerShell script to copy a file from a folder to another folder to queue it for FTP upload. I want to copy last days file (it's a temperature log from a weather station), which is written to disk at 0:59 am. The problem is that the logging software also keeps the current days file in the folder, constantly writing to it. As such, I can't target the specific file with /minage /maxage as I normally would. I can't either use the /minage:1 /maxage:2 trick, since I need to copy yesterdays file daily, not the "day before yesterday" file.

My guess is I need to target the specific date in the file name, but that's a bit past my current skill level.

The robocopy command I'm using without success is robocopy $source .\ /maxage:1 /sec /copy:datsou /s /e

The file name structure is TAAVG1M_YYYYMMDD

UPDATE: Okay, I looked into it a bit more. I can get today's date in the correct format with Get-Date -format yyyyMMdd and I can get yesterdays date with (Get-Date).adddays(-1). But what I can't figure out is how to combine the commands into one that get's me yesterdays date in the right format.

Upvotes: 1

Views: 522

Answers (2)

Matt
Matt

Reputation: 46710

Robocopy is a great utility but I don't think you need that versatility for the example you are giving. You could just use Get-Item to get all of yesterdays files and do a simple copy. Something simple like this would copy all files in the source that end with yesterdays date to the destination folder

$yesterdaysDate = (Get-Date).adddays(-1).ToString("yyyyMMdd")
Get-Item  -Filter "*$yesterdaysDate" -Path $source | Copy-Item -Destination $destination

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

There are several ways to get a date as a formatted string:

  • The DateTime object's ToString() method:

    (Get-Date).AddDays(-1).ToString('yyyyMMdd')
    
  • The format operator (-f):

    '{0:yyyyMMdd}' -f (Get-Date).AddDays(-1)
    
  • Calling Get-Date a second time with the date you want to format and the parameter -Format:

    Get-Date -Date (Get-Date).AddDays(-1) -Format 'yyyyMMdd'
    

Upvotes: 0

Related Questions