Solved

How to determine if a filename is valid?

Posted on 1997-09-02
4
246 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

825 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