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

VB script - open files from mulitple locations

Hi

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
    Next
    Next

'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

Example
->C
-->Users
--->xxxxxxxxx
---->Desktop
----->XML_Parser

then the specific location

->Location1 or Location2 etc

Then

->txt
0
wilko1000
Asked:
wilko1000
2 Solutions
 
KimputerCommented:
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.
0
 
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

0
 
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", _
                  "C:\Users\xxxxxxx\Desktop\XML_Parser\Loction7\txt")  

For Each sFolderName In aPathList
    Wscript.Echo sFolderName
Next

Open in new window

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

~bp
0
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