Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What is going on with this article?
@gane_gane

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

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)
0
Help us understand the problem. What is going on with this article?
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.
Sign Up
If you already have a Qiita account Login
0
Help us understand the problem. What is going on with this article?