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


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was useful.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.