Function for checking correct file name

classic Classic list List threaded Threaded
32 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Function for checking correct file name

Krzysztof
Hi,

Exists any multiplatform function which check if string contains not
allowed characters (like < > / \ on windows) for filename?

Regards
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

leledumbo
Administrator
Nope AFAIR. Just build one yourself, it's easy.
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Graeme Geldenhuys-3
In reply to this post by Krzysztof
On 12/09/12 19:51, Krzysztof wrote:
> Hi,
>
> Exists any multiplatform function which check if string contains not
> allowed characters (like < > / \ on windows) for filename?


Nothing in FPC as far as I know, but once again tiOPF does provide that.
Have a look at the tiUtils.pas unit in tiOPF v2. tiOPF is hosted on
SourceForge.net.  You can browse the repository code online with a web
browser if you must.

The function you are looking for is tiIsFileNameValid()

I had a quick look. It can be copied and pasted (with one helper
function). There are no other tiOPF dependencies.


Regards,
   - Graeme -

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Krzysztof
I just quicky googled not allowed characters and wrote this simple function:

function IsValidFilename(const AFilename: String): Boolean;
var
  c: set of Char = ['<','>',':','"','/','\','|','?','*', '%', '.'];
  i: Integer;
begin
  Result := True;
  for i:=1 to Length(AFilename) do
    if AFilename[i] in c then
      Exit(False);
end;

So, problem solved. Thanks and regards
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Mark Morgan Lloyd-5
Krzysztof wrote:

> I just quicky googled not allowed characters and wrote this simple function:
>
> function IsValidFilename(const AFilename: String): Boolean;
> var
>   c: set of Char = ['<','>',':','"','/','\','|','?','*', '%', '.'];
>   i: Integer;
> begin
>   Result := True;
>   for i:=1 to Length(AFilename) do
>     if AFilename[i] in c then
>       Exit(False);
> end;
>
> So, problem solved. Thanks and regards

But several of those are fine in filenames- just inadvisable since they
need special quoting/escaping to get past the shell. As does ' ' which
isn't in your list :-)

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Mattias Gaertner

Mark Morgan Lloyd <[hidden email]> hat am 14. September 2012 um 15:18 geschrieben:
> Krzysztof wrote:
> > I just quicky googled not allowed characters and wrote this simple function:
> >
> > function IsValidFilename(const AFilename: String): Boolean;
> > var
> > c: set of Char = ['<','>',':','"','/','\','|','?','*', '%', '.'];
> > i: Integer;
> > begin
> > Result := True;
> > for i:=1 to Length(AFilename) do
> > if AFilename[i] in c then
> > Exit(False);
> > end;
> >
> > So, problem solved. Thanks and regards
>
> But several of those are fine in filenames- just inadvisable since they
> need special quoting/escaping to get past the shell. As does ' ' which
> isn't in your list :-)

And it does not check for #0..#31, #127.
 
 
Mattias
 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Howard Page-Clark
On 14/9/12 2:43, Mattias Gaertner wrote:

>  > > c: set of Char = ['<','>',':','"','/','\','|','?','*', '%', '.'];

>  > But several of those are fine in filenames- just inadvisable since they
>  > need special quoting/escaping to get past the shell. As does ' ' which
>  > isn't in your list :-)
>
> And it does not check for #0..#31, #127.

As is so often the case, a truly cross-platform solution requires a good
deal more care and research (and cross-platform experience) than simply
extending a Windows-only solution.

Howard

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Jürgen Hestermann
In reply to this post by Krzysztof

Am 2012-09-12 20:51, schrieb Krzysztof:
> Exists any multiplatform function which check if string contains not
> allowed characters (like < > / \ on windows) for filename?
>
The question is: Do you realy need it?

I came to the conclusion that it's very hard to determine what exactly is a valid file name
and that it is much easier, safer, platform independend and more future proof to just try
creating a file with the name. If it fails, you can check the error result for the reason.

And even if the file name is valid it does not prevent you from error checking because
other obstacles like missing rights can also prevent you from using the file(path) name.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Graeme Geldenhuys-3
In reply to this post by Krzysztof
On 14/09/12 13:48, Krzysztof wrote:
> I just quicky googled not allowed characters and wrote this simple function:

Also in Windows (as far as I know), file names my not be longer than 255
characters. So you will have to check the length of AFilename parameter too.


Regards,
   - Graeme -

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Graeme Geldenhuys-3
In reply to this post by Howard Page-Clark
On 14/09/12 15:02, Howard Page-Clark wrote:
>
> As is so often the case, a truly cross-platform solution requires a good...

Under linux such a check is easy. Everything is accepted, expect for the
NUL character.  :-)


Regards,
   - Graeme -

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Jonas Maebe-2

On 14 Sep 2012, at 17:47, Graeme Geldenhuys wrote:

> On 14/09/12 15:02, Howard Page-Clark wrote:
>>
>> As is so often the case, a truly cross-platform solution requires a good...
>
> Under linux such a check is easy. Everything is accepted, expect for the NUL character.  :-)

And the "/".


Jonas_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Graeme Geldenhuys-3
On 14/09/12 16:50, Jonas Maebe wrote:
>
> And the "/".

Ah yes, you are correct.


Regards,
   - Graeme -

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Krzysztof
In reply to this post by Jonas Maebe-2
So I for sure check if file exists after creating it. Thanks for that tips.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Vinzent Höfler
In reply to this post by Krzysztof
On Fri, 14 Sep 2012 14:48:08 +0200, Krzysztof <[hidden email]> wrote:

> I just quicky googled not allowed characters and wrote this simple  
> function:

Seems, you found one of the many (I'd suspect) wrong search results.

> function IsValidFilename(const AFilename: String): Boolean;
> var
>   c: set of Char = ['<','>',':','"','/','\','|','?','*', '%', '.'];

Really?

|>copy con "a.b.c.%def"
|test
|^Z
|        1 Datei(en) kopiert.
|
|>type "a.b.c.%def"
|test
|
|>dir a.*
|[...]
|
|14.09.2012  18:08                 6 a.b.c.%def

And, actually, disallowing the '.' in a filename is really stup^Wshort-
sighted. Most files out there in the wild contain at least one.


Vinzent.

--
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents.
     -- Nathaniel Borenstein
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Jorge Aldo G. de F. Junior
am i the only one seeing a security risk here ?

2012/9/14 Vinzent Höfler <[hidden email]>:

> On Fri, 14 Sep 2012 14:48:08 +0200, Krzysztof <[hidden email]> wrote:
>
>> I just quicky googled not allowed characters and wrote this simple
>> function:
>
>
> Seems, you found one of the many (I'd suspect) wrong search results.
>
>
>> function IsValidFilename(const AFilename: String): Boolean;
>> var
>>   c: set of Char = ['<','>',':','"','/','\','|','?','*', '%', '.'];
>
>
> Really?
>
> |>copy con "a.b.c.%def"
> |test
> |^Z
> |        1 Datei(en) kopiert.
> |
> |>type "a.b.c.%def"
> |test
> |
> |>dir a.*
> |[...]
> |
> |14.09.2012  18:08                 6 a.b.c.%def
>
> And, actually, disallowing the '.' in a filename is really stup^Wshort-
> sighted. Most files out there in the wild contain at least one.
>
>
> Vinzent.
>
> --
> The most likely way for the world to be destroyed, most experts agree,
> is by accident. That's where we come in; we're computer professionals.
> We cause accidents.
>     -- Nathaniel Borenstein
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Vinzent Höfler
On Fri, 14 Sep 2012 18:42:55 +0200, Jorge Aldo G. de F. Junior  
<[hidden email]> wrote:

> am i the only one seeing a security risk here ?

*scratching head* Which part? Care to elaborate?

I'd presume, you mean another than that I'm using Windows? ;)

I don't see how manually restricting the character set of an application-
created file name can be a security risk per se.


Vinzent.

--
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents.
     -- Nathaniel Borenstein
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Marco van de Voort
In reply to this post by Graeme Geldenhuys-3
In our previous episode, Graeme Geldenhuys said:
> > As is so often the case, a truly cross-platform solution requires a good...
>
> Under linux such a check is easy. Everything is accepted, expect for the
> NUL character.  :-)

Which paths are you talking about? Paths passed to the shell or to the OS?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Krzysztof
In reply to this post by Jorge Aldo G. de F. Junior
I need only basic checking, it not need to be 100% compability with OS
specification. My program asking user for playlist name and I'm saving
it in local folder with my own extension.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Vinzent Höfler
In reply to this post by Jorge Aldo G. de F. Junior
On Fri, 14 Sep 2012 18:42:55 +0200, Jorge Aldo G. de F. Junior  
<[hidden email]> wrote:

> am i the only one seeing a security risk here ?

Now it dawns. You're thinking environment variables ("%")?


Vinzent.

--
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents.
     -- Nathaniel Borenstein
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Function for checking correct file name

Vinzent Höfler
In reply to this post by Krzysztof
On Fri, 14 Sep 2012 18:53:53 +0200, Krzysztof <[hidden email]> wrote:

> I need only basic checking, it not need to be 100% compability with OS
> specification. My program asking user for playlist name and I'm saving
> it in local folder with my own extension.

Then for von Neumanns sake simply try to save it where the user asks you to
and if any error occurs, tell the user.

I mean, there are hundreds of reasons why a file write may fail, why bother
with trying to prevent just one of them? Especially because this hugely
differs between different platforms.

You still have to do error handling anyway, and IMO the user is entitled to
name his own files any way he likes - as long as the operating system  
accepts
it. But after all, the choice of operating system was his, so the blame  
goes
to someone else, not you who prevented the user to create a playlist called
"100% Ginuwine" (yes, I googled).


Vinzent.

--
The most likely way for the world to be destroyed, most experts agree,
is by accident. That's where we come in; we're computer professionals.
We cause accidents.
     -- Nathaniel Borenstein
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12