windows - 批处理列表文件夹,统计pdf文件并获取文件名
问题描述
我想创建一个我每周从其他部门收到的所有文件的摘要。文件夹结构如下图。每个文件夹将包含多个 pdf 文件和只有 1 个 .docx 文件。因此,我想列出文件夹名称,计算其中的 pdf 文件总数并获取 docx 文件的名称。如果 docx 丢失,显示“丢失”,这也可能吗?
我确实尝试了这段代码,但这将显示文件夹中的所有文件。
@echo off
FOR /F "delims=" %%F IN ('dir /B /A /S *') DO (
for %%D in ("%%~dpF\.") do echo/ FolderName: %%~nxD ^| FileName: %%~nxF
)
pause
更新:今天我在测试建议的代码时,他们上周向我发送了文件。令我惊讶的是,另一个部门改变了文件夹结构(facepalm!)。下面更新了文件夹结构以供参考。
更新的文件夹结构:
Current
|count.bat
|--FolderA-Folder1-Nov 2020.docx
| -pdf1.pdf
| -pdf2.pdf
| -pdf3.pdf
| -pdf4.pdf
| Folder2-Dec 2020.docx
| -pdf1.pdf
| -pdf2.pdf
| -pdf3.pdf
| -pdf4.pdf
|--FolderB-Folder1-Nov 2020.docx
| -pdf1.pdf
| -pdf2.pdf
| -pdf3.pdf
| -pdf4.pdf
| Folder2-Dec 2020.docx
| -pdf1.pdf
| -pdf2.pdf
| -pdf3.pdf
| -pdf4.pdf
|--FolderC-Folder1-Nov 2020.docx
| -pdf1.pdf
| -pdf2.pdf
| -pdf3.pdf
| -pdf4.pdf
预期结果
FolderName: Folder A Folder 1 PDFtotal: 4pdfs DocFile: Nov 2020.docx
FolderName: Folder A Folder 2 PDFtotal: 4pdfs DocFile: Dec 2020.docx
FolderName: Folder B Folder 1 PDFtotal: 4pdfs DocFile: Nov 2020.docx
FolderName: Folder B Folder 2 PDFtotal: 4pdfs DocFile: Dec 2020.docx
FolderName: Folder C Folder 1 PDFtotal: 4pdfs DocFile: Nov 2020.docx
解决方案
原始脚本
不带选项的dir
命令/B
在倒数第二行显示匹配项的计数,可以通过for /F
循环捕获和解析。
由于您有一个平面目录结构,我不会使用递归方法(如dir /S
or for /R
)。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=." & rem // (target directory; `.` is current, `%~dp0.` is parent)
set "_MASK=Doc*.docx" & rem // (pattern to find particular file)
set _LIST="pdf" & rem // (space-separated list of file extensions without `.`)
rem // Loop through immediate sub-directories of the target directory:
for /D %%D in ("%_ROOT%\*") do (
rem // Return name of current sub-directory without trailing line-break:
< nul set /P ="FolderName: %%~nxD "
rem // Loop through given list of file extensions:
for %%E in (%_LIST%) do (
rem // Initialise variables:
set /A "CNT=0, NUM=0" & set "NAME="
rem // Capture number of matching files from last but one line of `dir`:
for /F "eol= " %%C in ('2^> nul dir /A:-D-H-S "%%~D\*.%%~E"') do (
2> nul set /A "CNT=NUM, NUM=%%C"
)
rem // Return count of matching files without trailing line-break:
< nul call set /P ="PDFtotal: %%CNT%%%%~Es "
)
rem // Find file that matches the given pattern (assuming there is only one):
for %%F in ("%%~D\%_MASK%") do set "NAME=%%~nxF"
rem // Return name of found file, with trailing line-break this time:
call echo DocFile: %%NAME%%
)
endlocal
exit /B
/A:-D-H-S
代码中命令的选项dir
防止隐藏和系统文件被统计;/A:-D
如果您也想考虑它们,请将其更改为。
更新的脚本
在下文中,您将找到适应您更改的目录结构的新代码(您会注意到只需要进行一些修改):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=." & rem // (target directory; `.` is current, `%~dp0.` is parent)
set "_MASK=* 2020.docx" & rem // (pattern to find particular file)
set _LIST="pdf" & rem // (space-separated list of file extensions without `.`)
rem // Loop through immediate sub-directories of the target directory:
for /D %%S in ("%_ROOT%\*") do (
rem // Loop through next level of sub-directories:
for /D %%D in ("%%~S\*") do (
rem // Return names of current sub-directories without trailing line-break:
< nul set /P ="FolderName: %%~nxS %%~nxD "
rem // Loop through given list of file extensions:
for %%E in (%_LIST%) do (
rem // Initialise variables:
set /A "CNT=0, NUM=0" & set "NAME="
rem // Capture number of matching files from last but one line of `dir`:
for /F "eol= " %%C in ('2^> nul dir /A:-D-H-S "%%~D\*.%%~E"') do (
2> nul set /A "CNT=NUM, NUM=%%C"
)
rem // Return count of matching files without trailing line-break:
< nul call set /P ="PDFtotal: %%CNT%%%%~Es "
)
rem // Find file that matches the given pattern (assuming there is only one):
for %%F in ("%%~D\%_MASK%") do set "NAME=%%~nxF"
rem // Return name of found file, with trailing line-break this time:
call echo DocFile: %%NAME%%
)
)
endlocal
exit /B
推荐阅读
- javascript - 在角度 9 的 elemntRef 中找不到 Div 的内部文本
- android - android片段没有填满整个屏幕
- apostrophe-cms - 我可以更改 apos-area 及其小部件的包装器元素吗?
- android - 我想使用Android App从firebase中单独获取所有数据
- android - “android:windowLightStatusBar”不起作用
- vue.js - 在 Quasar/Vue 中触发/更新从子组件到父组件的数据更改,以防子组件具有项目列表
- java - 禁用 Spring Cloud 配置客户端
- javascript - 如何使用 Vue CLI 一次将多个 Vue.js 组件构建为原生 Web 组件?
- laravel - 关闭浏览器时,Laravel 登录过期
- git - 如何在 GitLab-runner 中使用 Git 命令?