none
VBS Script Questions - Variables RRS feed

  • Question

  • Hi Folks -

    I have this VBS script that I want to add variable too.

    I'm unable to activate the variables as they are now. I assume my quotes are wrong? Can someone assist?

    Thank you!

    Dim VAR1
    VAR1 = GJA_PROCESS
    
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "CMD /C taskkill /f /fi ""WINDOWTITLE eq Administrator:  VAR1"" & C:\Hyperion_Batch\Scripts\Batch\VAR1.cmd VAR1",0
    Set objShell = Nothing

    • Moved by Reed KimbleMVP Wednesday, April 19, 2017 11:52 AM VBA script
    Wednesday, April 19, 2017 11:49 AM

All replies

  • You've posted to the VB.Net language forum; this forum is not for VBA (which is quite different than VB.Net).  I will move this to a scripting forum.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, April 19, 2017 11:50 AM
  • You've posted to the VB.Net language forum; this forum is not for VBA (which is quite different than VB.Net).  I will move this to a scripting forum.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    More suitable for The Official Scripting Guys Forum! perhaps?

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, April 19, 2017 2:13 PM
  • I can't help much, but I can help w/ the quoting.

    The resulting string that is sent to the shell for execution is:

    CMD /C taskkill /f /fi "WINDOWTITLE eq Administrator:  VAR1" & C:\Hyperion_Batch\Scripts\Batch\VAR1.cmd VAR1

    Unlike other languages, vba does not do the variable (VAR1) replacement for you in a string operation. Further assuming VAR1 is a String variable, and the result you want is:

      CMD /C taskkill /f /fi "WINDOWTITLE eq Administrator:GJA_PROCESS" C:\Hyperion_Batch\Scripts\Batch\GJA_PROCESS.cmd

    then the snippet you shared needs to be something like:

    Dim VAR1 as String
    VAR1 = "GJA_PROCESS"
    
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "CMD /C taskkill /f /fi ""WINDOWTITLE eq Administrator:  " & VAR1 & """ C:\Hyperion_Batch\Scripts\Batch\" & VAR1 & ".cmd ",0
    Set objShell = Nothing


    -MainSleuth You've Got It, Use It! Engineering, Science, Statistics Solutions http://ToolSleuth.com. For any reply that either helps to answer your question or is the answer, please mark it as helpful or as the answer so others with the same question will have an answer quickly.


    • Edited by MainSleuth Wednesday, April 19, 2017 3:23 PM copy/paste error
    Wednesday, April 19, 2017 3:21 PM
  • Thank you for your help!

    However, I'm getting an "expected end of statement" error.

    Also you'll notice in my orginal request I'm trying to pass a param to the batch file im calling.

    So i tired something like:

    Dim VAR1 as String
    VAR1 = "GJA_PROCESS"
    
    Set objShell = CreateObject("WScript.Shell")
    objShell.Run "CMD /C taskkill /f /fi ""WINDOWTITLE eq Administrator:  " & VAR1 & """ C:\Hyperion_Batch\Scripts\Batch\" & VAR1 & ".cmd "" & VAR1"",0
    Set objShell = Nothing

    But still not working. Any recommendations?

    Thanks!


    • Edited by cdtakacs1 Wednesday, April 19, 2017 4:47 PM
    Wednesday, April 19, 2017 4:46 PM
  • The thing to remember w/ vba is that one double quote (") starts a string; from that point, any Double-double quote ("") results in a double quote in the string, and finally, a closing double quote(") terminates the string. An Ampersand (&)  joins strings. """The Quick " & "Brown Fox""" => "The Quick Brown Fox"

    And if strPrefix = """The Quick", then
    strPrefix & "Brown Fox""" => "The Quick Brown Fox"

    If I didn't want open or closing quotes, then I'd skip the internal Double quote sets. So if

    if strPrefix2 = "The Quick", then
    strPrefix2 & "Brown Fox" => The Quick Brown Fox

    Since I am unfamiliar with the resulting taskkill string you actually need, Here's a couple choices:

    objShell.Run "CMD /C taskkill /f /fi ""WINDOWTITLE eq Administrator:" & VAR1 & """ C:\Hyperion_Batch\Scripts\Batch\" & VAR1 & ".cmd " & VAR1 & ",0"

    will result in this statement sent to the shell to be run:

    CMD /C taskkill /f /fi "WINDOWTITLE eq Administrator:GJA_PROCESS" C:\Hyperion_Batch\Scripts\Batch\GJA_PROCESS.cmd GJA_PROCESS,0

    <OR>

    objShell.Run "CMD /C taskkill /f /fi ""WINDOWTITLE eq Administrator:" & VAR1 & """ C:\Hyperion_Batch\Scripts\Batch\" & VAR1 & ".cmd """ & VAR1 & """,0"

    will result in this statement sent to the shell to be run:

    CMD /C taskkill /f /fi "WINDOWTITLE eq Administrator:GJA_PROCESS" C:\Hyperion_Batch\Scripts\Batch\GJA_PROCESS.cmd "GJA_PROCESS",0

    <Then again>

    It might be better if you post what the actual statement you are passing to the shell is.


    -MainSleuth You've Got It, Use It! Engineering, Science, Statistics Solutions http://ToolSleuth.com. For any reply that either helps to answer your question or is the answer, please mark it as helpful or as the answer so others with the same question will have an answer quickly.


    • Edited by MainSleuth Thursday, April 20, 2017 1:35 AM
    Thursday, April 20, 2017 1:34 AM
  • Thank you! Although, niether option worked.

    Let me break it down for you.  Here is the syntax without variables:

    objShell.Run "CMD /C taskkill /f /fi ""WINDOWTITLE eq Administrator:  GJA_PROCESS"" & C:\Hyperion_Batch\Scripts\Batch\GJA_PROCESS.cmd GJA_PROCESS",0

    The first piece I am sending to the CMD session is the syntax to kill any CMD sessions with a title of GJA_PROCESS

    taskkill /f /fi ""WINDOWTITLE eq Administrator:  GJA_PROCESS""

    Then using the & symbol, I'm sending another operation to the cmd session which is to start a cmd script called GJA_PROCESS.cmd as well as pass it parameter called GJA_PROCESS

    & C:\Hyperion_Batch\Scripts\Batch\GJA_PROCESS.cmd GJA_PROCESS

    Does that make sense? So its two parts. Without the variables this works fine...Just need to incorporate the variables...

    Note the ampersand above is for the cmd session, NOT vbs. Sorry if I wasn't clear.

    Thanks!


    • Edited by cdtakacs1 Thursday, April 20, 2017 7:39 AM
    Thursday, April 20, 2017 7:34 AM
  • Ah - didn't correctly understand use of the &. Learned something - Precisely why I choose to respond in these forums.

    objShell.Run "CMD /C taskkill /f /fi ""WINDOWTITLE eq Administrator:  " & VAR1 & """ & C:\Hyperion_Batch\Scripts\Batch\" & VAR1 & ".cmd " & VAR1 & ",0"


    -MainSleuth You've Got It, Use It! Engineering, Science, Statistics Solutions http://ToolSleuth.com. For any reply that either helps to answer your question or is the answer, please mark it as helpful or as the answer so others with the same question will have an answer quickly.

    Thursday, April 20, 2017 5:51 PM