Answer» Hello,
please check my script:
@echo off SET loc=c:\temp\labor set ext=txt set /p loc=Please enter the SEARCH location (default c:\temp\labor) : set /p ext=Please enter the search file extension (default txt) : echo ---------------------------------------------------------------------------- echo The search will now search for the files with the extension you have entered echo *****Please wait this might take a time *********************** echo ---------------------------------------------------------------------------- echo The result of the search are : echo ----------------------------------------------------------------------------
for /f "usebackq delims=;" %%q in (`dir /s /b /ad %loc%`) DO (
for /f "tokens=*" %%a in ( 'dir /s %%q ^| find /c "%ext%"' ) do (
echo [D] = %%q [F] = %%a
) ) echo ----------------------------------------------------------------------------
pause
You omit the dot at the start of a file extension which means that if the user types in (for example) "txt" the search will pick up and COUNT files like mytxt.doc, matxt.jpg, etc. You could narrow it down by searching for the dot as well but you are still going to pick up myfile.txtjpg.zip etc.
Why not restrict the inner search using a wildcard (no dot before ext here)
for /f "tokens=*" %%a in ( 'dir /s %%q\*.%ext% ^| find /c "."' ) do (
This line will cope with folders having spaces in the name
Code: [Select]for /f "tokens=*" %%a in ( 'dir /s "%%q\*.%ext%" ^| find /c "."' ) do (
|