Hello I'm looking to write a batch file to check to see if there are any files of any type inside a given folder.
So far I've tried the following
if EXIST FOLDERNAME\\*.* ( echo Files Exist ) ELSE ( echo "Empty" )
I can get it to work if I know the file extension such as a txt file with the follwing
if EXIST FOLDERNAME\\*.txt ( echo Files Exist ) ELSE ( echo "Empty" )
Thank you for your help
Checking for the existence of a file can be accomplished by using IF EXIST in a batch file called from the login script, or by using the login script ERRORLEVEL variable with a MAP statement. The COMMAND.COM /C will close the CMD box window automatically after it terminates.
[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.
To use for in a batch file, use the following syntax: for {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>] To display the contents of all the files in the current directory that have the extension .doc or .txt by using the replaceable variable %f, type: for %f in (*.doc *.txt) do type %f.
To check if a folder contains at least one file
>nul 2>nul dir /a-d "folderName\*" && (echo Files exist) || (echo No file found)
To check if a folder or any of its descendents contain at least one file
>nul 2>nul dir /a-d /s "folderName\*" && (echo Files exist) || (echo No file found)
To check if a folder contains at least one file or folder.
Note addition of /a
option to enable finding of hidden and system files/folders.
dir /b /a "folderName\*" | >nul findstr "^" && (echo Files and/or Folders exist) || (echo No File or Folder found)
To check if a folder contains at least one folder
dir /b /ad "folderName\*" | >nul findstr "^" && (echo Folders exist) || (echo No folder found)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With