Check file existence for files with no extension

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Check file existence for files with no extension

Post by Wanderer82 »

Hello

I'm trying to check whether a file without an extension exists in a directory or not. The function "FileExists" won't find the file if there is no extension. I try to do it like this:

Code: Select all

if(FileExists("c:\\users\\%username%\\appdata\\roaming\\microsoft\\signatures"))
Is my syntax wrong or is it just not possible to find such a file using "FileExists"?
User avatar
doublemax@work
Super wx Problem Solver
Super wx Problem Solver
Posts: 474
Joined: Wed Jul 29, 2020 6:06 pm
Location: NRW, Germany

Re: Check file existence for files with no extension

Post by doublemax@work »

Is that real code? The wxWidgets method would be called "wxFileExists".

Also, i don't think it will expand the %username% variable automatically. So are you sure the missing extension is the problem?
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check file existence for files with no extension

Post by Wanderer82 »

Hi Doublemax

You're right, this is a function I wrote myself which does the following:

Code: Select all

return access( Filename.c_str(), 0 ) == 0;
And you were right, it's the %username% part that is the problem. I wasn't sure whether I've already used this syntax somewhere else but it seems that this function doesn't work with it, others do. Now it's working.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Check file existence for files with no extension

Post by PB »

Funny thing is that something seemingly correct like

Code: Select all

bool TestIfFileExists(const wxString& fileName)
{
    wxFileName fn(fileName);

    if ( !fn.Normalize(wxPATH_NORM_ENV_VARS) )
    {
        wxLogError("Could not normalize file name '%s'", fileName);
        return false;
    }

    const bool exists = fn.FileExists();

    wxLogMessage("File '%s' %s.", fn.GetFullPath(), exists ? "exists" : "does not exist");
    return exists;
}

TestIfFileExists("c:\\users\\%username%\\appdata\\roaming\\microsoft\\signatures")
is not going to work either, as wxExpandEnvVars() considers those "%" to be escaped by preceding slash as explained in the wxPATH_NORM_ENV_VARS description. However, something like

Code: Select all

TestIfFileExists("%appdata%\\microsoft\\signatures")
should work, except the path is a folder, not a file.
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Check file existence for files with no extension

Post by Wanderer82 »

Hm, but I have another problem now. If there is no "signatures" file without extension but a folder, it also recognizes it as "file". And this is not what I want to have. So maybe I'll just check if the directory "signatures" exists because if it exists there can't be another file named "signatures" without an extension. And only if the directory doesn't exist I'll check if the file exists.

EDIT: This approach worked.
Post Reply