Solved

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

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

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
Process Javascript errors with Delphi TWebBrowser 1 198
Find and Replace Stream with 0s 8 71
DBCtrlGrid, Delphi, Scroll 7 32
IP without any Dots 1 49
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 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