• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 443
  • Last Modified:

determine if filename is valid

I have a tedit which the user enters a full path and filename to a file in

I need a function to determine

1.) that the path is a valid path

2.) that the file name is a valid fileName

of course, the file name could already exist or NOT

if not, then it must be a valid qualified filename

thanks

function ValidFileName(FileName: String): Boolean;
begin
 result:= false;
 //your code here
end;

best function wins points

thanks
0
DelphiRulez
Asked:
DelphiRulez
  • 5
  • 4
  • 3
  • +1
1 Solution
 
malachi223Commented:
Hi,

You can use the built in FileExists function:

function ValidFileName(FileName: String): Boolean;
begin
 result:= FileExists(FileName);
end;

This will only return true if the path is valid and the file exists.

Mike
0
 
epasquierCommented:
for FileName validation (assuming the path exists), you could use this
function ValidFileName(FileName: String): Boolean;
begin 
 Result:=FileExists(FileName);
 if Result Then Exit;
 try
  TFileStream.Create(FileName,fmCreate).Free;
  Result:=True;
  DeleteFile(FileName);
 except
 end;
end;

Open in new window

0
 
epasquierCommented:
@malachi223: The file could NOT exists, by valid DelphiRulez wants to know if the name is correct, i-e if a file could be created with that name (correct me if I'm wrong)

pb comes when the path does not exists, that's a lot more complex. Other than what I did, we would have to check if the SYNTAX of the file is valid. But what of drive checking, network path (UNC) ? This could be a lot of trouble, would be worth it ?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
epasquierCommented:
here is the function to validate a file path. It actually CREATES the path if it does not exists.
I uncomment the line to delete the path create after checking the possibility to do so, because if I let it like that, it will not work if it has to create 2 levels. That would need a more complex function to recursively create all directories along the path, remembering when it had a valid already existing directory, and after returning to the first level call, delete again all the directory recursively until it return to the original state

The ValidFileName function could also be adapted to use this to create the path if not existing

DelphiRulez : tell me what you need regarding these points.

function ValidFilePath(FilePath:String):boolean;
Var
 SubDir:String;
begin
 Result:=DirectoryExists(FilePath);
 if Result Then Exit;
 SubDir:=ExtractFilePath(ExcludeTrailingBackslash(FilePath));
 Result:=ValidFilePath(FilePath);
 if Not Result Then Exit;
 Result:=CreateDir(FilePath);
// if Result Then RmDir(FilePath);
end;

Open in new window

0
 
aikimarkCommented:
I would suggest a hybrid approach.

1. parse the string into the path value and the filename value.
2. look for path existence
   * If no path, then fail - or use http:#28999118 technique to attempt path creation.
3. use epasquier technique (http:#28996664) to validate filename.

===============
This isn't trivial, as there are some valid strings that shouldn't be used as file names, such as COM1, AUX, LPT1, etc.

===============
Here is more RegExp approach to the problem, looking for invalid characters.
http://www.delphifaq.com/faq/delphi/windows_file_system/f418.shtml
http://delphi.about.com/od/delphitips2009/qt//is-valid-file-name-delphi.htm

===============
@DelphiRulez

Are you using a Delphi.Net version?
0
 
DelphiRulezAuthor Commented:

Im using a Delphi 2010 flavor

the file and path are assumed that they do not exist

there are many factors to take into account

is the string a path, a file, or a path and file, or none of those

if its none, it should return false

if its just a path, it should return false

if its just a file, and its a valid file, (Doesn't have to exist, just has to have all the valid cahracters that make it a file), it should return true

if its a valid path and valid file, it shoud return true

else it should return false

0
 
aikimarkCommented:
Are you creating a .Net application with D2010 or Win32 application?  The .Net framework has some libraries that can validate a file name.
0
 
epasquierCommented:
> if its just a path, it should return false
you can't tell a string is a path and not a file just by its name (if it does not exists) OR it is just considering that there is the trailing backslash.

> if its none, it should return false
meaning :
a) there are invalid characters
b) or it's starting from a drive not present, or without write permissions (could not create a new sub dir or file if not there already)

anyway, what I provided will check all of what you ask (providing it makes sense and is possible). What I don't know is do you need to also check for UNC file locators (aka \\Server\NetDir\SomeSubDir), and is it acceptable for you to create the sub dir or do I have to adapt something a bit more complex to test creation and revert back after ?
0
 
DelphiRulezAuthor Commented:
>>you can't tell a string is a path and not a file just by its name (if it does not exists) OR it is just considering that there is the trailing backslash.

You can tell if the string is qualified to be a path, by it characters

>>>meaning :

its not a path, a file, or a path and file

No, i dont care about UNC paths

0
 
DelphiRulezAuthor Commented:
>>>anyway, what I provided will check all of what you ask (providing it makes sense and is possible).

I see too different functions doing two different things

i need one function returning a boolean

thanks
0
 
aikimarkCommented:
@DelphiRulez

What are you going to do if the path doesn't exist?
What are you going to do if the path can't exist?
0
 
epasquierCommented:
> You can tell if the string is qualified to be a path, by it characters
if it's qualified to be a file, it can be a path, and vice versa, with the only exception when it ends with a slash it has to be a path. Even directory can have extension.

All the functions provided where enough to check for path & file validity, with the inconvenient or feature (depending of what you really need) of directory creation.

here is all what you need for filename validity without directory or file creation (or to be exact, with the deletion of all that is created for the validity test purpose)
// delete all directory in reverse from a high level dir to it's base root
// all parent dir must be empty except for those child dir in the path
procedure DeletePathUntil(From,ToRoot:String);
begin
 From:=ExcludeTrailingBackslash(From);
 if From=ToRoot Then Exit;
 if RemoveDir(From) Then DeletePathUntil(ExtractFilePath(From),ToRoot);
end;

// will return the base existing path found
function ValidPath(Path:String;DeletePath:Boolean=True):String;
Var
 SubDir:String;
begin
 Path:=ExcludeTrailingBackslash(Path);
 if DirectoryExists(Path) Then
  begin
   Result:=Path;
   Exit;
  end;
 SubDir:=ExtractFilePath(Path);
// we use there the fact that path & file share the same syntax, as long
// as we cut the trailing slash, it returns the base path of this sub dir
 if Length(SubDir)>2 Then Result:=ValidPath(SubDir,False) Else Result:='';
 if Result='' Then Exit;
 if Not CreateDir(Path) Then
  begin
   DeletePathUntil(SubDir,Result);
   Result:='';
  end Else if DeletePath Then DeletePathUntil(Path,Result);
end;

function ValidFileName(FileName: String): Boolean;
Var
 Path,RootDir:String;
begin
 Result:=FileExists(FileName);
 if Result Then Exit;
 Path:=ExtractFilePath(FileName);
 RootDir:=ValidPath(Path,False);
 Result:=RootDir<>'';
 if Not Result Then Exit;
 try
  TFileStream.Create(FileName,fmCreate).Free;
  Result:=True;
  DeleteFile(FileName);
 except
 end;
 DeletePathUntil(Path,RootDir);
end;

Open in new window

0
 
DelphiRulezAuthor Commented:
Not 100% what i wanted, but good enough to hack and make what i want work.

THanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now