반응형
PowerShell로 특정 시간 이상 된 파일인지 확인하려면 어떻게 해야 합니까?
$fullPath의 파일이 "5일 10시간 5분"보다 오래되었는지 확인하려면 어떻게 해야 합니까?
(OLD는 5일 10시간 5분 이내에 생성 또는 수정된 경우를 의미합니다.)
이를 위한 간단하면서도 매우 쉽게 읽을 수 있는 방법은 다음과 같습니다.
$lastWrite = (get-item $fullPath).LastWriteTime
$timespan = new-timespan -days 5 -hours 10 -minutes 5
if (((get-date) - $lastWrite) -gt $timespan) {
# older
} else {
# newer
}
이것이 효과가 있는 이유는 날짜 두 개를 빼면 시간 범위가 생기기 때문입니다.기간은 표준 연산자와 비교 가능합니다.
도움이 되길 바랍니다.
Test-Path
이 작업을 수행할 수 있습니다.
Test-Path $fullPath -OlderThan (Get-Date).AddDays(-5).AddHours(-10).AddMinutes(-5)
이 파워셸 스크립트는 5일, 10시간, 5분보다 오래된 파일을 표시합니다.파일로 저장할 수 있습니다..ps1
extension(확장) 후)
# You may want to adjust these
$fullPath = "c:\path\to\your\files"
$numdays = 5
$numhours = 10
$nummins = 5
function ShowOldFiles($path, $days, $hours, $mins)
{
$files = @(get-childitem $path -include *.* -recurse | where {($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)})
if ($files -ne $NULL)
{
for ($idx = 0; $idx -lt $files.Length; $idx++)
{
$file = $files[$idx]
write-host ("Old: " + $file.Name) -Fore Red
}
}
}
ShowOldFiles $fullPath $numdays $numhours $nummins
다음은 파일을 필터링하는 줄에 대해 조금 더 자세히 설명합니다.다음과 같은 의견을 포함할 수 있도록 여러 줄로 나누어집니다(법적 파워셸이 아닐 수도 있습니다.
$files = @(
# gets all children at the path, recursing into sub-folders
get-childitem $path -include *.* -recurse |
where {
# compares the mod date on the file with the current date,
# subtracting your criteria (5 days, 10 hours, 5 min)
($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins))
# only files (not folders)
-and ($_.psIsContainer -eq $false)
}
)
언급URL : https://stackoverflow.com/questions/16613656/how-can-i-check-if-a-file-is-older-than-a-certain-time-with-powershell
반응형
'itsource' 카테고리의 다른 글
그룹의 SQL 선택 멤버 (0) | 2023.10.29 |
---|---|
Access-Control-Allow-Origin에서는 오리진 null이 허용되지 않습니다. (0) | 2023.10.29 |
엑셀 시트가 TFS에서 생성되었다는 것을 잊게 하려면 어떻게 해야 합니까? (0) | 2023.10.24 |
쿼리 내에서 DML 작업을 수행할 수 없습니다. (0) | 2023.10.24 |
아이폰 애플리케이션에서 비밀번호 필드의 텍스트를 어떻게 모호하게 합니까? (0) | 2023.10.24 |