Simple PowerShell LastWriteTime compare

前端 未结 7 1959
别那么骄傲
别那么骄傲 2021-02-12 08:31

I need a PowerShell script that can access a file\'s properties and discover the LastWriteTime property and compare it with the current date and return the date differe

相关标签:
7条回答
  • 2021-02-12 08:50
    (ls $source).LastWriteTime
    

    ("ls", "dir", or "gci" are the default aliases for Get-ChildItem.)

    0 讨论(0)
  • 2021-02-12 08:56

    I can't fault any of the answers here for the OP accepted one of them as resolving their problem. However, I found them flawed in one respect. When you output the result of the assignment to the variable, it contains numerous blank lines, not just the sought after answer. Example:

    PS C:\brh> [datetime](Get-ItemProperty -Path .\deploy.ps1 -Name LastWriteTime).LastWriteTime
    
    Friday, December 12, 2014 2:33:09 PM
    
    
    
    PS C:\brh> 
    

    I'm a fan of two things in code, succinctness and correctness. brianary has the right of it for succinctness with a tip of the hat to Roger Lipscombe but both miss correctness due to the extra lines in the result. Here's what I think the OP was looking for since it's what got me over the finish line.

    PS C:\brh> (ls .\deploy.ps1).LastWriteTime.DateTime
    Friday, December 12, 2014 2:33:09 PM
    
    PS C:\brh> 
    

    Note the lack of extra lines, only the one that PowerShell uses to separate prompts. Now this can be assigned to a variable for comparison or, as in my case, stored in a file for reading and comparison in a later session.

    0 讨论(0)
  • 2021-02-12 09:01

    Slightly easier - use the new-timespan cmdlet, which creates a time interval from the current time.

    ls | where-object {(new-timespan $_.LastWriteTime).days -ge 1}
    

    shows all files not written to today.

    0 讨论(0)
  • 2021-02-12 09:04

    (Get-Item $source).LastWriteTime is my preferred way to do it.

    0 讨论(0)
  • 2021-02-12 09:11

    I have an example I would like to share

    $File = "C:\Foo.txt"
    #retrieves the Systems current Date and Time in a DateTime Format
    $today = Get-Date
    #subtracts 12 hours from the date to ensure the file has been written to recently
    $today = $today.AddHours(-12)
    #gets the last time the $file was written in a DateTime Format
    $lastWriteTime = (Get-Item $File).LastWriteTime
    
    #If $File doesn't exist we will loop indefinetely until it does exist.
    # also loops until the $File that exists was written to in the last twelve hours
    while((!(Test-Path $File)) -or ($lastWriteTime -lt $today))
    {
        #if a file exists then the write time is wrong so update it
        if (Test-Path $File)
        {
            $lastWriteTime = (Get-Item $File).LastWriteTime
        }
        #Sleep for 5 minutes
        $time = Get-Date
        Write-Host "Sleep" $time
        Start-Sleep -s 300;
    }
    
    0 讨论(0)
  • 2021-02-12 09:13

    Try the following.

    $d = [datetime](Get-ItemProperty -Path $source -Name LastWriteTime).lastwritetime
    

    This is part of the item property weirdness. When you run Get-ItemProperty it does not return the value but instead the property. You have to use one more level of indirection to get to the value.

    0 讨论(0)
提交回复
热议问题