Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

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

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
jiekuan
Asked:
jiekuan
2 Solutions
 
TheLeaderCommented:
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
 
StayGreedyCommented:
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
 
geobulCommented:
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
 
StayGreedyCommented:
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
 
robert_marquardtCommented:
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

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now