LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to check string control to see if it has a valid windows file name (no illegal characters like * / > " : |).

Here is a web page that supposedly offers a regex that will validate a file name input.  I haven't tried this yet, but will soon.  Check it out.

http://www.regexlib.com/REDetails.aspx?regexp_id=345

I hope this will save someone else the 30 minutes of Googling I spent looking for it.

Thanks to all who made this an educational conversation.

0 Kudos
Message 21 of 38
(13,518 Views)
Since so many people are chiming in, I though I throw my $.02 worth. People keep forgetting that ascii has a bunch of unprintable characters which may or may not be legal character. I use a search and replace function. enclosed is my VI.
Message 22 of 38
(13,515 Views)
I too was hoping there would be an easy way to check filename validity (like turning on the password feature of a string control), but my quick scan did not come up with anything.  You can use a “strip path” and “match regular expression” to come up with a working solution.  You would just have to check the “whole match” output to see if it contains anything.  If it does, then there is a problem with the filename.  See attached code (version 8.5)
0 Kudos
Message 23 of 38
(13,477 Views)
We convert user-entered data strings into file and path names for identification of the test data files. I needed to create a trap that would (1.) keep the system from trying to create an illegal file or directory name and (2.) notify the user immediately of invalid characters. I read through what you guys were doing and then came up with this. It works with our application that is running in LabVIEW 8.5
 
CS
Message 24 of 38
(13,302 Views)
I couldn't find one to fit what I needed so I wrote one to check file names with some error handling.
Message 25 of 38
(13,039 Views)

Not sure where everybody sourced their filename illegal character rules.  Heres some from MSDN (http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx😞

 

  • Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:
    • The following reserved characters:
      • < (less than)
      • > (greater than)
      • : (colon)
      • " (double quote)
      • / (forward slash)
      • \ (backslash)
      • | (vertical bar or pipe)
      • ? (question mark)
      • * (asterisk)
    • Integer value zero, sometimes referred to as the ASCII NUL character.
    • Characters whose integer representations are in the range from 1 through 31, except for alternate streams where these characters are allowed. For more information about file streams, see File Streams.
    • Any other character that the target file system does not allow.
  • Use a period as a directory component in a path to represent the current directory, for example ".\temp.txt". For more information, see Paths.
  • Use two consecutive periods (..) as a directory component in a path to represent the parent of the current directory, for example "..\temp.txt". For more information, see Paths.
  • Do not use the following reserved device names for the name of a file:

    CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see Namespaces.

  • Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp
  •  

    ** The code kindly posted by Rob F does not deal with file or directory names which end with a space or period.

    Message Edited by battler. on 05-03-2010 12:41 AM
    Message Edited by battler. on 05-03-2010 12:47 AM
    Message 26 of 38
    (12,807 Views)

    Check if path ends in space or period by reversing string (converted to string) and checking if first character (index 0) matches.  Modified code as follows:

     

    Check Empty Path & End in Space or Period.png 

     

     

     

    Check for path ending in space and/or period.  It is illegal and generates an error in Windows when you try to create a file/folder.

    Message 27 of 38
    (12,789 Views)

    Another option for the space or period at the end of a string is using a regular expression:

     

     

    String_End.png

     

     

    Also, here's an old example which shows this with a bubble similar in style to that of Windows Explorer (reply 11).


    ___________________
    Try to take over the world!
    Message 28 of 38
    (12,779 Views)

    If you are on 8.6 or newer there is a VI in the vi.lib that I think does what you need.  Look in vi.lib\Utility\libraryn.llb\Is Name Multiplatform.vi. 

     

    Looking at the source I see a whole lot of code handling other operating systems.  It also gives a suggestion file name that does work is valid.

    Message 29 of 38
    (12,427 Views)

    I have found that LabVIEW's "Open/Create/Replace File Function" cannot handle valid characters in a file path, such as single quotes ('), whereas the TDMS Open function handles this properly.

     

    Does anyone have any advice for how to use the single quote (') for LabVIEW file paths other than TDMS?

     

    I have attached the test cases which illustrate the problem.

    0 Kudos
    Message 30 of 38
    (12,044 Views)