Page 1 of 2

GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 7:40 am
by PB
It's not right for GetFilePart() to ignore a forward slash, right?
Sending "/e" to it returns just "e" on Windows, which is wrong
because CheckFilename() on "/e" correctly returns 0.

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 9:11 am
by Danilo

Code: Select all

Debug FileSize("c:/pagefile.sys")
Debug FileSize("c:/windows/autochk.exe")
Debug FileSize("c:/windows/attrib.exe")

End

IncludeBinary "c:/windows/COMCTL32.ocx"

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 9:13 am
by STARGĂ…TE
CheckFilename is only for a file name, not a hole path!

Under Windows you can open files with / as well as with \

Code: Select all

Debug OpenFile(1, "C:/Example.txt")
so GetFilePart() works right

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 2:56 pm
by PB
Okay, here's the problem I have. I need to get the argument
of a commandline that was presented to me, and check if
the argument is a valid filename specification.

Run this code to see the problem. All checks do NOT return
the wanted argument of "/e" at all. What can I do?

Code: Select all

Procedure ShowArg_v1(arg$)
  If CheckFilename(arg$)=0
    Debug "0"
  Else
    Debug "Arg given is: "+GetFilePart(arg$)
  EndIf
EndProcedure

Procedure ShowArg_v2(arg$)
  arg$=GetFilePart(arg$)
  If CheckFilename(arg$)=0
    Debug "0"
  Else
    Debug "Arg given is: "+arg$
  EndIf
EndProcedure

spec$="c:\notepad.exe /e"

p=FindString(spec$,".exe")
wantarg$=LTrim(Mid(spec$,p+4))
Debug "Arg wanted is: "+wantarg$
Debug "-----------------"

ShowArg_v1(wantarg$) ; Incorrect!
ShowArg_v2(wantarg$) ; Incorrect!

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 3:04 pm
by luis
1) if you want to get the params specified for YOUR program use ProgramParameter()

2) if you don't want that, spec$="c:\notepad.exe /e" doesn't have nothing to do with GetFilePart(), it's not a well formed full path name, it's just a string.
Process the string and extract the parameters.

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 3:09 pm
by PB
> use ProgramParameter()

The strings/params are not my own. They're provided from a list.

> Process the string and extract the parameters

Yep, I've already extracted the params. One of the params is "/e".
So how do I tell if that param is a filename or not? I can't, because
it either returns 0 or "e", and both are not "/e" and thus incorrect.

I can't tell my user that "e" is their argument, when they know it's not.

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 3:18 pm
by PB
Here's a simpler version. Imagine you want to ask the user
for a filename to act on later. So, you do something like this:

Code: Select all

file$=InputRequester("test","Please type your filename:","/e")
ToBeDone$=GetFilePart(file$) ; Is now "e" instead of what they typed!
; Later down the track, the incorrect name is used in a report. BAD!

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 3:30 pm
by luis
So how do I tell if that param is a filename or not?
You should just explain what your problem is in coding questions, because to me it's not really clear what you want.
"/e" is a string. It's a filename only if you know it's a filename.
How can I know if it's a filename ? Could be "e" in root somewhere on someone's computer. Could be a param standing for "e"xcruciating_pain_in_the_ass.
Maybe you want to know if that string *could* be a filename, so if it's a formally valid file name.
That does not mean is a file, unless you know it must be.

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 3:36 pm
by PB
> Maybe you want to know if that string *could* be a filename,
> so if it's a formally valid file name

That's what I mean. I don't care if the file exists or not. All I need
to know, is if the string "/e" is a valid filename format. Currently,
CheckFilename() says no, and GetFilePart() strips it to just "e".
I can't win either way.

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 3:46 pm
by luis
Can't you just use GetFilePart$() and if its return is not empty test that (the file name) for validity with CheckFileName$() ?

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 4:37 pm
by PB
> Can't you just use GetFilePart$() and if its return is not empty
> test that (the file name) for validity with CheckFileName$() ?

No, because GetFilePart() returns "e" which means CheckFilename()
returns true. But it's not true, because the filename is not "e" at all.
It's "/e". ;)

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 4:59 pm
by Tenaja
It sounds to me like you are chasing your own tail. The simple string "e" IS a valid filename, and can be valid for a file. However, "/e" cannot be, because the "e" would denote a folder name....which of course, is not a file.

So, if your first char is a slash, then the first chars following it are a folder so the string must contain a second slash to start the file. That means it must be at least four characters long. You need to do more than just two command calls to get what you want. Sometimes it just works out that way.

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 5:04 pm
by luis
PB wrote:the filename is not "e" at all. It's "/e".
OK, let's stick to some nomenclature.
the filename is "e".
"/e" is the path + the filename, let's call it full pathname ?

If you want to be sure the full pathname is valid I don't think you can do it with the PB "path" commands because they don't really check for the validity for the current OS.

For example: Debug GetPathPart("/aaa/bbb/&*&*&/...*../abc.txt") gives "/aaa/bbb/&*&*&/...*../". It's ok, but it's not a valid path.

I think every OS offers some API in a form or another to check if the parts of a pathname are valid or not, maybe you could use that, or build your own check in PB code or using a regular expression.

EDIT: in Windows there are these http://msdn.microsoft.com/en-us/library ... 85%29.aspx but they seem to work mostly on the actual file system, not "in abstract".

Re: GetFilePart should ignore forward slash

Posted: Thu Feb 27, 2014 5:07 pm
by luis
Tenaja wrote:However, "/e" cannot be, because the "e" would denote a folder name....which of course, is not a file.
"/e" = file "e" in root OR maybe a folder, but for an open command only a file, can be a folder (if the file "e" is not present) for a makedir command

In the end it depends on what function is using the path, but in any case "/e" it's a formally valid fullpath+filename.

http://msdn.microsoft.com/en-us/library/aa365247.aspx

Seems to me there are so many rules and special cases (and this is only Windows) the only way to be really sure a path + file is valid is to actually try to open/access the file. If you do a lot of manual checks, you can probably get reasonably sure.

But in the end, is something useful ? Normally you validate a path when you are about to use it, on the right system, with security doing its part etc...

How useful is to check *offline* a list of "possible" full pathnames in real life ?

Re: GetFilePart should ignore forward slash

Posted: Fri Feb 28, 2014 1:14 am
by PB
> The simple string "e" IS a valid filename, and can be valid for a file.
> However, "/e" cannot be, because the "e" would denote a folder name

Yes, exactly. However, Microsoft Excel is telling me that it launched a
file named "/e" via the commandline. This is obviously not true, but it's
the "filename" given to me. So, in my report, the user is told that they
worked on a document named "/e", which is obviously incorrect. What
do I tell the user? "Sorry mate, Excel said you did it, so you did it." :(