Solved

valid filename

Posted on 2001-08-28
9
389 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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