Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

3
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.

ワンラインでフォルダ以下のファイル、フォルダ数を再帰的にカウントするコマンド(備忘録)

Last updated at Posted at 2018-12-06

概要

ホワイトリスト型のセキュリティツールが入ったPCでファイル数が多いフォルダを調査する羽目になったので、今後使うかどうかはさておきバッチファイル等を使わずにコマンド一行のみでカウントする手段をメモっておく。

実行環境

一応Windows7で確認したが、コマンドプロンプトを使用するので多分Windows系OSなら問題なく動くはず。

実行コマンド

各コマンド、オプションの説明はdir /?等で確認できるので割愛。

コマンドプロンプト
for /f "delims=" %a in ('dir /s /b /ad "パス"') do ( echo %a && dir /b "%a" | find /c /v "" )

大まかな処理の流れとしては

  • for /f "delims=" %a in ('dir /s /b /ad "パス"')で属性がフォルダのものだけを一行ずつ変数(%a)に格納する。
  • echo %aでパスを表示する。
  • &&でコマンドを結合して一行で実行できるようにする。
  • dir /b "%a"で1)で変数に格納したフォルダ以下を取得、|find /c /v ""に渡して行数をカウントする。

実行結果

コマンドプロンプトを起動して上記の"パス"のところにサンプルとして用意した"C:\work\temp"を入れて実行すると・・・

実行結果
C:\work\temp\sample1
6
C:\work\temp\sample2
10
C:\work\temp\sample1\sample1_1
5

いい感じにフルパスとファイル、フォルダ数が表示されている。

その他・あとがき

今回は表示さえできれば結果がログに残るからテキスト出力とかしてないけど、必要ならリダイレクトで出力する。
バッチとかが使える環境ならshebang記法でJScriptとかを埋め込んで整形したテキストとかにできるのになぁ・・・(´・ω・`)

3
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

Qiita Conference 2025 Autumn will be held!: 11/5(Wed) - 11/7(Fri)

Qiita Conference is Qiita's largest tech conference, dedicated to engineers in the age of AI!

Keynote Speaker

piacere, Tsuyoshi Ushio, Esteban Suarez, Takuto Wada, seya, MinoDriven, Toshihiro Ichitani, Karaage, Yoshimasa Iwase, Matz, Minorun, Null-Sensei

View event details

Qiita Advent Calendar is held!

Qiita Advent Calendar is an article posting event where you post articles by filling a calendar 🎅

Some calendars come with gifts and some gifts are drawn from all calendars 👀

Please tie the article to your calendar and let's enjoy Christmas together!

3
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?