Is there a way to count number of files in a folder then stop if over a limit (for efficiency), say 100?
I am not a programmer, I believe this has to do with looping & controlling the iterations. I have tried some various command & am not figuring it out. Thanks in advance :)
Backstory: I have an interactive batch file that recursively lists files in a folder. I'd love if I could warn user if there is a large number of files before listing the files. I found a command to count the number of files, catch-22 it likely takes as long to count the files as it does to list them; both (counting & its efficiency) are covered here: Efficiently counting files in directory and subfolders with specific name
File list interactive script if it helps for context:
REM https://support.microsoft.com/en-us/help/196158/how-to-create-a-text-file-list-of-the-contents-of-a-folder
REM this is a comment line, technically called a REM(ark) line
REM @echo off tells command-prompt not to show commands & directory location (what you normally see when opening cmd), the @ symbol says to not show that command itself. We clear the screen (cls) so it doesn't show THIS remark text or any errors about being run from a UNC location. The two && says that if the 1st command is successful do the 2nd one
REM %1 is a placeholder/variable for the filename of whatever is dropped-on the batch. If we had just put echo %1 & no file was specified it would be like putting echo by itself turning echo back on. IF checks whether the variable is blank/not-given & does a blank line if so; ELSE gets processed if variable wasn't blank (display the file path+name in our situation). The brackets are needed if there is a space in the file path/name. The empty brackets indicate a blank string (similar to ""). The parenthesis tells IF to process only things inside it, without it the program will think you want to run "echo. ELSE echo %1", basically echo the rest of the line
REM %~dp0 is the location of THIS batch file (%0=file path including the filename, dp tells it to cut that out [DrivePath only]), %1 is the variable holding the filename & path
@echo off && cls
echo ------------------------------------------------------------------------------
echo Directory contents I will list in Notepad:
IF [%1]==[] (echo [%~dp0]) ELSE (echo [%1])
echo.
echo You can choose another location by dragging it onto this batch file or copying this batch file to it
echo #4 is the only way to get JUST filenames. Sadly DIR command can't show just filenames AND date else it has headers and summary, try #3 for that
echo ------------------------------------------------------------------------------
REM cmd/batch files do not support UNC paths (\\acme.com\SharedFolder) natively-kb156276. PUSHD is the workaround, it maps UNC to a drive letter, then CHANGES-MOVES to that path, POPD reverts it (removes network drive)-kb317379
IF [%1]==[] (pushd %~dp0) ELSE (pushd %1)
echo.
REM declare then fill variable to file path we will store output; %temp is SYSTEM variable going to C:\Users\UserWhoRanScript\AppData\Local\Temp; parenthesis in case it has a space in path which causes errors
set ListFileLog="%temp%\FileListing.txt"
REM CHOICE prompts users; /C list of choices; /M message before prompt
CHOICE /C 123456 /M "Choose what to list: 1)All files; 2)All files-newest first (no folder dates); 3)All files-all dates (Create+Modify); 4)All files-no date or sizes; 5)Executables/compressed (exe, msi, zip, 7z, rar); 6)Media files"
REM IF ERRRORLEVEL by itself has a weird oddity so this format checks it exactly as user enters then jumps to that section of the script
IF "%ERRORLEVEL%" == "1" GOTO ListAll
IF "%ERRORLEVEL%" == "2" GOTO ListAllNewestFirst
IF "%ERRORLEVEL%" == "3" GOTO ListAll-EveryDate
IF "%ERRORLEVEL%" == "4" GOTO ListAllNoDatesSizes
IF "%ERRORLEVEL%" == "5" GOTO ListExe
IF "%ERRORLEVEL%" == "6" GOTO ListMedia
:ListAll
REM S=subdirectory files (recursive) we then redirect > that output to our text file so it stays open for user when cmd window closes. > wipes logfile contents whereas >> appends to existing logfile contents
dir /s > %ListFileLog%
GOTO ExitProc
:ListAllNewestFirst
REM /a attributes -d means don't show directories (doesn't show folder modify date or the period(.) folders); /o order by [d]ate - is to reverse so newest first; /t time-date to show ([c]reation, last [a]ccess, last [w]ritten)
dir /a:-d /o:-d /s > %ListFileLog%
GOTO ExitProc
:ListAll-EveryDate
REM shorthand: powershell ls^|fl
REM there is no way to show creation, modify, and access dates with cmd dir so using powershell. ^carat is because cmd will interpret that as you giving another command so fails
powershell.exe Get-ChildItem -Recurse ^| Select-Object FullName,CreationTime,LastWriteTime,LastAccessTime > %ListFileLog%
GOTO ExitProc
:ListAllNoDatesSizes
REM B=bare no dates or file sizes
dir /s /b > %ListFileLog%
GOTO ExitProc
:ListExe
REM show only files with these extensions, S and B switches/parameters detailed above
dir /s /b *.exe *.msi *.zip *.7z *.rar > %ListFileLog%
GOTO ExitProc
:ListMedia
dir /s /b *.mp3 *.wma *.aac *.m4a *.wav *.mpg *.mp4 *.avi > %ListFileLog%
GOTO ExitProc
:ExitProc
popd && cls
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!DONE!! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo.
echo You should see output in Notepad, close Notepad ^& this window will close with it
start /w notepad %ListFileLog%
del %ListFileLog%
exit
powershell.exe
from a batch file, just doing so would add a not insignificant delay to your process. This may mean that runningPowerShell
may only be worthwhile if you have a very large number of matching files and/or subdirectory tree.