Declined Function to to verify file name validity...

May 24, 2010
855
0
Northlake, Il
I think it would be useful to have a function/keyword (similar to the "EXIST" keyword) that would test whether or not a given path/file name is valid; not whether or not it exists, rather just whether it could exist. I make this distinction because there are instances where a file name/path can be absolutely syntactically valid, but not (yet) exist. A perfect example of what I mean by this would be the command "MD D:\A\B\C\D\E\F" /D", which would, of course, create that directory tree and CD into it. However, I'm not sure at this point if the drive letter should be checked for existence because, simply put, it can be "made" to exist at any point in time if it does not exist simply by using the "subst" command. Also, the existence of the "path" part of a file name/path can be easily determined by using the existing EXIST keyword (although the path would have to be separated from the file name) if that is what is needed. The reason this comes up, just so I can make it clear exactly what I'm looking for, is that I have a batch file that displays a dialog box asking the user for the name of a file that may be created sometime in the future, and I would prefer that the user of this batch file know that the file name is invalid and therefore can not be created at the time it is entered rather than waiting to later and having the attempted creation fail. (In the interim I will probably write a C++ program...)
 
May 20, 2008
11,415
99
Syracuse, NY, USA
On Thu, 26 May 2011 03:02:06 -0400, mathewsdw <> wrote:

|I think it would be useful to have a function/keyword (similar to the "EXIST" keyword) that would test whether or not a given path/file name is valid; not whether or not it *exists*, rather just whether it *could* exist. I make this distinction because there are instances where a file name/path can be absolutely syntactically valid, but not (yet) exist. A perfect example of what I mean by this would be the command "MD D:\A\B\C\D\E\F" /D", which would, of course, create that directory tree and CD into it. However, I'm not sure at this point if the drive letter should be checked for existence because, simply put, it can be "made" to exist at any point in time if it does not exist simply by using the "subst" command. Also, the existence of the "path" part of a file name/path can be easily determined by using the existing EXIST keyword (although the path *would* have to be separated from the file name) if that is what is needed. The reason this comes up, just so I can make it clear
|exactly what I'm looking for, is that I have a batch file that displays a dialog box asking the user for the name of a file that *may* be created sometime in the future, and I would prefer that the user of this batch file know that the file name is invalid and therefore *can not be created* at the time it is entered rather than waiting to later and having the attempted creation fail. (In the interim I will probably write a C++ program...)

I don't get it! As you pointed out, any path that doesn't contain forbidden
characters may **could** exist. Is that what you want ... a test for whether a
path spec contains forbidden characters?
 
May 24, 2010
855
0
Northlake, Il
On Thu, 26 May 2011 03:02:06 -0400,
I don't get it! As you pointed out, any path that doesn't contain forbidden
characters may **could** exist. Is that what you want ... a test for whether a path spec contains forbidden characters?
Yes, but both for the path spec and the file name itself as well as forbidden character combinations/ill-formed paths ("\A\B\..\.\C\D\E" or "Q\\R\\S" are admittedly rather unlikely examples), total path/file name lengths exceeding MAX_PATH, invalid names like NUL.txt, PRN.doc, CON.cpp (yes, these are all invalid file names despite the fact that they do not have the terminating colon; try to create a file using any given device name and you will see what I mean), not all devices/file systems support all given file/path names that might be valid for other devices/file systems (R/W CD & DVD drives are one example, as well as FAT16, FAT32 vs. NTFS), and more other cases than I can think of and/or are willing to list at the moment, which is precisely why I didn't want to write that code myself if I didn't have to.

If you visit http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx you will at least get a glimpse of precisely how many cases one has to consider, and I have thought of several myself (and tested them!) that that article didn't even mention.

I will also admit that I am kind of a fanatic when it comes to coding, I absolutely will not under any circumstances if I can possibly avoid it allow a user of one of my programs to supply data to that program that will be rejected by the operating system or some other program that is involved in the process that I am trying to create, particularly when handling that error is beyond my "input" stage in the total process and therefore what happens is outside of my control. (Not to give a wrong impression here, no, I do not think of "everything", but I do try very hard to come as close to that goal as I can possibly manage.)
 
May 20, 2008
3,515
4
Elkridge, MD, USA
From: mathewsdw
| From: vefatica
|| I don't get it! As you pointed out, any path that doesn't contain
|| forbidden characters may **could** exist. Is that what you want ...
|| a test for whether a path spec contains forbidden characters?
|
| Yes, but both for the path spec and the file name itself as well as
| forbidden character combinations/ill-formed paths ("\A\B\..\.\C\D\E"
| or "Q\\R\\S" are admittedly rather unlikely examples), total
| path/file name lengths exceeding MAX_PATH, invalid names like
| NUL.txt, PRN.doc, CON.cpp (yes, these are all invalid file names
| despite the fact that they do not have the terminating colon; try to
| create a file using any given device name and you will see what I
| mean), not all devices/file systems support all given file/path names
| that might be valid for other devices/file systems (R/W CD & DVD
| drives are one example, as well as FAT16, FAT32 vs. NTFS), and more
| other cases than I can think of and/or are willing to list at the
| moment, which is precisely why I didn't want to write that code
| myself if I didn't have to.

The basic request - an "IF VALIDNAME" test - is at first blush legitimate.

The samples of ill formed paths above are all legal, and occasionally useful, esp. when they are constructed by a program. The TRUENAME command and the @TRUENAME function translate them to the properly formed equivalents, whether or not the path exists, or - if they included a drive letter - even if no such drive exists.

Testing for name length exceeding MAX_PATH could, obviously, be done only on the TRUENAME.

Testing for device names (AUX COM1 COM2 COM3 COM4 CON LPT1 LPT2 LPT3 LPT4 NUL PRN) is already available: the function %@DEVICE[%@name[x]] returns 1 if the NAME portion of the string x is a device name. (See its limitations without using the @NAME function in a separate post.)

Testing based of file system type would be impossible, because there are file systems which only accept lower case letters, and others which accept only upper case letters, and your network may contain both. NET USE allows you to map conflicting file systems to the same drive letter at different times, and USB ports likewise. You may put a writable CD into a DVD-writer. The only thing you could detect is whether or not a specific name is acceptable to the currently connected file system. Even then TCC may not be aware of the internal naming rules of the file system, and the only way it could validate a name is to try to create the directory or file. For some devices this may result in the irrevocable creation of the file, which is not your goal.

In conclusion, I do not think your suggestion is feasible.
--
Steve
 
May 24, 2010
855
0
Northlake, Il
From: mathewsdw
The basic request - an "IF VALIDNAME" test - is at first blush legitimate.

The samples of ill formed paths above are all legal, and occasionally useful, esp. when they are constructed by a program. The TRUENAME command and the @TRUENAME function translate them to the properly formed equivalents, whether or not the path exists, or - if they included a drive letter - even if no such drive exists.

Testing for name length exceeding MAX_PATH could, obviously, be done only on the TRUENAME.

Testing for device names (AUX COM1 COM2 COM3 COM4 CON LPT1 LPT2 LPT3 LPT4 NUL PRN) is already available: the function %@DEVICE[%@name[x]] returns 1 if the NAME portion of the string x is a device name. (See its limitations without using the @NAME function in a separate post.)

Testing based of file system type would be impossible, because there are file systems which only accept lower case letters, and others which accept only upper case letters, and your network may contain both. NET USE allows you to map conflicting file systems to the same drive letter at different times, and USB ports likewise. You may put a writable CD into a DVD-writer. The only thing you could detect is whether or not a specific name is acceptable to the currently connected file system. Even then TCC may not be aware of the internal naming rules of the file system, and the only way it could validate a name is to try to create the directory or file. For some devices this may result in the irrevocable creation of the file, which is not your goal.

In conclusion, I do not think your suggestion is feasible.
--
Steve

Steve, thank you for your response. I'm sorry I haven't gotten back to you sooner, but my DSL modem died the other day and it's taken me until now to be able to connect to the internet again. As far as your ideas/suggestions go, I will look into them in some more detail and get back to you when/if I have anything to ask/add. (Although I must admit that I find the idea that it is "not feasible" to be a little bit suspect, if nothing else I can try to make a zero-length file or directory of that name and see if that succeeds (and delete the file/directory once I know that it has succeeded). And as far as "For some devices this may result in the irrevocable creation of the file, which is not your goal." goes, I haven't ever encountered that (although I am not arguing that that might not be the case for some rather "strange", in my opinion, file systems, but I consider that to be a small enough risk for me and the intended users of my batch "programs" that I can safely disregard it. And I will also note that the "@Unique" function also obviously disregards that possibility.) But I absolutely would prefer to do as many checks as I possibly can before going to that last, final, step, and ironically, just for the reason you previously mentioned if nothing else). And as far as "%@DEVICE" goes, thank you, I wasn't aware of that function (as I have mentioned several times previously, because of my bad eyesight I tend to read almost strictly on an "as needed" basis, and I have never seen that in the documentation before as far as I can remember). And, as a final point, I knew that path names like the above are generally valid; I just just don't consider them to easily decipherable by a human being. (It's completely non-relevant to me if a "program" creates paths of that form "internally" because they will never be seen in that form by any human being (other than possibly the person that is "debugging" the program), although I must add that it might make the debugging of that program somewhat more difficult. And if you actually try some of them (which I have done) you will see what I mean regarding "human readability. Although the "@TrueName" function might alleviate that situation somewhat.) But that's a relatively minor concern...
 
Similar threads
Thread starter Title Forum Replies Date
Joe Caverly Done Function that returns environment variable type Suggestions 1
Charles Dye @IPADDRESSV6 function Suggestions 1
R @BTWN[low number, high number] function Suggestions 0
mfarah Done pdir /(k) for the output of the @CKSUM function. Suggestions 0
E Done Support for IPv6 (ping function) Suggestions 5
Joe Caverly Done ISLIBRARY Conditional Expression Status Test and Function Suggestions 0
A @REALPATH variable function. Suggestions 0
rps Declined @ffind[] function. Suggestions 3
K_Meinhard Done @dowi function Suggestions 2
R (Nearest) Weekday function Suggestions 4
N Done Add display options to new HASH function Suggestions 1
mfarah Done Posix 100.32 compliant %@cksum[] function. Suggestions 12
Joe Caverly Function that converts an expression into a date value Suggestions 0
mikea Done Function to create GUID Suggestions 13
S Declined Character / string classification function Suggestions 9
S Done Enhanced ALIAS, SET, ESET and FUNCTION commands Suggestions 0
M Done Enhancement to @PID function... Suggestions 4
S Done Enhanced FUNCTION and ALIAS definition syntax Suggestions 0
S Done Window positioning commands and current position function Suggestions 0
S Declined Indirect function specification Suggestions 2
S Done Command and help enhancement, esp. ALIAS, FUNCTION and SET Suggestions 2
Charles Dye Done @PLUGINVER function Suggestions 0
Charles Dye Done v11 idea - Date picker function Suggestions 3
M Done Verify validity of file name and/or path... Suggestions 2
Joe Caverly Link in Help File to Blog Articles Suggestions 0
Joe Caverly Declined Use ADS file with TOAST Suggestions 1
vefatica File name completion at the beginning of the command line Suggestions 6
Joe Caverly Done @FILESIZE and NTFS File Stream Suggestions 1
Joe Caverly Determine if a file is on an SSD drive Suggestions 5
Joe Caverly Declined Debug without first saving to a file Suggestions 3
R Declined File Explorer enhancement request Suggestions 8
D Secondary shells should not search for - but reuse - the current configuration file. Suggestions 11
Joe Caverly Declined Update FFIND in Help File with example for ORing expressions to search for Suggestions 6
D Done Re-activate the [secondary] section in the ini-file Suggestions 0
x13 Done Typo in help file (including PDF) | v24.02.46 Suggestions 0
R Declined wish-list: add a command to copy a file to clipboard. Suggestions 17
M Done Parameter to ignore [Escape] for FFIND /T"XXX" in a file Suggestions 5
Joe Caverly Done File completion for LIBRARY commands Suggestions 1
vefatica Declined Enhance file name completion Suggestions 4
D Declined File Explorer Suggestions 2
R Declined Install option: Add File Explorer Run As Adminstrator option Suggestions 14
mfarah Done TCEDIT: show the full name (with drive and path) of the active file on the title bar. Suggestions 3
R User-created extended help (probably in a text file): DIRHISTORY /?x Suggestions 10
Joe Caverly Declined Help File "See Also" Suggestions 4
thorntonpg Done Goto folder in file explorer from the current directory in the tcc prompt Suggestions 7
rconn add support for batch file projects Suggestions 0
rconn Done map the registry as a file system Suggestions 0
rconn Declined Help file organization Suggestions 0
rconn Done extend tcc file format Suggestions 0
K_Meinhard File descriptions for Explorer etc. Suggestions 1

Similar threads