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.
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.
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!!
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!!
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?
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(F ilename), PathChars))
and not (Illegal(ExtractFileName(F ilename), FileChars));
end;
//************************ *********
procedure TForm1.Button1Click(Sender : TObject);
begin
if IsValidFilename(Edit1.Text ) then
ShowMessage('valid')
else
ShowMessage('invalid');
end;
end.
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(
and not (Illegal(ExtractFilePath(F
and not (Illegal(ExtractFileName(F
end;
//************************
procedure TForm1.Button1Click(Sender
begin
if IsValidFilename(Edit1.Text
ShowMessage('valid')
else
ShowMessage('invalid');
end;
end.
FileExists(FileName)