![](https://batch-file.net/wp-content/themes/cocoon-master/images/b-woman.png)
バッチファイルで
・指定したフォルダ配下の
・ファイル数を取得
できます!
コード
ここでは例として
・デスクトップにあるフォルダ「test」配下の
・ファイル数を取得
します。
![フォルダ「test」](https://batch-file.net/wp-content/uploads/2022/07/bat_get_file_count_001.png)
![フォルダ「test」配下には3ファイルある](https://batch-file.net/wp-content/uploads/2022/07/bat_get_file_count_002.png)
@echo off
rem 対象フォルダ
set targetFolder=C:\Users\user\Desktop\test
rem フォルダの存在有無を確認
if not exist %targetFolder% (
echo 対象フォルダが存在しないため、処理を終了します。
echo.
pause
exit
)
rem ファイル数を取得するコマンドを作成
set cmd="dir %targetFolder% /a-d /b | find /c /v """
rem ファイル数を取得するコマンドを実行し、結果を変数「fileCount」へ格納
for /f %%a in ('%cmd%') do (
set fileCount=%%a
)
rem 結果
echo ファイル数:%fileCount%
echo.
pause
exit
実行結果
バッチファイルで
・指定したフォルダ配下の
・ファイル数を取得
できました。
※フォルダ「test」配下のファイル数は3であることを確認できました。
![実行結果](https://batch-file.net/wp-content/uploads/2022/07/bat_get_file_count_003.png)
参考①
フォルダ数も取得できます。
詳細は以下の記事をご確認ください。
参考②
上記で使用した以下の詳細は、公式サイトをご確認ください。
●「dir」コマンド
●「find」コマンド
●「for」コマンド