Solved

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

Posted on 2003-11-26
5
646 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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
creating threads in delphi 1 201
SUM 2 INTEGER ARRAYS INTO 1 10 117
Firemonkey webbrowser scrollbars ? 1 70
FMX TEdit KeyUp handler detecting  "enter" key 4 62
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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