How to check a string to see if it’s valid for use in creating a file name
-
In my old c++ days, I would use my old function below;
BOOL IsValidFilename(const char *szFilename) { if ( (!szFilename) || (!strlen(szFilename)) ) return (FALSE); if ( (strchr(szFilename, '.')) || (strchr(szFilename, '\\')) || (strchr(szFilename, '/')) || (strchr(szFilename, ':')) || (strchr(szFilename, '<')) || (strchr(szFilename, '>')) || (strchr(szFilename, '*')) || (strchr(szFilename, '?')) || (strchr(szFilename, '|')) || (strchr(szFilename, '\"')) ) return (FALSE); return (TRUE); }
I thought there might be a cleaner way to test a string before I used it for a file name.Programmer Glenn Earl Graham Austin, TX
-
In my old c++ days, I would use my old function below;
BOOL IsValidFilename(const char *szFilename) { if ( (!szFilename) || (!strlen(szFilename)) ) return (FALSE); if ( (strchr(szFilename, '.')) || (strchr(szFilename, '\\')) || (strchr(szFilename, '/')) || (strchr(szFilename, ':')) || (strchr(szFilename, '<')) || (strchr(szFilename, '>')) || (strchr(szFilename, '*')) || (strchr(szFilename, '?')) || (strchr(szFilename, '|')) || (strchr(szFilename, '\"')) ) return (FALSE); return (TRUE); }
I thought there might be a cleaner way to test a string before I used it for a file name.Programmer Glenn Earl Graham Austin, TX
Maybe slightly cleaner. The
Path.GetInvalidFileNameChars
should give you an array of invalid chars, which you can iterate and check
IndexOf
in your candidate file name.
"Once in Africa I lost the corkscrew and we were forced to live off food and water for weeks." - Ernest Hemingway My New Blog
-
In my old c++ days, I would use my old function below;
BOOL IsValidFilename(const char *szFilename) { if ( (!szFilename) || (!strlen(szFilename)) ) return (FALSE); if ( (strchr(szFilename, '.')) || (strchr(szFilename, '\\')) || (strchr(szFilename, '/')) || (strchr(szFilename, ':')) || (strchr(szFilename, '<')) || (strchr(szFilename, '>')) || (strchr(szFilename, '*')) || (strchr(szFilename, '?')) || (strchr(szFilename, '|')) || (strchr(szFilename, '\"')) ) return (FALSE); return (TRUE); }
I thought there might be a cleaner way to test a string before I used it for a file name.Programmer Glenn Earl Graham Austin, TX
I'd just try it and catch the exception if any. Let the OS/framework determine validity; it knows best.
-
Maybe slightly cleaner. The
Path.GetInvalidFileNameChars
should give you an array of invalid chars, which you can iterate and check
IndexOf
in your candidate file name.
"Once in Africa I lost the corkscrew and we were forced to live off food and water for weeks." - Ernest Hemingway My New Blog
Thanks here is the final code it works perfectly...
bool IsValidFilename(String ^fileName) { if ( String::IsNullOrEmpty( fileName ) == true ) return false; array^ invalidFileChar = System::IO::Path::GetInvalidFileNameChars(); for each ( Char ^i in invalidFileChar ) { if ( fileName->Contains(String::Format("{0}", i)) ) { String ^tmp = String::Format(L" '{0}' is invalid in a file name.\n", i); System::Windows::Forms::MessageBox::Show(gcnew String(tmp),gcnew String(L"Warning"), MessageBoxButtons::OK); return false; } } return true; }
Programmer Glenn Earl Graham Austin, TX
-
I'd just try it and catch the exception if any. Let the OS/framework determine validity; it knows best.
-
Thanks here is the final code it works perfectly...
bool IsValidFilename(String ^fileName) { if ( String::IsNullOrEmpty( fileName ) == true ) return false; array^ invalidFileChar = System::IO::Path::GetInvalidFileNameChars(); for each ( Char ^i in invalidFileChar ) { if ( fileName->Contains(String::Format("{0}", i)) ) { String ^tmp = String::Format(L" '{0}' is invalid in a file name.\n", i); System::Windows::Forms::MessageBox::Show(gcnew String(tmp),gcnew String(L"Warning"), MessageBoxButtons::OK); return false; } } return true; }
Programmer Glenn Earl Graham Austin, TX
That doesn't look like C# to me. :~
-
Absolutely, why check what the OS/framework will check anyway? That's wasteful. The custom check may be faulty, causing a false valid or invalid. A false valid will likely result in an exception anyway, so you've wasted the time on the extra check, plus the time writing the faulty check. I know I couldn't write a filename validity checking routine as fast and accurate as the built-in one. You don't know to what OSs the framework may be ported in future (I know, it's unlikely) and what their validity rules are. I could certainly see adding some additional checking, like ensuring the name doesn't include a SPACE, or is 8.3 or something, but other than that you're just wasting your time. This is a wheel best not reinvented.
-
In my old c++ days, I would use my old function below;
BOOL IsValidFilename(const char *szFilename) { if ( (!szFilename) || (!strlen(szFilename)) ) return (FALSE); if ( (strchr(szFilename, '.')) || (strchr(szFilename, '\\')) || (strchr(szFilename, '/')) || (strchr(szFilename, ':')) || (strchr(szFilename, '<')) || (strchr(szFilename, '>')) || (strchr(szFilename, '*')) || (strchr(szFilename, '?')) || (strchr(szFilename, '|')) || (strchr(szFilename, '\"')) ) return (FALSE); return (TRUE); }
I thought there might be a cleaner way to test a string before I used it for a file name.Programmer Glenn Earl Graham Austin, TX
Or a Regular Expression, matching any of the invalid characters.
-
Maybe slightly cleaner. The
Path.GetInvalidFileNameChars
should give you an array of invalid chars, which you can iterate and check
IndexOf
in your candidate file name.
"Once in Africa I lost the corkscrew and we were forced to live off food and water for weeks." - Ernest Hemingway My New Blog
String.IndexOfAny() seems appropriate here. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Or a Regular Expression, matching any of the invalid characters.
I like that idea. A regex over a path size string should be blindingly fast as well.
"Once in Africa I lost the corkscrew and we were forced to live off food and water for weeks." - Ernest Hemingway My New Blog