We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


How to determine if a filename is valid?

wolfcrag asked
Medium Priority
Last Modified: 2010-04-04
Hi, how do I determine if a string entered by the user is a valid filename? Forget the extension part (.*), just the first part, because I'll set the extension myself. The user just has to give the first part.

Yes, this is for Win95. I use Delphi3. I know of one way already, but it relies on an error being generated, and I would prefer a neater (maybe safer?) way.

If the answer you give me does not require trapping an error, and is correct, I'll give you an A. If it does require error trapping, and it's correct, I'll give you a C. If it's wrong, well, it's wrong. :-)
Watch Question

Hi wolfcrag,

Don't know if this is such a good solution, but it is one :

{function returns true if filename is correct}

function CheckFileName(Value:TFileName):Boolean;
var i : integer;

 Result := True;
 i := 0;

 { iterate through filename [Value] until : }
 {  Bad character : return false            }
 {  Or checked all characters : return true }

 while result and (i<>length(Value)) do begin
  if Value[i] in ['\','/',':','?','"','<','>','|'] then
   result := false;
['\','/',':','?','"','<','>','|'] = characters which may not be for a filename. Possibly you should add '.', because you give the extension yourself.
Hope I've helped you with this,
Have fun,
c.u. ZifNab;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts



    I'm giving you a B. Why? Well, you missed one: '*' I've decided that I will use your method but will protect it with the error trapping method. Thanks for your help.

Hi wolfcrag,

Well, you're correct, sorry I've missed that '*'. But I'm not really doing it for the points, I just want to help people.

Have fun,
c.u. ZifNab;



   That's a rare attribute you have there, ZifNab. Well, not so rare for a Delphi user. ;-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.