1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ディレクトリ名にファイル・ディレクトリ数を追加する

Posted at

下位のディレクトリ内のファイル・ディレクトリ数を追加するPowerShellスクリプトを書いてみた。
InboxというディレクトリがInbox(10)となる感じ。

Recause-Item.ps1
$dir = $args[0]

## カレントディレクトリがInbox(10)みたいな時、引数がInbox, 10の二つに分かれるため
if ($args.Length -eq 2) {
   $dir = $dir + "(" + $args[1] + ")"
}

if ((Test-Path $dir) -eq $false) {
    Write-Output $dir
    Exit
}

function Recurse-Item($dir)
{
    foreach ($f in Get-ChildItem $dir | Where-Object { $_.GetType().Name -eq "DirectoryInfo" }) {
        Recurse-Item($f.FullName)

        $cnt = @(Get-ChildItem  $f.FullName).Length

        if ($cnt -eq 0) {
            Remove-Item $f.FullName
        ##  Write-Output $f.FullName
        } else {
            $name = $f.Name -replace "^(.+)\(\d+\)", "`$1"
            $name = $name + "(" + $cnt + ")"

            if ($f.Name -ne $name) {
                Rename-Item $f.FullName $name
            ##  Write-Output $name
            }
        }

    }
}

Recurse-Item($dir)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?