Simple batch file commands to count number of files in a folder or directory and then store it in a variable
Step 1: To Count the number of files in a folder including sub-folders.
dir /a:-d /s /b "Folder Path" | find /c ":\" > tempFile.txt
Above command counts the number of files in a folder (mentioned as “Folder Path”) and writes the count number to a text file. Note that replace Folder path in above command with your folder path Example: “C:\Folder”.
Step 2: Write Files count from text file to a Variable
SET /p FilesCount=<tempFile.txt
References:
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/dir
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1