Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

valid filename

Posted on 2001-08-28
9
Medium Priority
?
397 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

722 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