Solved

How to determine if a filename is valid?

Posted on 1997-09-02
4
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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