Reputation: 53
I am using powershell to copy files into a folder, but I want to update the modified time [LastWriteTime] with the current time. I would like some advice on how I can do that once I copy the file. Below is my current code
$Date = Get-Date -Format "dd-MM-yyyy-dddd HH-mm"
$FROM = Get-ChildItem "C:\Testing\DeviceLists\" -Recurse
$TO = Get-ChildItem "C:\Transfer"
$LOGS = "C:\Testing\logs\"
#$d = [datetime](Get-ItemProperty -Path C:\Transfer -Name LastWriteTime).lastwritetime
$d = Get-ChildItem C:\Transfer\ -File | Sort-Object -Property -CreationTime | Select-Object -Last 1
foreach($item in $from){
$fromdate = $item.LastWriteTime
$ToFileInfo = $TO | where Name -eq $item.Name
if((Get-date $fromdate) -ge (Get-Date $d.LastWriteTime))
{
#Move the files that are greater than destination date
copy-item $item.FullName -Destination "C:\Transfer" -Force
#change lastwritetime of copied file
$item.LastWriteTime = (Get-Date)
Add-Content "$Logs\Log - $Date.txt" -Value "$item has been copied"
}
else
{
#log that file is having an error or lesser time
}
};
Add-Content "$Logs\Log - $Date.txt" -Value "All files have been copied"
Upvotes: 1
Views: 937
Reputation: 174465
Use -PassThru
to make Copy-Item
return the copied file info, then set the timestamp on that:
$newItem = Copy-Item $item.FullName -Destination "C:\Transfer" -Force -PassThru
$newItem.LastWriteTime = Get-Date
Upvotes: 2