Solved

valid filename

Posted on 2001-08-28
9
379 Views
Last Modified: 2010-04-06
Does anyone have some code that is robust in getting input from an edit component and checking that it is a valid filename?

I currently use a routine that tries to create a file and if that creates an error then i assume it is an invalid filename but this is not the best way to do it as it could be because of another error like disk full, etc.
0
Comment
Question by:foxjax
  • 4
  • 4
9 Comments
 
LVL 1

Expert Comment

by:ntony
ID: 6435225
try
FileExists(FileName)
0
 

Author Comment

by:foxjax
ID: 6435253
Thanks ntony but i am looking more for a routine that will take what is typed into an edit box and check to see if it is valid - ie: does not contain any invalid characters that are generally not allowed aspart of the filename.
0
 
LVL 1

Expert Comment

by:ntony
ID: 6435271
In OnKeyPress event you coul do this


if not (Key in ['A'..'Z',#8]) then
Key:=#0

with this you alow only letters to be tiped into EditBox


this is only a start!!
0
 

Author Comment

by:foxjax
ID: 6435296
thanks ntony.  the main problem is that this allows only letters, and i acknowledge you said it was "only a start" so thanks for the help.

I'm not 100% with delphi as yet myself to be honest but could i use POS to check the filename entered against a list of characters?  If so, how would i do this?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:ntony
ID: 6435306
pos(substr,str) return the first position of substr in str

if you want to know all the position you must do something like this

for i:=0 to length(Edit.Text)-1 do begin
   if copy(Edit.Text,i,1) in ['A'..'Z'] then begin
      //you methods
   end;
end;

copy(Edit.Text,i,1) return the character with the position i in the Edit.Text
0
 

Author Comment

by:foxjax
ID: 6435335
Thanks ntony but i cannot get that code to work.  The characters i am looking to check for are \/:*?"<>| and if i find a character in the edit.text then i show a message (showmessage).

0
 
LVL 1

Accepted Solution

by:
ntony earned 100 total points
ID: 6435345
if (pos('\',Edit.Text)<>0)or(pos('/',Edit.Text)<>0)or(pos('.',Edit.Text)<>0)or(pos('*',Edit.Text)<>0)or(pos('?',Edit.Text)<>0)or(pos('"',Edit.Text)<>0)(pos('<',Edit.Text)<>0)or(pos('>',Edit.Text)<>0)or(pos('|',Edit.Text)<>0) then begin
   MessageDlg('Error',mtError,[mbOk],0);
end;

pos(substr,str)<>0  => the str string doesn't contains the substr string
0
 

Author Comment

by:foxjax
ID: 6435383
Thanks ntony - that seems to have done the trick.  the points are yours.  thanks for the help.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6435388
Try the "IsValidFilename" function below.  I didn't test it exhaustively, but let me know how it works for you.

Note that this allows wildcards in the "filename part" of the filename.  If you don't want to allow wildcards, just change the declaration of FileChars to

FileChars  = AllChars - IllegalChars - IllegalFileChars - Wildcards;


-----------------------------------------------------



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


// *************************************

function IsValidFilename(Filename :string) :boolean;
type
    TCharset = set of char;
const
    AllChars  = [#0..#255];
    Letters  = ['a'..'z', 'A'..'Z'];
    DriveChars  = Letters + [':'];
    IllegalChars  = ['"', '<', '>', '|'];
    IllegalFileChars  = ['\', '/'];
    WildCards  = ['*', '?'];
    PathChars  = AllChars - IllegalChars - WildCards;
    FileChars  = AllChars - IllegalChars - IllegalFileChars;

    function Illegal(s :string; c :TCharset) :boolean;
    var
        i :integer;
    begin
        Result := false;
        i := 1;
        while (not Result) and (i <= Length(s)) do begin
            Result := not (s[i] in c);
            Inc(i);
        end;
    end;

begin

    Result := not (Illegal(ExtractFileDrive(FileName), DriveChars))
              and not (Illegal(ExtractFilePath(Filename), PathChars))
              and not (Illegal(ExtractFileName(Filename), FileChars));

end;


//*********************************




procedure TForm1.Button1Click(Sender: TObject);
begin
    if IsValidFilename(Edit1.Text) then
        ShowMessage('valid')
    else
        ShowMessage('invalid');
end;

end.

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

19 Experts available now in Live!

Get 1:1 Help Now