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

x
?
Solved

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

Posted on 2003-11-26
5
Medium Priority
?
655 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
[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
5 Comments
 
LVL 2

Accepted Solution

by:
TheLeader earned 300 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 80 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

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

670 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