• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

VB script - open files from mulitple locations


I have a working script  that opens files from mulitple locations. All works fine and no problems.

However the issue i have is there is upto 40 file paths the script looks in (and the opens files) so as you can imagine its a pain to maintan if a new location needs to be added and looks very messy and subject to typos

I would like to see if theres a better more neater way to look in mulitple locations. Heres the current solution:

'Open multiple folders, in my current solution this has upto 50 file paths
       Set oFSO = CreateObject("scripting.filesystemobject")
       cPathFile  = "C:\Users\xxxxxxx\Desktop\XML_Parser\\Loction1\txt,C:\Users\xxxxxxx\Desktop\XML_Parser\\Loction2\txt",C:\Users\xxxxxxx\Desktop\XML_Parser\\Loction3\txt,C:\Users\xxxxxxx\Desktop\XML_Parser\\Loction4\txt"

    'Split out the lines in to valid file paths
      For Each oFolder In Split(cPathFile, ",")
 'Check for vaild txt files
    For Each oFile In oFSO.GetFolder(oFolder).Files
    'bHeader = False 'tag the fact that the header for the next file is pending
     If UCase(oFSO.GetExtensionName(oFile.Name)) = "TXT" Then
     CreateXML oFSO, oFile
    End if

'Do work here....

The file structure will always start with C:\Users\xxxxxxx\Desktop\XML_Parser\ ' followed by 'location1' or 'Location2' etc then 'txt' which is aways be the same


then the specific location

->Location1 or Location2 etc


2 Solutions
The code can only be slightly better, but you already did most of important stuff already.
One thing is simple, the variable can be shortened:
cPathFile  = xxxx, xxxx ,xxxx
Just add the first and last part later ("C:\Users\" & xxxxxxx & "\Desktop\XML_Parser\")
Then add extra for loop to add Location (for each location in locations, where you already prepared afew locations in this variable) behind it, but it means, ALL the folders you want to walk by, HAS TO GO through the same locations. Otherwise the logic is broken, and the code will be messy again.
Robberbaron (robr)Commented:
personally i would use an array, simply because it is easier to reead and add.

you can easily spot the extra backslash you had there..
'Open multiple folders, in my current solution this has upto 50 file paths
       Set oFSO = CreateObject("scripting.filesystemobject")

dim cPathList(6)    'starts at zero

cPathList(0) = "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction1\txt"
cPathList(1) = "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction2\txt"
cPathList(2) = "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction3\txt"
cPathList(3) = "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction4\txt"
cPathList(4) = "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction5\txt"
cPathList(5) = "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction6\txt"
cPathList(6) = "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction7\txt"

    'Split out the lines in to valid file paths
      For Each sFolderName In cPathList        '<<< sFolderName is string, not object
            'Check for vaild txt files
            For Each oFile In oFSO.GetFolder(sFolderName ).Files

Open in new window

Bill PrewCommented:
I know this question is closed, and I had read it and thought "I'd probably use an array", but just for a different approach here is how I would do that.  I find this a little easier to add and delete entries to - no changing the array dimensions, and no need to manage the arrey indexes in the original assignments.

It's a personal preference, but just wanted to share.

aPathList = Array("C:\Users\xxxxxxx\Desktop\XML_Parser\Loction1\txt", _
                  "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction2\txt", _
                  "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction3\txt", _
                  "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction4\txt", _
                  "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction5\txt", _
                  "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction6\txt", _

For Each sFolderName In aPathList
    Wscript.Echo sFolderName

Open in new window

wilko1000Author Commented:
That works a treat thank you Bill Prew
Bill PrewCommented:
Welcome, glad that was useful.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now