Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

下位のディレクトリ内のファイル・ディレクトリ数を追加する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)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした