C++Introduction to C++Language FeatureLearn C++

How To Check If A File Exists or Not on Windows

How To Check If A File Exists or Not on Windows

C++ Builder has a lot of specific methods in its SysUtils library that are included in the vcl and fmx libraries. Some of these are grouped as a Path Manipulation Routines that allows user to edit, extract, get and set drive name, directory name, file name, file extensions. .. etc, and these methods are combined in Vcl.FileCtrlSystem.IOUtilsSystem.SysUtils libraries. These all methods are easy to use and easy to get or set file path strings in that operating system. These can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check files or directories to see if they exist or not in that given path.

In this post, you’ll learn how to check if a file exists in a folder, whether a file exists or not, and how to check if a drive, directory, or file exists. By learning more about FileExists Method that checks if it exists in that path on Windows, and how to compile c++ in windows. It will help you to easily build C++ applications.

What is the C++ FileExists method?

The FileExists Method (System::SysUtils::FileExists) is a SysUtils Method in C++ Builder that checks whether a specified file exists. FileExists returns True if the file specified by FileName exists. If the file does not exist, FileExists returns False.

What is the syntax of the C++ FileExists method?

Here is the Syntax of FileExists Method:

Note: If the FileName parameter is a symbolic link and the FollowLink parameter is set to True, the method is performed on the target file. If the first condition is True, but the FollowLink parameter is set to False, the symbolic link is used, regardless whether the link is broken (the target file is invalid).


Method behavior if the FileName parameter is a file:

File ExistsFollowLinkMethod result
YESTrueTrue
YESFalseTrue
NOTrueFalse
NOFalseFalse

Method behavior if the FileName parameter is a symbolic link:

Target ExistsFollowLinkMethod result
YESTrueTrue
YESFalseTrue
NOTrueFalse
NOFalseTrue

A simple example of how to use the C++ FileExists() method

FileExist Method returns a Boolean, we can check a file and set the result to a bool as below,

or we can directly set its result to a components value, for example we can use it with a CheckBox (TCheckBox) component as below

A more complete example of how to use the C++ FileExists() method

The most used way is the checking file existence by if clauses and doing actions in that way, For example, here is a full example that checks two different file existence.

This method can be used with other component properties like FileName property of OpenDialog, SaveDialog components. For example you can let user browse a file and if that file doesn’t exist then create a new file. Or you can use them to select a folder then you can check your file if it exist or not in that folder. Remember that there is a drive check Note that this method can be used as same as examples above with FMX applications too.

As same as here above, there are a DirectoryExists() method to check directories and FileExists() method to check files. More examples about Path Operations can be found here

Here is the full example about using DriveExists(), DirectoryExists() and FileExists() Methods,

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition for students, beginners, and startups.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?