Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists from inside a batch file [duplicate]

I need to run a utility only if a certain file exists. How do I do this in Windows batch?

like image 622
sab Avatar asked Dec 02 '10 21:12

sab


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.

Why is %% used in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

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?

doc or . txt extension in the current directory is substituted for the %f variable until the contents of every file are displayed. To use this command in a batch file, replace every occurrence of %f with %%f. Otherwise, the variable is ignored and an error message is displayed.


1 Answers

if exist <insert file name here> (     rem file exists ) else (     rem file doesn't exist ) 

Or on a single line (if only a single action needs to occur):

if exist <insert file name here> <action> 

for example, this opens notepad on autoexec.bat, if the file exists:

if exist c:\autoexec.bat notepad c:\autoexec.bat 
like image 168
Chris J Avatar answered Oct 09 '22 08:10

Chris J