Announcement

Collapse
No announcement yet.

Input box for folder location issues

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Input box for folder location issues

    I have an input box that the user will input the name of a folder, how can I verify that it does not have invalid characters that windows can't have in a folder name.

    Additionally they might enter the folder in with a sub folder, but everytime that happens it added in an extra \

    Example:
    Docs\Taxes\

    that gets saved as Docs\\Taxes\\

    So i need to somehow make sure there are no dbl \'s and no invalid chars.

  • #2
    I have an input box that the user will input the name of a folder, how can I verify that it does not have invalid characters that windows can't have in a folder name.
    You can use a helper function which is actually part of the Setup Factory distribution:

    Code:
    --[[	
    **********************************************************************************
    Function:	g_ContainsValidPathChars
    Purpose:	Checks whether a string contains valid path characters, which is
    			anything EXCEPT these characters: / * ? " < > |
    Arguments:	(string) strText - The string to validate
    Returns:	(boolean) true if the string is valid, false if it contains any invalid characters
    **********************************************************************************
    --]]
    function g_ContainsValidPathChars(strText)
    	-- note: this uses the internal lua function string.find which supports
    	--       pattern matching
    	return string.find(strText, "[/%*%?\"<>|]") == nil;
    end
    The use of the function above should be pretty much obvious: Provide a path as an argument, and the function returns true if it is a valid path, or false if it contains invalid chars.

    Additionally they might enter the folder in with a sub folder, but everytime that happens it added in an extra \

    Example:
    Docs\Taxes\

    that gets saved as Docs\\Taxes\\
    The ending backslash is actually correct - folder names should preferably end with a backslash, so you can concatenate with a file name when needed and build a complete path. For example:

    Code:
    local sFolder = "C:\\TEMP\\";
    local sFilename = "textfile.txt";
    local sFullyQualifiedPath = sFolder .. sFilename;
    If you use actions such as String.SplitPath(), you will also see that the folder name ends with a backslash.

    However, what doesn't make much sense is that you state that the backslashes are doubled when the data is being saved. You may want to check your script, because that isn't something that is done automatically, but is probably an indication of a mistake in your programming.

    Ulrich

    Comment


    • #3
      Thanks for the info. I tried searching and had a heck of a time finding something. I will check it out in the morning.

      One quick thing about my data when saved to sqlite. I run the input to be saved through this:

      Code:
      function Enclose(strText)
      	--returns the supplied string as a quoted string.
      	return string.format("%q", strText);
      end
      so the user puts in the box
      \Docs\Taxes\
      When I view the data in sqlite browser (or retrieve it) it comes out like this
      \\Docs\\Taxes\\
      Here is the code for saving the info to sqlite
      Code:
      SQLite.Query(db, "Insert into filestbl(fileloc) values("..Enclose(strFileLoc)..")", nil);
      Mostly everything I save to sqlite is passed through the Enclose, and it only seems to affect the "\"

      I have read a few things using the search and got a bit confused. (Newbe less than 2 weeks days old with this, trying to learn and searching a lot and that helps. Figuring out a lot.)

      Comment


      • #4
        Originally posted by Ulrich View Post
        You can use a helper function which is actually part of the Setup Factory distribution:

        Code:
        --[[	
        **********************************************************************************
        Function:	g_ContainsValidPathChars
        Purpose:	Checks whether a string contains valid path characters, which is
        			anything EXCEPT these characters: / * ? " < > |
        Arguments:	(string) strText - The string to validate
        Returns:	(boolean) true if the string is valid, false if it contains any invalid characters
        **********************************************************************************
        --]]
        function g_ContainsValidPathChars(strText)
        	-- note: this uses the internal lua function string.find which supports
        	--       pattern matching
        	return string.find(strText, "[/%*%?\"<>|]") == nil;
        end
        The use of the function above should be pretty much obvious: Provide a path as an argument, and the function returns true if it is a valid path, or false if it contains invalid chars.
        Ok I stayed up for a few and tried, but I am unsure what variable is set to give me the true or false. (help file was not helpful this time)

        I have the this code:

        Code:
        function g_ContainsValidPathChars(strText)
        	-- note: this uses the internal lua function string.find which supports
        	--       pattern matching
        	return String.Find(strText, "[/%*%?\"<>|]") == nil;
        end
        
        ---- Lots of other code and functions
        
        	local strLoc = Input.GetText("I-Loc");
        	g_ContainsValidPathChars(strLoc)
        
        -- then using a dialog box to try and show me result for debugging.
        	Dialog.Message("No Error in text",strLoc, MB_OK);
        How to I find out boolean value?

        I tried removing it from a function and just having it a line of code that matched the help file but that always returned errorvalue -1

        Code:
        	local strLoc = Input.GetText("I-Loc");
        	errorvalue = String.Find(strLoc, "[/%*%?\"<>|]", 1, false)
        	Dialog.Message("Error Value",errorvalue, MB_OK);
        Thanks for the help!

        Comment


        • #5
          The code I posted here uses the pure Lua function string.find(), not String.Find().

          If you don't know how a function returns a value, I recommend that you read the documentation. In the help file, you can find a chapter titled Scripting Guide, which explains, among other things, what functions are, how they are used, and how they return values.

          Ulrich

          Comment


          • #6
            Solved!

            Aw I see what I was doing wrong. I did read the help file before on functions, but now I see what I missed. Thanks. That and I was using the wrong string.find was not aware there is a difference between string.find and String.Find. That helped knowing that. had my script working in less then 3 mins now. YAY

            using your original code posted I then added this to my script. and it now works.

            Code:
            	local strLoc = Input.GetText("I-Loc");
            	errorvalue = g_ContainsValidPathChars(strLoc)
            	if errorvalue == false then
            		Dialog.Message("Error in text",strLoc, MB_OK);
            	else 
            		Dialog.Message("No Error in text",strLoc, MB_OK);
            	end
            Also reviewing my other issue with the dbl \\ being added. I found the issue.

            I was sending the text to the Enclose function when saving. that was the issue. changed it to have the code like below and dbl \\ are now gone. YAY!

            Code:
            SQLite.Query(db, "Insert into filestbl(fileloc) values(\""..strLoc.."\")", nil);
            Thanks you so much for pointing me in the right direction in both issues, I missed the needed info last night. Probably to tired is why. But all is well now.

            time to look into some of the other things such as "String.SplitPath()" that you mentioned above and see how it can work for me.

            Thank you!

            P.S. I noticed I am not allowed to edit my posts, is that normal? I just hate it when I reply to my own post when I could have simply just made a change to my OP. that and once my issue is resolved I like to change the title to have the world Solved at the end.

            Comment

            Working...
            X