24 Votes

Batch Script: Check if File exists

Question by Guest | Last update on 2021-04-06 | Created on 2016-08-29

I would like to check whether a specific file exists using a Windows Batch Script (.BAT) before going on with the script.

Is there any possibility to do so? I have never worked with conditions in batch scripts before.

ReplyPositiveNegative
3Best Answer5 Votes

For checking whether a file exists, you can just write "IF EXIST".

Behind that, you can write the file name and the action that should be executed in the case that the file exists. Here is an example:

IF EXIST c:\test.txt notepad c:\test.txt

With this line, first, it is checked, whether the file c:\test.txt exists. If yes, the file will be opened in the program notepad.

If you want to check for nonexistence, you can use IF NOT EXIST:

IF NOT EXIST c:\test.txt ECHO file does not exist

This writes the text "file does not exist" into the console if the file c:\test.txt is not existing.

If you cannot or do not want to write all commands into one line, you can also use the following syntax:

SET F="c:\test.txt"

IF EXIST %F% (
  ECHO %F% is existing
) ELSE (
  ECHO %F% is not existing
)

Additionally, I have worked with a variable in this example. "F" is first set to our file and afterwards, it is invoked with %F%.
2016-08-29 at 18:28

ReplyPositive Negative
Reply

Related Topics

PHP: File Download Script

Tutorial | 0 Comments

Rename File to its Folder Name

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.