Simple PowerShell LastWriteTime compare

前端 未结 7 889
我寻月下人不归
我寻月下人不归 2021-02-12 08:42

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:57

    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;
    }
    

提交回复
热议问题