check if file with certain file extension present in folder using batch script

Manas Mohanta

I want to check a certain file extension in the folder using batch script

Its like,

if exist <any file with extension .elf>
  do something
else
  do something else

Here file name may be anything but only extension(.elf) is important

MC ND

In the simplest case the batch language includes a construct for this task

if exist *.elf (
    :: file exists - do something 
) else (
    :: file does not exist - do something else
)

where the if exist will test for existence of an element in the current or indicate folder that matches the indicated name/wildcard expression.

While in this case it seems you will not need anything else, you should take into consideration that if exist makes no difference between a file and a folder. If the name/wildcard expression that you are using matches a folder name, if exists will evaluate to true.

How to ensure we are testing for a file? The easiest solution is to use the dir command to search for files (excluding folders). If it fails (raises errorlevel), there are no files matching the condition.

dir /a-d *.elf >nul 2>&1 
if errorlevel 1 (
    :: file does not exist - do something
) else (
    :: file exists - do something else
)

Or, using conditional execution (just a little abreviation for the above code)

dir /a-d *.elf >nul 2>&1 && (
    :: file does exist - do something
) || ( 
    :: file does not exist - do something else 
)

What it does is execute a dir command searching for *.elf , excluding folders (/a-d) and sending all the output to nul device, that is, discarding the output. If errorlevel is raised, no matching file has been found.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Check if a folder has a certain type of file present

how to check for certain file extension in a folder with php

Check if a particular file is present in the folder using python

Batch script to check if a file is over a certain age

Check if the path is File or Folder using batch

How to check a file/folder is present using pyspark without getting exception

To get the file extension of the files present in each row using shell script

Check if an argument passed to a bash script is a file of a certain extension

Change the name of file using windows batch script (keeping extension)?

How to extract extension of input file parameter using Windows batch script

copy certain files using batch script with whitelist txt file

Bash script delete file inside another folder if present in both with extension *.c

Bash script delete file inside another folder if present in both with extension *.c

PowerShell Script finding File using extension and excluding a folder

BATCH SCRIPT: Checking if file exist in folder using forloops

Getting certain file names using a powershell script in a folder

dir folders at a certain folder depth in a batch file

Batch - Move file that start with pattern to a certain folder

Batch Unrar after check file extension

How to check if the current batch file is in a specific folder?

How to associate a file extension to be opened with a batch script?

Trying to remove path, file extension and, if present, a certain query parameter

How to check file with given extension which are present in array list using nodejs

How to check for string in log file and move file to different folder according to status using batch

Continuously check a file modified or not using batch file

Check whether a file/folder exists, with cmd command-line (NOT batch script)

Delete all files in the folder except one extension(say .idf) using batch file

Add prefix to all file names in a folder with a certain extension using C#

how to create batch file in system folder using a batch file