Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if any type of files exist in a directory using BATCH script

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

like image 841
psycho Avatar asked May 30 '12 09:05

psycho


People also ask

How do you verify if a file exists in a batch file?

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.

What is == in batch file?

[ == ] (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.

What is %% f in batch file?

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.


1 Answers

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) 
like image 131
dbenham Avatar answered Oct 24 '22 08:10

dbenham