Commas in Scripting.FileSystemObject

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Escargot18
    New Member
    • Mar 2013
    • 3

    #1

    Commas in Scripting.FileSystemObject

    Hi This code works great unless there is a comma in the filename:

    Code:
    Dim dir, folder, files
    dir = "c:\ValidateFiles\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(dir)
    Set files = folder.files
    
    lstFiles.RowSourceType = "Value List"
    For Each file In files
    If Right(file.Name, 3) = "xml" Or Right(file.Name, 3) = "XML" Then
    lstFiles.AddItem (file.Name)
    End If
    Next
    If a comma exists the file is added to the list, but stops at the first comma.

    Example:

    Test File 1.xml works fine.
    Test, File 1.xml only add "File" to the list.

    What am I missing?

    Thanks,
    John
    Last edited by zmbd; Mar 25 '13, 03:32 PM. Reason: [Z{Please use the [CODE/] button to format posted code/html/sql - Please read the FAQ}]
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    1) Even if the OS allows it, it is not considered best practice to use non-alphanumeric characters other than the underscore in file names, field names, table names, etc... and your problem is just such one case in point. (IMHO: Even spaces should never be used!)

    2) You should not use reserved/key/token-words such as "File," "Folder," "Database," etc... as names for your variables. If you do want to use these words then you should preface (or affix) "me" or "obj" etc to the variable name.

    Using reserved words will cause you issues!
    Access 2007 reserved words and symbols
    (...)You might also encounter errors if you use a reserved word to name a control, an object, or a variable. The error messages you receive don't necessarily tell you that a reserved word is the cause of the problem. As a result, it can be difficult to identify what needs to be changed.



    -->
    To directly address your issue: what you'll need to do is place the file name within quotes such as you would if the name had/has spaces within it.

    IMHO: the best way is to use a string variable... and because using double quotes is a pain, I'll break my rule this time on posting code ;-) :
    Code:
    '(..>preface Code Omitted<..)
    Dim strADDMe as String
    '(..>Code Omitted<..)
    strAddMe = """" & objfile.Name & """" '<<< Note I prefixed obj to the variable!
    lstFiles.AddItem (strAddMe)
    '(..>Closeing Code Omitted<..)
    Last edited by zmbd; Mar 25 '13, 04:03 PM.

    Comment

    • Escargot18
      New Member
      • Mar 2013
      • 3

      #3
      Commas in Scripting.FileS ystemObject Solved

      Thank you for the quick response. I'll modify the code to incorperate your suggestions. Unfortunately I have relatively little control over the inport file names.
      Thanks again,
      John

      Comment

      Working...