Skip to content

Instantly share code, notes, and snippets.

@kazuhito-m
Last active January 25, 2018 18:19
Show Gist options
  • Save kazuhito-m/bacdf9bc14b71bca5c8fabc577d65c75 to your computer and use it in GitHub Desktop.
Save kazuhito-m/bacdf9bc14b71bca5c8fabc577d65c75 to your computer and use it in GitHub Desktop.
PowerShellで書いた「指定フォルダ直下の容量チェック」
# フォルダ容量チェッカー。
#
# フォルダの上限と容量を指定すると、
# 直下のフォルダがその容量を超えていた場合に、
# 警告ファイル(warning.log)を出力する。
#
# ex. folderLimitChecker.ps1 C:work 1000000
#
Param( $targetFolder, $maxSize )
$WARNING_FILE_NAME = "\warning.log"
function GetFolderTotalSize($folderPath)
{
$size = 0
$items = Get-ChildItem $folderPath
foreach ($item in $items)
{
if ($item.PSIsContainer) {
$size += GetFolderTotalSize $item.FullName
} else {
$size += $item.Length
}
}
return $size;
}
function WriteWarningFile($folderPath, $maxFileSize)
{
$path = $folderPath + $WARNING_FILE_NAME
$message = "フォルダ: ${folderPath} の容量が ${maxFileSize} byteを超えています。ファイルを削除してください。"
$message | Out-File -FilePath $path -Encoding utf8
}
# main process.
$folders = Get-ChildItem $targetFolder | ? { $_.PSIsContainer }
foreach ($folder in $folders)
{
$size = GetFolderTotalSize $folder.FullName
if ($size -gt $maxSize)
{
WriteWarningFile $folder.FullName $maxSize
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment