Link to home
Start Free TrialLog in
Avatar of foxjax
foxjax

asked on

valid filename

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.
Avatar of ntony
ntony

try
FileExists(FileName)
Avatar of foxjax

ASKER

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.
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!!
Avatar of foxjax

ASKER

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?
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
Avatar of foxjax

ASKER

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).

ASKER CERTIFIED SOLUTION
Avatar of ntony
ntony

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of foxjax

ASKER

Thanks ntony - that seems to have done the trick.  the points are yours.  thanks for the help.
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.