Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ensuring a valid path and file name?

27 views
Skip to first unread message

Tony Toews

unread,
Dec 24, 2012, 6:36:01 PM12/24/12
to
Folks

I have a vague memory of someone, possibly Karl P., posting an API
call to which you passed a Windows/Server path and, optionally, a file
name. The return code told you if the path and file was a valid and
didn't contain any invalide characters such as a forward "/" slash.

Any suggestions?

Tony

ralph

unread,
Dec 24, 2012, 8:24:37 PM12/24/12
to
FindFirstFile?

http://www.vbforums.com/showthread.php?349990-Classic-VB-How-can-I-check-if-a-file-exists

PathFileExists(), and PathIsDirectory()
http://www.vbforums.com/showthread.php?538510-How-to-check-if-a-file-path-is-valid

And note VB's Dir() and GetAttr() often do just as well.

-ralph

ralph

unread,
Dec 24, 2012, 8:34:38 PM12/24/12
to
On Mon, 24 Dec 2012 16:36:01 -0700, Tony Toews
<tto...@telusplanet.net> wrote:

Here is Karl's blurp on this, he suggests just using the Attributes
function. Which is what I do.

But thought I would check before I got caught quoting him incorrectly.
<g>

-ralph

ralph

unread,
Dec 24, 2012, 8:36:20 PM12/24/12
to
On Mon, 24 Dec 2012 19:34:38 -0600, ralph <nt_con...@yahoo.com>
wrote:


>Here is Karl's blurp on this, he suggests just using the Attributes
>function. Which is what I do.
>

Oops!
Forgot the URL:
http://visualstudiomagazine.com/articles/2009/09/22/finding-the-right-tool-for-the-job.aspx

Tony Toews

unread,
Dec 24, 2012, 8:44:48 PM12/24/12
to
On Mon, 24 Dec 2012 19:24:37 -0600, ralph <nt_con...@yahoo.com>
wrote:

>FindFirstFile?

My apologies. I wasn't clear enough in my posting. I want to ensure
that the string passed is a valid path and possibly file name. I
don't actually want to find the file or create it at this point in
time.

I'm going to store the string and use it days, weeks or months later
on other systems.

Tony

ralph

unread,
Dec 24, 2012, 9:43:17 PM12/24/12
to
ha. I wondered. A simple query returned all kinds of answers. I just
posted the one with more choices.

Have any problem using RegEx?
There are a zillion "valid character/path" patterns posted online.
Example:
http://www.regexlib.com/Search.aspx?k=file+&c=-1&m=-1&ps=20

-ralph

Tony Toews

unread,
Dec 24, 2012, 9:58:21 PM12/24/12
to
On Mon, 24 Dec 2012 20:43:17 -0600, ralph <nt_con...@yahoo.com>
wrote:

>ha. I wondered. A simple query returned all kinds of answers. I just
>posted the one with more choices.

Yes but then there's the tried and true best way, or ways, as
determined by, sometimes vigourous <smile>, discussion in this
newsgroup. I don't trust web pages for anything serious unless I know
of the person who wrote the page.

>Have any problem using RegEx?

It was some kinda API call so the OS did the path and file validation.

Tony

ralph

unread,
Dec 24, 2012, 11:35:53 PM12/24/12
to
I was looking for the same thing.

I know .Net has a good assortment of exactly what your looking for. So
I figured there was likely an API call, as much of .System is just a
wrapper for the API, but came up blank.

Hopefully someone else will come up with it.

I would absolutely trust anything found at RegExlib.com. A thousand
programmers have tested those patterns. <g>

Outside of .Net (and a few Unix and data libs) everyone pretty much
uses RegEx when it comes to validating characters or formats over
rolling their own. Mainly because once you have established a pattern
- essentially defining the business rule - you can use Regular
Expressions any and everywhere with any language or tool.

-ralph

CoderX

unread,
Dec 25, 2012, 1:26:07 PM12/25/12
to
I've always found this site to be incredibly helpful: http://txt2re.com



"ralph" <nt_con...@yahoo.com> wrote in message
news:4r9id89d79sb8o1d9...@4ax.com...

Tony Toews

unread,
Dec 25, 2012, 1:58:02 PM12/25/12
to
On Tue, 25 Dec 2012 13:26:07 -0500, "CoderX" <co...@x.com> wrote:

>I've always found this site to be incredibly helpful: http://txt2re.com

Wow.

Tony

ralph

unread,
Dec 25, 2012, 2:59:34 PM12/25/12
to
There are multiple free-standing Regular Expression generators
availble (many free).

They are invaluable.

The resulting pattern will still look like goobly-gook, but you will
feel like a guru. <g>

-ralph

Thorsten Albers

unread,
Dec 25, 2012, 3:09:30 PM12/25/12
to
Tony Toews <tto...@telusplanet.net> schrieb im Beitrag
<2jphd8lr3csdoa679...@4ax.com>...
> I have a vague memory of someone, possibly Karl P., posting an API
> call to which you passed a Windows/Server path and, optionally, a file
> name.

It is very likely that it is a procedure expored by shlwapi.dll. Check the
exports of this library and you presumable will find what you are searching
for.

> The return code told you if the path and file was a valid and
> didn't contain any invalide characters such as a forward "/" slash.

The forward "/" slash isn't an invalid character but an alternative for the
path character "\".

--
Thorsten Albers

gudea at gmx.de

ralph

unread,
Dec 25, 2012, 3:38:15 PM12/25/12
to

Tony Toews

unread,
Dec 25, 2012, 5:40:04 PM12/25/12
to
On Tue, 25 Dec 2012 13:59:34 -0600, ralph <nt_con...@yahoo.com>
wrote:

>There are multiple free-standing Regular Expression generators
>availble (many free).
>
>They are invaluable.
>
>The resulting pattern will still look like goobly-gook, but you will
>feel like a guru. <g>

Yeah, but if I don't understand them then I don't trust them. <smile>

Tony

Larry Serflaten

unread,
Dec 25, 2012, 7:10:04 PM12/25/12
to
If you find no API, how about using Like to test for invalid characters as in:

IsValidFilename = Not (FileName Like "*[*?""<>|[" Or FileName Like "*]*")

LFS

Larry Serflaten

unread,
Dec 25, 2012, 7:13:59 PM12/25/12
to
> IsValidFilename = Not (FileName Like "*[*?""<>|[" Or FileName Like "*]*")

Oops "*[*?""<>|[*" should be "*[*?""<>|]*"

The right bracket got turned around. You can't test the right bracket
in a grouping, which is why it was tacked on in an individual test by itself.

LFS

Mayayana

unread,
Dec 25, 2012, 8:35:17 PM12/25/12
to

| >The resulting pattern will still look like goobly-gook, but you will
| >feel like a guru. <g>
|
| Yeah, but if I don't understand them then I don't trust them. <smile>
|

I feel the same way. It feels reckless, not knowing
what you don't know.

Presumably you need to test
these paths at a time when a file can't be written to
disk as a test. Wouldn't it be enough to just test the
first section (disk/server) and then test the rest of the
line for no illegal characters? XP tells me what they are
by refusing to print them: *<>?/|":
(And \ is prohibited in a file or folder name.)


ralph

unread,
Dec 25, 2012, 9:41:02 PM12/25/12
to
On Tue, 25 Dec 2012 20:35:17 -0500, "Mayayana"
<maya...@invalid.nospam> wrote:

>
>| >The resulting pattern will still look like goobly-gook, but you will
>| >feel like a guru. <g>
>|
>| Yeah, but if I don't understand them then I don't trust them. <smile>
>|
>
> I feel the same way. It feels reckless, not knowing
>what you don't know.
>

Luckily I don't suffer from that particular limitation, else I'd still
be writing Bourne Shell utilities. <g> Besides, to paraphrase Mark
Twain - It isn't what you don't know that causes the biggest problems,
it is what you do know that just ain't so.

Regular Expressions are not anything knew - they predate Windows by a
century or so (in computer years) - and are probably as ubiquitous,
well-tested, and proven as any computer technology can get.

However, judging by the type of applications the OP has posted about
before, RegExp may not be appropriate, nor perhaps worth the time it
takes to 'understand' them.

I think Thorsten made a good suggestion. I forgot to look at the
Windows Shell. Dop! Unfortunately I wonder about availablility - it
looks like many of the shell routines that would be of the most direct
use are recent additions.

-ralph

Mayayana

unread,
Dec 26, 2012, 10:54:55 AM12/26/12
to

| > I feel the same way. It feels reckless, not knowing
| >what you don't know.
| >
| Luckily I don't suffer from that particular limitation,

Lucky... for you? You might want to bring a length of
rope with you today if you intend to "confidently" go
for a pleasant walk across the local pond. Mr. Twain's
wit won't help you if you fall through.

| Besides, to paraphrase Mark
| Twain - It isn't what you don't know that causes the biggest problems,
| it is what you do know that just ain't so.
|

| I think Thorsten made a good suggestion.

Except that he didn't actually check to see if it was
a good suggestion. I looked up SH* methods but haven't
found anything that looks promising. Thinking about it,
I don't see why there would be such a method. Generally
people want to know whether a path exists. Deciding
whether a path string is valid is just a syntax question.


ralph

unread,
Dec 26, 2012, 1:48:28 PM12/26/12
to
On Wed, 26 Dec 2012 10:54:55 -0500, "Mayayana"
<maya...@invalid.nospam> wrote:

>
>| > I feel the same way. It feels reckless, not knowing
>| >what you don't know.
>| >
>| Luckily I don't suffer from that particular limitation,
>
> Lucky... for you? You might want to bring a length of
>rope with you today if you intend to "confidently" go
>for a pleasant walk across the local pond. Mr. Twain's
>wit won't help you if you fall through.
>

I've fallen in so often I never go anywhere without a towel.

But come on, it's all virtual. It's not real.

>| Besides, to paraphrase Mark
>| Twain - It isn't what you don't know that causes the biggest problems,
>| it is what you do know that just ain't so.
>|
>
>| I think Thorsten made a good suggestion.
>
> Except that he didn't actually check to see if it was
>a good suggestion. I looked up SH* methods but haven't
>found anything that looks promising. Thinking about it,
>I don't see why there would be such a method. Generally
>people want to know whether a path exists. Deciding
>whether a path string is valid is just a syntax question.
>

I couldn't find anything either. I thought perhaps there might be some
subtle 'switch' or return ... but I'm not clever enough to puzzle out
any reliable solution.

I can see that within a 'Try and Catch' model (eg, VB's SEH) extensive
validation up front might not seem too important. One simply accepts
the data and boldly goes forward (with or without full 'understanding'
<g>). However, in a database scenario where the data is separated from
where it is collected, and where, when, and across what it is 'Tried',
validation becomes very important.

[For example, compare machine-processing time against hitting the wire
and waiting for a resource to complain. Or catching a problem during
entry, when it can be most easily modify, against puzzling out what
went wrong with Sunday night's 3am build.]

It also depends on the development environment. With a single
programmer/project, the programmer is often the SME - intimately
familiar with any imports and eventual uses - thus can make broad
assumptions and worry little about limited, almost trivial,
validation.

Whereas, in a multiple programmer, multiple project, distributed,
shared environment, validation becomes more important, and having a
consistent, reliable, known pattern or 'API' to define the BR's is
very useful. Compared to allowing programmers to roll-their-own
routines here and there that have to be ferreted out and re-tested
with any changes.

I can't really say I missed any of the new validation routines found
in Fred, but I do know I abused them extensively now that they are
available. <g>

-ralph

Mayayana

unread,
Dec 26, 2012, 3:15:13 PM12/26/12
to
| > Lucky... for you? You might want to bring a length of
| >rope with you today if you intend to "confidently" go
| >for a pleasant walk across the local pond. Mr. Twain's
| >wit won't help you if you fall through.
| >
|
| I've fallen in so often I never go anywhere without a towel.
|

Then I applaud your courage. That's certainly one way
to do things.

| But come on, it's all virtual. It's not real.
|

Maybe so. But the water feels damned cold just the same.
And I'm guesing that death by drowning is probably an
especially compelling illusion.

There's a famous Hindu story... Christmas seems a nice
time for inspiring Hindu stories... where a disciple has a
revelation of God and realizes that All Is God. He's blissful
and at peace in recognizing that nothing could ever go wrong
because All Is God. At that point a man is coming down the
road on an elephant, shouting to the disciple to get out
of the road. But the disciple feels confident in his new
realization: The elephant is God, I'm God, the road is God...
All is God. Nothing can go wrong.
As the elephant charges by it reaches out with its trunk
and flings the disciple into a ditch. The disciple, dissheveled,
bruised and confused, goes to his guru and says, "I realized
that I am God, the elephant is God...that All is God. I don't
understand what went wrong." The guru replies, "Why didn't
you listen to God when he shouted to you to get out of
the road?"


ralph

unread,
Dec 26, 2012, 4:16:28 PM12/26/12
to
The Baptist have a similar story, only it is a man on a rooftop during
a flood. Of course, Aesop likely predates them all. <g>

But I feel you are missing the point. The OP was baulking at using
well-known, throughly tested, widely-used, published regular
expression patterns because he didn't fully understand what all those
strange characters in an odd format were doing.

I can feel his pain. It is complex subject and takes a bit of getting
used to - and some never quite grasp it all. (There is a reason why I
quickly abandoned sed (vi) and grep and jumped to Emacs and "keystroke
macros". <g>)

-ralph

GS

unread,
Dec 26, 2012, 5:15:50 PM12/26/12
to
Tony Toews formulated the question :
Have you tried filtering for invalid chars so that if any exist they're
removed?

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


Tony Toews

unread,
Dec 26, 2012, 6:40:38 PM12/26/12
to
On Wed, 26 Dec 2012 15:16:28 -0600, ralph <nt_con...@yahoo.com>
wrote:

>Aesop likely predates them all. <g>

Hehehehe

>But I feel you are missing the point. The OP was baulking at using
>well-known, throughly tested, widely-used, published regular
>expression patterns because he didn't fully understand what all those
>strange characters in an odd format were doing.

>I can feel his pain. It is complex subject and takes a bit of getting
>used to - and some never quite grasp it all. (There is a reason why I
>quickly abandoned sed (vi) and grep and jumped to Emacs and "keystroke
>macros". <g>)

That and I know there's an API call. What if MS changes the spec in
the future? Which is highly unlikely now after 20 or 30 years
experience with FAT and NTFS but still....

Tony

Mayayana

unread,
Dec 26, 2012, 8:59:28 PM12/26/12
to

| But I feel you are missing the point. The OP was baulking at using
| well-known, throughly tested, widely-used, published regular
| expression patterns because he didn't fully understand what all those
| strange characters in an odd format were doing.
|
| I can feel his pain. It is complex subject and takes a bit of getting
| used to - and some never quite grasp it all. (There is a reason why I
| quickly abandoned sed (vi) and grep and jumped to Emacs and "keystroke
| macros". <g>)
|

I guess that's two points. One is whether to jump
into things one doesn't understand on the say-so of
others who *seem* to know what they're talking about,
without any expert advice on possible "thin ice".

The other is whether RegExp is worth the trouble. I
once decided to get into RegExp and ended up giving
up. I just couldn't seem to get the hang of writing the
code myself. I could only use pre-designed samples.
That seemed rather reckless. Even if I trust the source,
I don't know if they've covered all possible exceptions
to the rule in their RegExp. And if I don't actually understand
the code myself then I can't check. I suppose I could just
use pre-written samples when allelse fails, but if I really
needed RegExp I'd want to just sit down and figure it
all out. So far I haven't had that need. Maybe I'm
overlooking something, but I don't see any problem
with the following for the path after the drive notation:

For i = 1 to Len(TestPathAfterDrive)
Select Case Asc(Mid(TestPathAfterDrive, i, 1))
'-- " * / : < > ? |
Case 34, 42, 47, 58, 60, 62, 63, 124
PathInvalid = True
Exit For
Case Else
'--
End Select
Next

Meanwhile, no one has actually posted code for a
RegExp solution, and even if they do, what supports
it? I only know of the scripting objects in vbscript.dll
and jscript.dll. Or one can install Perl, I suppose.

When I enter the words: windows valid path
at regexlib.com I get no returns. The samples
there are mainly for simple checks of dates, phone numbers,
etc. Though I did find an interesting check for valid
Windows file names. I didn't know that some words are
off limits: PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT
I tried creating nul.txt and sure enough, it was rejected!
When I tried to do it with VBScript it failed with no
error. (As opposed to creating a .htaccess file. Windows
Explorer won't allow creation of a file starting with ".", but
it can be done programmatically and the resulting file is
valid.)


Jim Mack

unread,
Dec 26, 2012, 10:17:05 PM12/26/12
to
>>
>> I can feel his pain. It is complex subject and takes a bit of getting
>> used to - and some never quite grasp it all. (There is a reason why I
>> quickly abandoned sed (vi) and grep and jumped to Emacs and "keystroke
>> macros". <g>)
>>
>
> I guess that's two points. One is whether to jump
> into things one doesn't understand on the say-so of
> others who *seem* to know what they're talking about,
> without any expert advice on possible "thin ice".
>
> The other is whether RegExp is worth the trouble. I
> once decided to get into RegExp and ended up giving
> up. I just couldn't seem to get the hang of writing the
> code myself.

Famous quote by Jamie Zawinski:

'Some people, when confronted with a problem, think, "I know, I'll
use regular expressions." Now they have two problems.'

--
Jim


Mayayana

unread,
Dec 26, 2012, 10:39:59 PM12/26/12
to
| Famous quote by Jamie Zawinski:
|
| 'Some people, when confronted with a problem, think, "I know, I'll
| use regular expressions." Now they have two problems.'
|

:)
RegExp seems to be one of those things that people
either love or hate, like Perl.


Eduardo

unread,
Dec 30, 2012, 1:37:38 AM12/30/12
to

"Tony Toews" <tto...@telusplanet.net> escribi� en el mensaje
news:2jphd8lr3csdoa679...@4ax.com...
> Folks
>
> I have a vague memory of someone, possibly Karl P., posting an API
> call to which you passed a Windows/Server path and, optionally, a file
> name. The return code told you if the path and file was a valid and
> didn't contain any invalide characters such as a forward "/" slash.
>
> Any suggestions?

Hello, test IsValidPath from
http://firstchroniclesproductions.com/DEVELOP/REMOTE~1/SETUP1/SETUP1.BAS

Or http://www.google.com/search?q=vb+-.net+isvalidpath

Quoted from other post:

> I don't trust web pages for anything serious unless I know
> of the person who wrote the page.

Yes, but if you can see the code and understand what it does, you don't need
to trust.


Mayayana

unread,
Dec 30, 2012, 8:50:03 AM12/30/12
to
That function seems to be treating a space as invalid:

'-----------------------------------------
' Check for wildcard characters and spaces
'-----------------------------------------
If (InStr(DestPath$, "*") <> 0) GoTo parseErr
If (InStr(DestPath$, "?") <> 0) GoTo parseErr
If (InStr(DestPath$, " ") <> 0) GoTo parseErr


Eduardo

unread,
Dec 30, 2012, 6:30:55 PM12/30/12
to

"Mayayana" <maya...@invalid.nospam> escribió en el mensaje
news:kbpgp4$n9a$1...@dont-email.me...
Yes, that's an error. It must had been true for old MS-DOS FAT systems.

BTW, I found this:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx



Eduardo

unread,
Dec 30, 2012, 6:53:44 PM12/30/12
to

"Eduardo" <m...@mm.com> escribió en el mensaje
news:kbqira$hjm$1...@speranza.aioe.org...
>
> "Mayayana" <maya...@invalid.nospam> escribió en el mensaje
> news:kbpgp4$n9a$1...@dont-email.me...
>>| Hello, test IsValidPath from
>> |
>> http://firstchroniclesproductions.com/DEVELOP/REMOTE~1/SETUP1/SETUP1.BAS
>> |
>> That function seems to be treating a space as invalid:
>>
>> '-----------------------------------------
>> ' Check for wildcard characters and spaces
>> '-----------------------------------------
>> If (InStr(DestPath$, "*") <> 0) GoTo parseErr
>> If (InStr(DestPath$, "?") <> 0) GoTo parseErr
>> If (InStr(DestPath$, " ") <> 0) GoTo parseErr
>>
>
> Yes, that's an error. It must had been true for old MS-DOS FAT systems.

I'll express it better:

It positively had been true (the space not allowed) in old MS-DOS FAT
systems.
That function must had been coded for that naming convention.
It wasn't an error at that time, but it is now an error if one wants to
check a path for the current Windows's naming convention.

I didn't check all the function, so perhaps it's needed to also remove the 8
characters file name lenght limit and the three characters extension limit
(I don't remember other changes in the naming convention).

Tony Toews

unread,
Jan 1, 2013, 9:27:06 PM1/1/13
to
On Sun, 30 Dec 2012 20:30:55 -0300, "Eduardo" <m...@mm.com> wrote:

>BTW, I found this:
>http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx

Interesting reading. Thanks.

Gotta love the following weasel words after a list of reserved
characters.

"Any other character that the target file system does not allow."

Mind you I've used weasel words lots too. <smile>

Tony

Deanna Earley

unread,
Jan 2, 2013, 8:42:12 AM1/2/13
to
On 25/12/2012 01:44, Tony Toews wrote:
> On Mon, 24 Dec 2012 19:24:37 -0600, ralph <nt_con...@yahoo.com>
> wrote:
>
>> FindFirstFile?
>
> My apologies. I wasn't clear enough in my posting. I want to ensure
> that the string passed is a valid path and possibly file name. I
> don't actually want to find the file or create it at this point in
> time.
>
> I'm going to store the string and use it days, weeks or months later
> on other systems.

If on other systems then all bets are off. Beyond the basics (: < > | *
? " / \ taken from the error in Explorer) everything else is (file)
system dependant :)

--
Deanna Earley (dee.e...@icode.co.uk)
i-Catcher Development Team
http://www.icode.co.uk/icatcher/

iCode Systems

(Replies direct to my email address will be ignored. Please reply to the
group.)

Tony Toews

unread,
Jan 3, 2013, 1:44:57 PM1/3/13
to
On Wed, 02 Jan 2013 13:42:12 +0000, Deanna Earley
<dee.e...@icode.co.uk> wrote:

>> My apologies. I wasn't clear enough in my posting. I want to ensure
>> that the string passed is a valid path and possibly file name. I
>> don't actually want to find the file or create it at this point in
>> time.
>>
>> I'm going to store the string and use it days, weeks or months later
>> on other systems.
>
>If on other systems then all bets are off. Beyond the basics (: < > | *
>? " / \ taken from the error in Explorer) everything else is (file)
>system dependant :)

True, but this is for my utility which works with MS Access so if the
user is not using a Windows Server oh well.

Tony
0 new messages