PowerShell 技能连载 - Working with [FileInfo] Object
Often, code needs to check on files, and for example test whether the file exists or exceeds a given size. Here is some commonly used code:
$logFile = "$PSScriptRoot\mylog.txt"
$exists = Test-Path -Path $logFile
if ($exists)
{
$data = Get-Item -Path $logFile
if ($data.Length -gt 100KB)
{
Remove-Item -Path $logFile
}
}
By immediately converting a string path into a FileInfo object, you can do more with less:
[System.IO.FileInfo]$logFile = "$PSScriptRoot\mylog.txt"
if ($logFile.Exists -and $logFile.Length -gt 0KB) { Remove-Item -Path $logFile }
You can convert any path to a FileInfo object, even if it is not representing a file. That’s what the property “Exists” is for: it tells you whether the file is present or not.
ReTweet this Tip!
PowerShell 技能连载 - Working with [FileInfo] Object
http://blog.vichamp.com/2017/11/15/working-with-fileinfo-object/