Batch list folder, count pdf files and get file name

amein

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
aschipfl

Original Script

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.


Updated Script

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.

edited at
0

Comments

0 comments
Login to comment

Related

Batch File: Copying files to an unknown folder name

Batch Files: List file names and folder names

Batch create folder based on partial file name and move files to folder

Batch file that identfies files based on its folder name

PowerShell script - Loop list of folders to get file count and sum of files for each folder listed

Delete all files in folder except file in list using batch

Recursively append folder name to files in it and moves all files to base folder Windows batch file

Batch print .pdf files from list.txt file

Batch file to count files matching first 6 characters in file name and output file group and its count

Get a list of all files in folder and sub-folder in a file

Batch using dir to get folder destination (cmd) and copy+paste files in name's folder found

How to delete file in folder except name files in list.txt?

Files Directory with Folder name and link to file creating a list

Batch Conversion of different files in a folder to PDF

Batch File - Rename files based on parent name and (sub)folder(s) name

Batch rename files in subfolders to parent folder name

Batch rename files in linux using folder name

How to get list of files in some folder of my war file?

List files from certain folder using batch

batch file deal with Ampersand (&) in folder name

duplicate file with different name and folder via batch

How to run a batch file on all files in a folder?

Batch file to rename files within a folder

Batch File to rename multiple files and move to folder

Batch file to delete files from wildcard folder

Batch Script: Move files to folder based on source folder name

Comparison of file list with files in folder

Windows batch file, Looping through files in folder to create a parameter list to pass into another script

How to list all files of specific extensions inside a folder with numbering order using batch file