Solved

valid filename

Posted on 2001-08-28
9
390 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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