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

How To Check Characters of File Names and Paths on Windows

How can I check a file name to see if it has extension or not ? How can I check whether a given file name has an extension part? How can I check whether a given file name contains only allowed characters? How can I check whether a given path string contains only allowed characters? What is HasExtension? What is HasValidFileNameChars? What is HasValidPathChars?

C++ Builder has a lot of specific methods in its SysUtils library included in VCL and FMX libraries. Some of these are grouped as Path Manipulation Routines that allows users to edit, extract, get and set drive name, directory name, file name and file extensions. These methods are combined in Vcl.FileCtrl, System.IOUtilsSystem.SysUtils libraries.

These methods are easy to use and easy to get or set file path strings in that operating system. They can be used with other component properties like the FileName property of OpenDialog, SaveDialog components. We can also check drives, files or directories to see if they are exist or not on a given path. You can also check file names and paths to see if it has a extension and has valid, allowable characters in it. For example, on Windows is it not permissible to create a file with a name which contains the “*” (asterisk) character since that is reserved by Windows as a wildcard. Files created with an “*” in their names could result in unexpected and possibly damaging behavior – for example trying to delete a file called “a*.txt” on the command line would actually delete all text files beginning with the letter “a”!

Let’s see how we can check file names and paths if they are valid for the Windows operating system.

What is the C++ HasExtension method ?

The HasExtension Method (System::SysUtils::HasExtension ) is a SysUtils Method that checks whether a given file name has an extension part. We can call HasExtension to check whether a given file name has an extension part. HasExtension returns true if the file name has an extension; false otherwise.

The following table lists the parameters expected by this method:

NameMeaning
PathThe verified file or directory name

Note: that HasExtension method raises an exception if the given path contains invalid characters.

What is the syntax of HasExtension method?

Here is the Syntax for the HasExtension Method,

What is the C++ HasValidFileNameChars method?

The HasValidFileNameChars Method (System::SysUtils::HasValidFileNameChars) is a SysUtils Method that checks whether a given file name contains only allowed characters. We can call HasValidFileNameChars to check whether a given file name contains only allowed characters. HasValidFileNameChars returns true if the string contains only allowed characters; false otherwise.

The following table lists the parameters expected by this method:

NameMeaning
PathThe verified file name string.
UseWildcardsSpecifies whether the wildcard characters are treated as valid file name characters (e.g. when true we can include asterisk or question marks in the filename although we should not create files containing these characters). With this argument set to True we can know that a filepath such as “C:\myfile\alltextfiles\a*.txt” is valid which we can then use safely as a mask value in the OpenFile dialog functions.

What is the syntax of HasValidFileNameChars method ?

Here is the Syntax for the HasValidFileNameChars

What is the C++ HasValidPathChars method ?

The HasValidPathChars Method (System.SysUtils.ExcludeTrailingBackslash) is a SysUtils Method that checks whether a given path string contains only allowed characters. We can call HasValidPathChars to check whether the given path string contains only allowed characters. HasValidPathChars returns true if the string contains only allowed characters; false otherwise.

The following table lists the parameters expected by this method:

NameMeaning
PathThe verified path string.
UseWildcardsSpecifies whether the wildcard characters are treated as valid path characters (e.g. asterisk or question mark). See above.

What is the syntax of HasValidPathChars method ?

Here is the Syntax for the HasValidPathChars Method:

Here is a full example of how to use the C++ HasExtension, HasValidFileNameChars, HasValidPathChars methods

Here is the full C++ Builder VCL example,

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; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

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

How to Create a New Windows FMX Static Library In C++

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

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

ru_RURussian