Solved

How to determine if a filename is valid?

Posted on 1997-09-02
4
245 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
ID: 1343396
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
ID: 1343397
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
ID: 1343398
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
ID: 1343399
ZifNab,

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 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

8 Experts available now in Live!

Get 1:1 Help Now