Results 1 to 8 of 8

Thread: Access two files from multiple paths

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    7

    Access two files from multiple paths

    I have the following code which currently opens up a command box in which the user enters the information and if it finds a match in both paths, opens the file associated with that information from both paths.

    Basically, i would just like to add another path and open the file from that path if the user input begins with "E"

    So, if the input begins with "E" I would want it to open the file from "strFolderName2" as well as the file from a new path "strFolderName3"

    Currently, the code searches in just 2 paths and has a distinguishing script that returns a certain number of characters to search based on if it starts with "L" or doesn't.

    Thanks for any help!

    Code:
    Dim strSearchFileName, strFolderName1, strFolderName2, strFile1, strFile2, Part 
       
     'Parameters 
     strSearchFileName = getSearchFile()
     strSearchFileName1 = getSearchFile1()
     
     If strSearchFileName = "" Then WScript.quit()
     strFolderName1 = "K:\08 Admin Support\02 Develop & Control Docs & Data\09 Industry Docs\Customer Standards\GEAE\GE Drawings" 
     strFolderName2 = "K:\08 Admin Support\02 Develop & Control Docs & Data\17 Final Inspection Tech Cards" 
       
     'Get file if folder and file exists (Returns String) 
     strFile1 = getFile(strFolderName1, strSearchFileName) 
     strFile2 = getFile(strFolderName2, strSearchFileName1) 
       
     'If _strFile1 and _strFile2 are not equal to nothing open the files 
     If strFile1 <> "" And strFile2 <> "" Then 
                     openFile(strFile1) 
                     openFile(strFile2) 
     Else 
                     MsgBox("A drawing containing '" & strSearchFileName & "' was not found in GEAE.") 
     End If 
       
       
     'Get file if folder and file exists (Returns String) 
     Private Function getFile(strFolderName, strFileName) 
                     Dim objFSO, folder, file 
       
                     Set objFSO = CreateObject("Scripting.FileSystemObject") 
       
                     'Verify passing folder exists 
                     If objFSO.FolderExists(strFolderName) Then      
                                     Set folder = objFSO.GetFolder(strFolderName) 
       
                                     'Loop over all files in the folder until the searchFileName is found 
                                     For each file In folder.Files 
                                                     If InStr(file.Name, strFileName) >=1  Then 
                                                                     getFile = file.path 
                                                                     Exit Function 
                                                     End If 
                                     Next 
                     End If 
       
                     getFile = "" 
     End Function 
       
     'Get user input (Returns String) 
     Private Function getSearchFile() 
                     getSearchFile = InputBox("Find a Part Number containing:" & vbCr & vbCr & "(Case Sensitive)" & vbCr & vbCr & "Examples: L59253 or 1967M33") 
    				part = getSearchFile
    				If Left(getSearchFile,1) = "L" Then
    						getSearchFile = Left(getSearchFile,6)
    						
    				Else
    						getSearchFile = Left(getSearchFile,7)
    						
    				End If
    				
    End Function
    
    Private Function getSearchFile1() 
                     getSearchFile1 = part 
     
    				If Left(getSearchFile1,1) = "L" Then
    						getSearchFile1 = Left(getSearchFile1,10)
    						
    				Else
    						getSearchFile1 = Left(getSearchFile1,11)
    						
    				End If
    				
    End Function
       
     'Opens File 
     Private Sub openFile(strPath)  
                     CreateObject("WScript.Shell").Run chr(34) & strPath & chr(34)  
     End Sub

  2. #2
    Addicted Member
    Join Date
    Feb 2017
    Posts
    147

    Re: Access two files from multiple paths

    If you have managed to add 2 , why cant you add a third ?? Is there a specific part you are stuck with? Or did you not write this have no idea how it works and just want someone to write the code for you ? I'll can do that if you want but you should try to learn yourself

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: Access two files from multiple paths

    This should be moved to the VBScript forum.

    This is a little like asking someone to help add a third story to a treehouse. That code is aching to be overhauled, in a way that is more efficient (which probably isn't a large issue), and is easier to maintain/make future changes to (which is probably more important).

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    7

    Re: Access two files from multiple paths

    I am (as you pointed out) a very inexperienced code writer. I initially wrote the program for a single path. When i wanted to add another path, I had help from a person who is no longer working with me. I am just unsure on how to incorporate the IF statements and which variables needed to be added to add a 3rd path.

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: Access two files from multiple paths

    I wasn't trying to sound disparaging.

    I'll post some code in a few minutes. I am going to rename some variables to make their linkage consistent.

    If this code is something that needs even quasi-frequent maintenance, I would suggest getting someone to rewrite it in a more functional way to make future changes easier to implement. If this code almost never needs maintenance, then so be it.

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: Access two files from multiple paths

    Several notes. Your request on new functionality was somewhat light on specific details. I had to guess on how many characters you would want if the filename started with an E, so getSearchFile3 may need some adjustment.

    I've done the best I can given what you've provided, and this is my one-time favor for you in this thread. If you come back and say "But I need it to do xyz instead", someone else will need to help. I've tested this out on my own PC with different paths and different filenames and it works as well as I understand that it should.

    It is still somewhat spaghetti code, I wasn't in the mood to overhaul it and write it the way I would write it if I were given the problem to solve myself.

    Good luck.

    Code:
    Dim strSearchFileName1, strSearchFileName2, strSearchFileName3
    Dim strFolderName1, strFolderName2, strFolderName3
    Dim strFile1, strFile2, strFile3, Part 
       
    'Parameters 
    strSearchFileName1 = getSearchFile1()
    strSearchFileName2 = getSearchFile2()
    strSearchFileName3 = getSearchFile3()
    
    If strSearchFileName1 = "" Then WScript.quit()
    
    strFolderName1 = "K:\08 Admin Support\02 Develop & Control Docs & Data\09 Industry Docs\Customer Standards\GEAE\GE Drawings" 
    strFolderName2 = "K:\08 Admin Support\02 Develop & Control Docs & Data\17 Final Inspection Tech Cards" 
    strFolderName3 = "ENTER THE PATH FOR FOLDER3 HERE"
    
    'Get file if folder and file exists (Returns String)
    If Left(strSearchFileName1,1) <> "E" Then
        strFile1 = getFile(strFolderName1, strSearchFileName1)
        strFile2 = getFile(strFolderName2, strSearchFileName2)
        'If _strFile1 and _strFile2 are not equal to nothing open the files 
        If strFile1 <> "" And strFile2 <> "" Then 
            openFile(strFile1) 
            openFile(strFile2) 
        Else 
            MsgBox("A drawing containing '" & strSearchFileName1 & "' was not found in GEAE.") 
        End If
    Else
        strFile2 = getFile(strFolderName2, strSearchFileName2)
        strFile3 = getFile(strFolderName3, strSearchFileName3)
        'If _strFile2 and _strFile3 are not equal to nothing open the files 
        If strFile2 <> "" And strFile3 <> "" Then 
            openFile(strFile2) 
            openFile(strFile3) 
        Else 
            MsgBox("A drawing containing '" & strSearchFileName1 & "' was not found in GEAE.") 
        End If
    End If
       
       
    'Get file if folder and file exists (Returns String) 
    Private Function getFile(strFolderName, strFileName) 
    
        Dim objFSO, folder, file 
    
        Set objFSO = CreateObject("Scripting.FileSystemObject") 
       
        'Verify passing folder exists 
        If objFSO.FolderExists(strFolderName) Then      
            Set folder = objFSO.GetFolder(strFolderName) 
    
            'Loop over all files in the folder until the searchFileName is found 
            For each file In folder.Files 
                If InStr(file.Name, strFileName) >=1  Then 
                    getFile = file.path 
                    Exit Function 
                End If 
            Next 
        End If 
       
        getFile = ""
    
    End Function 
    
    'Get user input (Returns String) 
    Private Function getSearchFile1()
    
        getSearchFile1 = InputBox("Find a Part Number containing:" & vbCr & vbCr & "(Case Sensitive)" & vbCr & vbCr & "Examples: L59253 or 1967M33") 
        part = getSearchFile1
        If Left(getSearchFile1,1) = "L" Then
            getSearchFile1 = Left(getSearchFile1,6)
        Else
            getSearchFile1 = Left(getSearchFile1,7)
        End If
    				
    End Function
    
    Private Function getSearchFile2() 
    
        getSearchFile2 = part 
    
        If Left(getSearchFile2,1) = "L" Then
            getSearchFile2 = Left(getSearchFile2,10)
        Else
            getSearchFile2 = Left(getSearchFile2,11)
        End If
    				
    End Function
    
    Private Function getSearchFile3() 
    
        getSearchFile3 = part 
    
        If Left(getSearchFile3,1) = "E" Then
            getSearchFile3 = Left(getSearchFile3,10)
        Else
            getSearchFile3 = Left(getSearchFile3,11)
        End If
    				
    End Function
       
    'Opens File 
    Private Sub openFile(strPath)  
    
        CreateObject("WScript.Shell").Run chr(34) & strPath & chr(34)  
    
    End Sub
    Last edited by OptionBase1; Apr 19th, 2018 at 12:36 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    7

    Re: Access two files from multiple paths

    Thank you OptionBase1. I appreciate it!

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,106

    Re: Access two files from multiple paths

    FYI - there was one place where I didn't catch a rename of getSearchFile to getSearchFile1 in the getSearchFile1 Function, I corrected that in my code pasted above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width