使用批处理文件获取目录的名称和每个目录中的文件数

时间:2015-02-28 20:20:57

标签: batch-file

我对使用批处理文件和涉及的代码比较陌生。我想要做的是获取目录/子目录列表并获取每个目录中的特定文件(.txt,.MP4等)的数量,然后将它们存储在.txt文件中。我已经想出了如何获取目录列表并存储它们:

dir /b /ad "C:\FILE" > "C:\List.txt"

现在我如何获得每个目录中的文件数量?我希望文本文件中的格式类似于:

“folder1 - 5个文件”
“folder2 - 3个文件”

文件夹名称和一行中的文件数,然后是下一个文件夹。谢谢你能给我的任何帮助。

1 个答案:

答案 0 :(得分:0)

下一个脚本可能会按预期工作。使用rem注释给出的一些基本解释。

@ECHO OFF >NUL
SETLOCAL enableextensions

    rem set up initial folder
set "myFolder=C:\FILE"

    rem set up extension list (no * or ? wildcards, please)
    rem could be defined explicitly as follows
set "myExtens=bat html"
    rem or, with next  proposed usage: 28786418.bat "bat html"
set "myExtens=%~1"
    rem or, with next (current) usage: 28786418.bat bat html
set "myExtens=%*"

    rem call a subroutine to output folder name & file count 
call :filecount "%myFolder%"

    rem call that subroutine for ech subfolder (cf. /A:D /S switch)
for /F "tokens=*" %%g in ('
    DIR /B /A:D /S "%myFolder%"
  ') do call :filecount "%%~g"

ENDLOCAL
goto :eof

:filecount
    rem subroutine
    rem input: (full) path to folder
    rem output: echo "input" - file count

    rem init a file counter to zero 
set /A "numFiles=0"
    rem increment file counter by 1 for each file in folder
    rem note '2^>nul' to avoid "file not found" message if folder empty
    rem note 'set' does not require delayed variable expansion
    rem            as the 'numFiles' variable correctly increments
for /F "tokens=*" %%G in ('
    DIR /B /A:-D "%~1" 2^>nul
  ') do (
         rem treat the extension list if any
         rem ( or insert here any other logic instead ) 
      if defined myExtens (
        for %%H in (%myExtens%) do if /i ".%%~H"=="%%~xG" set /A "numFiles+=1"
      ) else (
        set /A "numFiles+=1"
      )
    )
    rem display output to console (command line window)
echo "%~1" - %numFiles%
goto :eof

要将输出存储到文本文件,您可以使用(假设脚本保存为28786418.bat

 28786418.bat>list.txt

编辑(补充):输出set "myFolder=d:\bat\StackOverflow"

==>D:\bat\StackOverflow\28786418.bat
"d:\bat\StackOverflow" - 44
"d:\bat\StackOverflow\files" - 27
"d:\bat\StackOverflow\files\backup_done" - 0
"d:\bat\StackOverflow\files\Roboto" - 0
"d:\bat\StackOverflow\files\root" - 0
"d:\bat\StackOverflow\files\Roboto\backup_done" - 0
"d:\bat\StackOverflow\files\root\Ian-ionescu" - 2
"d:\bat\StackOverflow\files\root\John-doe" - 2
"d:\bat\StackOverflow\files\root\Nicola-sheperd" - 1
"d:\bat\StackOverflow\files\root\Sara-smith" - 2

==>D:\bat\StackOverflow\28786418.bat bat html>files\28786418.txt

==>type files\28786418.txt
"d:\bat\StackOverflow" - 40
"d:\bat\StackOverflow\files" - 3
"d:\bat\StackOverflow\files\backup_done" - 0
"d:\bat\StackOverflow\files\Roboto" - 0
"d:\bat\StackOverflow\files\root" - 0
"d:\bat\StackOverflow\files\Roboto\backup_done" - 0
"d:\bat\StackOverflow\files\root\Ian-ionescu" - 0
"d:\bat\StackOverflow\files\root\John-doe" - 0
"d:\bat\StackOverflow\files\root\Nicola-sheperd" - 0
"d:\bat\StackOverflow\files\root\Sara-smith" - 0

==>