Answered by:
Compare timestamps for two files

Question
-
Hi guys,
I'm trying to replace Linux command with PS to compare timestamps for two files. In Linux it is [ $file1 -nt $file2 ]. Can somebody help to find PS analog?
Thursday, October 1, 2015 11:41 AM
Answers
-
it seems like I find the way:
"Get-ChildItem \\birepoprod\CRECHA\*.zip | Foreach-Object {if ($_.LastWriteTime -gt (Get-Item $_'.loaded').LastWriteTime) {rm $_'.loaded'} else {ls $_}}"
Thank you anyway for ideas!
- Marked as answer by Roman Kuritsyn Thursday, October 1, 2015 12:38 PM
- Edited by Roman Kuritsyn Thursday, October 1, 2015 12:38 PM shorter
Thursday, October 1, 2015 12:38 PM
All replies
-
-
Another way is calculate the difference between the timestamps
[datetime](Get-ItemProperty -Path $file1 -Name LastWriteTime).lastwritetime -[datetime](Get-ItemProperty -Path $file2 -Name LastWriteTime).lastwritetime
If the result is negative the $file1 is older. Deliver more information on this two files.
Thursday, October 1, 2015 12:02 PM -
Mike, Guy, thanks. Let me show what I get now, but have no idea where the mistake is:
"Get-ChildItem \\birepoprod\CRECHA\*.zip | Foreach-Object {if ('\\birepoprod\CRECHA\'+$_.LastWriteTime -gt (Get-ChildItem $_.loaded).LastWriteTime) {1} else {$_.Name}}"
- Edited by Roman Kuritsyn Thursday, October 1, 2015 12:09 PM mistake
Thursday, October 1, 2015 12:08 PM -
-
I need:
for each .zip file in the folder find .loaded (postfix) file compare timestamps. If .zip file is newer then .loaded then remove .loaded
Thursday, October 1, 2015 12:14 PM -
Get-ChildItem $_.loaded is not correct - delivers nothing. Trying something like this
$loaded = Get-ChildItem $_.name if ($loaded -like '*loaded*') { ... } else { ... }
- Edited by Guy Jascht Thursday, October 1, 2015 12:29 PM
Thursday, October 1, 2015 12:27 PM -
I need:
for each .zip file in the folder find .loaded (postfix) file compare timestamps. If .zip file is newer then .loaded then remove .loaded
Here's a framework:
Get-ChildItem .\*.zip | ForEach { $zip = $_ $loaded = Get-Item (Join-Path -Path $zip.Directory -ChildPath "$($zip.BaseName).loaded") If ($zip.LastWriteTime -gt $loaded.LastWriteTime) { Write-Host "$($zip.Name) is newer than $($loaded.Name)" -ForegroundColor Green Remove-Item -Path $loaded.FullName -WhatIf } Else { Write-Host "$($loaded.Name) is newer than $($zip.Name)" -ForegroundColor Red } }
This will not remove anything yet. If you like the results, remove the -WhatIf switch and run it again to do the actual removal.
- Edited by Mike Laughlin Thursday, October 1, 2015 12:39 PM
Thursday, October 1, 2015 12:36 PM -
it seems like I find the way:
"Get-ChildItem \\birepoprod\CRECHA\*.zip | Foreach-Object {if ($_.LastWriteTime -gt (Get-Item $_'.loaded').LastWriteTime) {rm $_'.loaded'} else {ls $_}}"
Thank you anyway for ideas!
- Marked as answer by Roman Kuritsyn Thursday, October 1, 2015 12:38 PM
- Edited by Roman Kuritsyn Thursday, October 1, 2015 12:38 PM shorter
Thursday, October 1, 2015 12:38 PM