Solved

How to determine if a filename is valid?

Posted on 1997-09-02
4
244 Views
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. :-)
0
Comment
Question by:wolfcrag
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 25 total points
Comment Utility
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;

begin
 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;
  inc(i);
 end;
end;
 
['\','/',':','?','"','<','>','|'] = 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;
0
 
LVL 3

Author Comment

by:wolfcrag
Comment Utility
ZifNab,

    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.
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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;
0
 
LVL 3

Author Comment

by:wolfcrag
Comment Utility
ZifNab,

   That's a rare attribute you have there, ZifNab. Well, not so rare for a Delphi user. ;-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now