How do I copy a file to new folder based on File name and date with powershell

Viewed 245

Hope someone can help, I'm new to PowerShell an can't seem to get this working the way I want.

I'm trying to create a PowerShell script to run every morning to check in a folder for a file that was created that morning, the file will have a creation date of the day before, for example 2022-04-20_test.csv

The list of files can grow in the folder so I only am trying to copy over the one file that was created in the morning with the previous days date on it.

The script I've been testing copies the file over but is not checking the date correctly so every file in the folder called *_test.csv gets moved.

is there a way of me checking the file name with the previous days date or checking when the file was created date?

$EarliestModifiedTime = (Get-Date).AddDays(-1)

I thought this would check for all yesterday only files.

Any help or suggestions would be gratefully received. thanks

$DestinationFolder = "c:\test1"
If(!(test-path $DestinationFolder))
{New-Item -ItemType Directory -Force -Path $DestinationFolder
}
$EarliestModifiedTime = (Get-date).AddDays(-1)
$Files = Get-ChildItem "c:\test\*_test.csv" -File
foreach ($File in $Files) 
{
if ($File.LastWriteTime -gt $EarliestModifiedTime)
{
    Copy-Item $File -Destination $DestinationFolder
    Write-Host "Copying $File"
}
else 
{
    Write-Host "Not copying $File"
}
}
1 Answers

If you want to identify the files by the date embedded in their names, use the following:

Note: I'm assuming you want just yesterday's file(s) in this example:

# Get yesterday's date in a format such as "2022-04-20"
$yesterdaysPrefix = (Get-Date).Date.AddDays(-1).ToString('yyyy-MM-dd')

Copy-Item -WhatIf "c:\test\${yesterdaysPrefix}_test.csv" -Destination $DestinationFolder

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

Note:

  • The .Date property returns the very start of the current calendar day (i.e., midnight). Its use isn't strictly necessary here, given that the format string ('yyyy-MM-dd') doesn't contain a time-of-day component.

  • However, use of .Date becomes important if you want to use [datetime] instances directly (rather than strings) for comparison, such as against files' .LastWriteTime properties, in order to cover the entire calendar day - see below.


As for what you tried:

  • $EarliestModifiedTime = (Get-Date).AddDays(-1) isn't the start of yesterday (midnight), but the same time of day as when you run this command; therefore, it's better to use $EarliestModifiedTime = (Get-date).Date.AddDays(-1)

  • Due to use of -gt, if ($File.LastWriteTime -gt $EarliestModifiedTime) also includes all of today's files.

    • Either: Use $File.LastWriteTime.Date -eq (Get-Date).AddDays(-1).Date to limit matching to yesterday's files.

    • Or: Use if ($File.LastWriteTime -le (Get-date).AddDays(-1).Date) - i.e. the -le operator (less-than or equal) operator rather than -gt (greater-than) - to match all files last written yesterday or before.

Related