Solved

How to check whether a input string is a valid filename in windows?

Posted on 2003-11-26
5
629 Views
Last Modified: 2010-04-05
more specified, how to check a string whether it is 80+1+3 ( filename + . + extension) format?

a filename contains alphabets, numbers, space, symbols (all symbols available on keyboard except \,/,:,*,?,”,<,>,| , . )

is there any function to check whether a CHARACTER is a number, alphabet?

thank you!!!
0
Comment
Question by:jiekuan
5 Comments
 
LVL 2

Accepted Solution

by:
TheLeader earned 75 total points
ID: 9829950
const
  { for short 8.3 file names }
  ShortForbiddenChars : set of Char = ['?',';', '=', '+', '<', '>', '|','"', '[', ']', '\', '/', ''''];
  { for long file names }
  LongForbiddenChars  : set of Char = ['<', '>', '|', '"', '\', '/'];

function TestFilename(Filename: String; islong: Boolean) : Boolean;
var
  I: integer;
begin
  Result := Filename <> '';
  if islong then
  begin
    for I := 1 to Length(Filename) do
      Result := Result and not (Filename[I] in LongForbiddenChars);
  end
  else
  begin
    for I := 1 to Length(Filename) do
      Result := Result and not (Filename[I] in ShortForbiddenChars);
  end;
end;

hope it helped ...
0
 
LVL 1

Expert Comment

by:StayGreedy
ID: 9830952
The Delphi Jedi Code Library has some good function, for example:

function CharIsNumber(const C: AnsiChar): Boolean;
function StrIsNumber(const S: AnsiString): Boolean;

The JCL contains hundreds of functions, and the JVCL contains over 400 components

You can find the Delphi Jedi Code Library as well as the JVCL at
Main Site: http://delphi-jedi.org/
JCL: http://homepages.borland.com/jedi/jcl/
JVCL: http://jvcl.sourceforge.net/

For a combine JCL and VCL goto:
http://prdownloads.sourceforge.net/jvcl/JCL+JVCL210FullInstall.zip?download

0
 
LVL 17

Assisted Solution

by:geobul
geobul earned 20 total points
ID: 9831267
Hi,

I suggest you a more radical way: try to create that file and if you can then the name is valid 100 %.

function ValidFilename(s:string):boolean;
var
  f:textfile;
begin
  if not fileexists(s) then begin
    Try
      Result:=true;              
      Assignfile(f,s);
      Rewrite(f);
      Close(f);
      Deletefile(s);
    except
      Result:=False;
    end;  
  end else
    Result:=true;// Valid name,but already exists
end;

Regards, Geo
0
 
LVL 1

Expert Comment

by:StayGreedy
ID: 9831303
Just looking at your question again,

if you want to extract pathname, filename and ext, then you can use the following functions:

function ExtractFilePath(const FileName: string): string;
function ExtractFileDir(const FileName: string): string;
function ExtractFileExt(const FileName: string): string;
function ExtractFileName(const FileName: string): string;
function ExtractShortPathName(const FileName: string): string; {Converts a file name to the short 8.3 form}
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 9831471
This question is not as easy as it sounds and i know no fully secure solution.
Keep in mind that "valid" is defined for file systems so a valid NTFS file name may not be valid for FAT.
NTFS allows several named streams in a file.
Not to mention the special DOS names like "prn:" or the driver file names starting with "\\.\" or the UNC file names.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now