Hi,
Tried searching the forum but couldn't find any working solution for my situation.
I'm trying to copy files from one folder to another with the condition that it can only copy files which were created only yesterday.
All other older or newer files should not be copied.
I tried altering this persons code: https://stackoverflow.com/questions/59052724/powershell-script-copying-file-from-yesterday-to-other-folder/66568079?noredirect=1#comment117676904_66568079
To this:
$DestinationFolder = "D:\test\upload"
If(!(test-path $DestinationFolder))
{
New-Item -ItemType Directory -Force -Path $DestinationFolder
}
$EarliestModifiedTime = (Get-date).AddDays(-1)
$Files = Get-ChildItem "D:\test\*.*" -File
foreach ($File in $Files)
{
if ($File.CreationTime -lt $EarliestModifiedTime)
{
Copy-Item $File -Destination $DestinationFolder
Write-Host "Copying $File"
}
else
{
Write-Host "Not copying $File"
}
}
But then it copied all older files together with those from yesterday. Which is obvious because of the "-lt" parameter. When I try to change that to "-eq" it does not work.
It does not want to copy, I get this message:
PS H:\> C:\test\testcopyps.ps1
Not copying D:\test\1.txt
Not copying D:\test\2.txt
Not copying D:\test\3.txt
Not copying D:\test\4.txt
Not copying D:\test\5.txt
Not copying D:\test\6.txt
Not copying D:\test\a.txt
PS H:\>
But the 1,2,3 text files are actually created yesterday so it should be equal to the $EarliestModifiedTime variable.
What am I doing wrong?