0

This is how I'm passing arguments between files these days.

1.vbs:

Dim MyVar

'1) Assigning some value to MyVar.
MyVar = "foo"

'2) Passing MyVar to 2.vbs.
CreateObject("WScript.Shell").Run _
Chr(34) & "C:" & "\2.vbs" & _
Chr(34) & " " & Chr(34) & MyVar & Chr(34), _
1, True

'4) End point.
MsgBox MyVar

2.vbs:

    If _
    WScript.Arguments.Count > 0 _
    Then
    Call MySub( _
    WScript.Arguments(0))
    End If

Sub MySub(MyVar)

'3) Doing some work with MyVar.
MyVar = "bar"

End Sub

So, if all code was in 1 single file, and
if I had to use Call MySub instead of CreateObject("WScript.Shell").Run
— i'd successfully changed MyVar from "foo" to "bar",
and got the latter on MsgBox.

Yet, sometimes I really want to work with MyVar in another file,
and be able to get it back (to the already running first file) with changes.

I just don't know how to do that properly.

8
  • On the second thought, the better and elegant way to implement this is probably to make 1 big file with all Subs I need; add some description to it (or smth, to ease the navigation), and then just CreateObject("WScript.Shell").Run this big file with specific arguments from smaller one-line files, "fixed" for any situation. Commented Jun 3, 2022 at 8:49
  • And by "this" (the initial problem to solve) I mean, like.. having all essential lines of code written and reachable, and not having to rewrite them over and over again. Commented Jun 3, 2022 at 9:15
  • Here is a basic idea of navigation in big text file via Notepad++. One could use some symbols to mark "the chapters" (or Subs really); and then use some custom script to collect all lines having these symbols to the "chapter-list" file. Commented Jun 3, 2022 at 9:26
  • 1
    I just put everything in one file broken into plenty of subs and functions, but some people like to use ExecutGlobal to make a kind of include like this example. Commented Jun 3, 2022 at 12:57
  • 1
    NotePad++ is a good editor, but you may be able to work more effectively using VBSEdit which is a complete VBScript IDE. Commented Jun 3, 2022 at 13:02

1 Answer 1

1

Option 1: keep the files as they are, and add an echo at the end of your sub:

2.vbs:

'(...)
'3) Doing some work with MyVar.
MyVar = "bar"
Wscript.Echo(MyVar)
'(...)

1.vbs

Read the output by changing the method ".Run" to ".Exec"

'(...)
'2) Passing MyVar to 2.vbs.
set oShell = CreateObject("Wscript.Shell")
set Exe = oShell.Exec("cmd /c 2.vbs """&MyVar&""" ")
MyVar = Exe.StdOut.ReadAll
'(...)
'4) End point.
MsgBox MyVar

Option 2: Using the "2.vbs" file as a function library and changing the sub to function:

1.vbs

'Put the import statements at the top of 1.vbs code for easier readability.
Import "2.vbs"
'(...)
MyVar = MyFunction(MyVar)

2.vbs

Function MyFunction(MyVar)
'3) Doing some work with MyVar.
MyFunction = "bar"
end Function
Sign up to request clarification or add additional context in comments.

1 Comment

Never heard of Import before; gotta look into it, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.