I would like to create a summary of all the files that I received weekly from other department. The folder structure shown below. Each folder will contain multiples pdf files and only 1 .docx file. So, i would like to list down the folder name, count total pdf files inside and get the name of docx file. If docx missing, show "missing", is this possible too?
I did try this code, but this will show all files in the folder instead.
@echo off
FOR /F "delims=" %%F IN ('dir /B /A /S *') DO (
for %%D in ("%%~dpF\.") do echo/ FolderName: %%~nxD ^| FileName: %%~nxF
)
pause
UPDATE:Today while Im testing suggested code, they send me last week file. AND to my surprised, the other department changed folder structure (facepalm!). Below updated folder structure for reference.
Updated folder structure:
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
Expectation Results
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
The dir
command without the /B
option displays the count of matching items in the penultimate line, which could be captured and parsed by a for /F
loop.
Since you have got a flat directory structure, I would not do a recursive approach (like 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
The option /A:-D-H-S
of the dir
command in the code prevents hidden and system files from being counted; change it to /A:-D
if you do want to regard them as well.
In the following you will find the new code adapted to your changed directory structure (you will notice that there were only a few modifications necessary):
@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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments