7 Replies Latest reply on Aug 16, 2018 1:23 PM by andrew_french

    How do I run a script that is located on a network drive without mapping the drive to a drive letter?

    jasonluu

      I want to run a script in Xpedition Designer by entering "run \\remote-server\script\script.vbs" in the command toolbar.

       

      This does not work. Xpedition Designer cannot find the file even if I typed "run \\\\remote-server\\script\\script.vbs"

       

      The only workaround I know of that works is to map the network to a drive letter such as "D:" = \\remote-server\

      After mapping, "run D:\script\script.vbs" will work.

       

      However, I want to avoid having to map a drive letter. Is this possible?

        • 1. Re: How do I run a script that is located on a network drive without mapping the drive to a drive letter?
          matthias.cosaert

          Hi,

           

          I had the same issue and found a workaround by using a local script that reads the server script and then executes the code using ExecuteGlobal.

           

          Example:

          '###############################################
          Const IncludeScriptsFolder = "\\myserver\myscriptfolder"
          '###############################################


          call Include("myscript.vbs")

           


          sub Include(ScriptFileName)
              Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")

             if LEN(IncludeScriptsFolder) = 0 then
                 Msgbox "Sciptsfolder variable not set." & vbnewline & "(To load " & ScriptFileName & ")" , 0 + 16
             elseif fso.fileExists(IncludeScriptsFolder & "\" & ScriptFileName) = false then
                 Msgbox "Failed to include script file:" & vbnewline & IncludeScriptsFolder & "\" & ScriptFileName & vbnewline & "File not found" , 0 + 16
             else

                 Dim tso : Set tso = fso.OpenTextFile(IncludeScriptsFolder & "\" & ScriptFileName, 1)

                 Dim LoadedScriptCode : LoadedScriptCode = tso.ReadAll

                 tso.Close
           
                ExecuteGlobal LoadedScriptCode

             end if

             set fso = nothing

          End Sub

          • 2. Re: How do I run a script that is located on a network drive without mapping the drive to a drive letter?
            jasonluu

            Thanks Matthias! I tried your solution and it works, however, my use case is a bit different.

             

            I want to bind key that runs script located on a remote server without mapping a drive letter.

             

            For example, I want to add this binding

            Bindings("Schematic").AddKeyBinding "Ctrl+R", "run \\remote-server\scripts\script.vbs",1,2

             

            However, I'm running into the network drive issue as mentioned before.

             

            Although ExecuteGlobal help resolves the network drive issue, I'm not sure if I can use it as a bindkey like this

            Binding("Schematic).AddKeyBinding "Ctrl+R", "ExecuteGlobal " & LoadedScriptCode,1,2

            • 3. Re: How do I run a script that is located on a network drive without mapping the drive to a drive letter?
              nadeau_

              Add \\remote-server\script to your WDIR environment variable (WDIR_EEVX_2_3, etc.) and just call it with "run script.vbs"

              • 4. Re: How do I run a script that is located on a network drive without mapping the drive to a drive letter?
                Patrick.Cashman

                Use an intermediate script, in a location with a common drive letter, or on a drive on the local machine. Use that script to point to your UNC path.

                 

                For example:

                 

                Bindings("Schematic").AddKeyBinding "Ctrl+R", "run C:\automation_tools\phonebook\phonebook.vbs",1,2

                 

                Now, in C:\automation_tools\phonebook, create the phonebook.vbs script, with the following contents:

                 

                Option explicit

                dim path

                dim wsh

                set wsh = CreateObject("shell.application")

                path = "\\wicdept\depts\ENG\LIBRARY\RELEASED\Mentor\Scripts\Phonebook\Phonebook.exe"

                wsh.open path

                set wsh = Nothing

                 

                Yes, this does imply that you have to choose at least one common location to use for startup scripts.

                 

                Or you could use the subst command to temporarily create a drive letter mapping, then delete it immediately after use.

                • 5. Re: How do I run a script that is located on a network drive without mapping the drive to a drive letter?
                  matthias.cosaert

                  I set the keybinding/menu to an intermediate local script that uses the executeglobal to run the server script.

                   

                  There is also another way to do it by using a custom command:

                   

                  Add this at the end of the keybindings script:

                   

                   

                  On error resume next
                  RegisterOLECommand "Srun", "Run server script", false, Scriptengine

                  if err.number = 0 then 'error when already registred
                     On error Goto 0
                     Scripting.DontExit = True
                  end if
                  On error Goto 0

                   

                  sub Srun(ScriptFilePath)

                      'use unique names to avoid issues with same variable name in loaded script
                      Dim UniqueNamefso : Set UniqueNamefso = CreateObject("Scripting.FileSystemObject")

                      if UniqueNamefso.fileExists(ScriptFilePath) = false then
                         Msgbox "Failed to include script file:" & vbnewline & ScriptFilePath & vbnewline & "File not found" , 0 + 16
                     else

                         Dim UniqueNametso : Set UniqueNametso = UniqueNamefso.OpenTextFile(ScriptFilePath, 1)

                         Dim LoadedScriptCode : LoadedScriptCode = UniqueNametso.ReadAll

                         UniqueNametso.Close
                   
                         ExecuteGlobal LoadedScriptCode

                     end if

                     set UniqueNamefso = nothing

                  End Sub

                   

                   

                   

                   

                  and use the custom command in the keybindings for server scripts:

                  Bindings("Schematic").AddKeyBinding "Ctrl+R", "srun \\remote-server\scripts\script.vbs",1,2

                  • 6. Re: How do I run a script that is located on a network drive without mapping the drive to a drive letter?
                    jasonluu

                    Thanks All. I went with Patrick's suggestion of writing a command to map the drive letter. The code belows checks if the network drive has been mapped to a drive letter. If the network drive is not mapped, it will map it to an available drive letter.

                     

                    The code below works. Unfortunately, this script has to be ran AFTER a project has been opened. The script will run at startup if I add it to script.ini, however, the bindkey will not bind. I was hoping to use the script below as a startup script to add in the bindkey but it looks like the user has to manually run it each time they open a project.

                    Option Explicit

                     

                    Const networkName = "\\remote-server"

                    Const scriptPathName = "\script\script.vbs"

                    Const hotKey     = "Ctrl+R"

                     

                    Dim Menu: Menu = 0

                    Dim Accelerator: Accelerator = 1

                    Dim NotSticky: NotSticky = 0

                    Dim Sticky: Sticky = 1

                    Dim cmd: cmd = 1

                    Dim key: key = 2

                     

                    Dim driveLetter

                    driveLetter = getDriveLetter(networkName)

                     

                    If driveLetter = "" Then

                      driveLetter = getAvailableDriveLetter

                      setDriveLetter driveLetter, networkName

                    End If

                     

                     

                    Bindings("Schematic").AddKeyBinding hotKey,"run " & driveLetter & scriptPathName ,cmd, key

                     

                     

                    Function getDriveLetter(networkName)

                      With CreateObject("WScript.Network")

                        Dim i

                        For i = 0 to .EnumNetworkDrives.Count - 1 Step 2

                          If .EnumNetworkDrives.Item(i+1) = networkName Then

                            getDriveLetter= .EnumNetWorkDrives.Item(i)

                          End If

                        Next

                      End With

                    End Function

                     

                     

                    Function getAvailableDriveLetter

                      With CreateObject("Scripting.FileSystemObject")

                        Dim i

                        For i = Asc("D") To Asc("Z")

                          If Not .DriveExists(Chr(i)) Then

                            getAvailableDriveLetter = Chr(i) & ":"

                            Exit For

                          End If

                        Next

                      End With

                    End Function

                     

                     

                    Sub setDriveLetter(driveLetter,networkName)

                      Dim WshShell, objNetwork

                      Set WshShell = CreateObject("WScript.Shell")

                      Set objNetwork = CreateObject("Wscript.Network")

                      objNetwork.MapNetworkDrive driveLetter, networkName, False

                    End Sub

                    • 7. Re: How do I run a script that is located on a network drive without mapping the drive to a drive letter?
                      andrew_french

                      Might have to do with how you are calling your scripts in the ini file.

                       

                      [Expedition PCB]  - this will run when the application is opened.

                      Script#0=EXPMenu.vbs

                       

                      [Expedition PCB - Document]  - this will run once the project is opened.

                      Script#0=ExpEventHandler.vbs

                       

                      If you're storing your scripts on a network drive to begin with, why don't you just add them to the corporate WDIR?  The path is already set and then any vbs file you create here can be easily executed by "run script.vbs"