locked
Compare timestamps for two files RRS feed

  • 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!


    Thursday, October 1, 2015 12:38 PM

All replies

  • Hi,

    You can use the LastWriteTime property to test against.

    Example:


    (Get-Item .\file1.txt).LastWriteTime -lt (Get-Item .\file2.txt).LastWriteTime


    Thursday, October 1, 2015 11:48 AM
  • 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}}" 


    Thursday, October 1, 2015 12:08 PM
  • What exactly are you trying to determine?

    Thursday, October 1, 2015 12:11 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.


    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!


    Thursday, October 1, 2015 12:38 PM