Help calling a vbs from a bat

Discussion in 'Programming & Software Development' started by UserInterface, Aug 9, 2013.

  1. UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    I have a bat and at the end I wish to call a vbs that finds and replaces text in a file but the vbs also has a inputbox variable for what to replace the text with but when I run the bat it just seems to end the script there. I put a pause after that line and it still just terminates before preforming that line.

    I call a vbs just before this command and it works fine, but i think the inputbox is stopping it some how.

    Can some one help me with this I just can not work out a way around it.

    Quick edit, I should have said that I can run the VBS from a bat like so
    But if I put that line at the bottom of my main script it just gets ignored..

    I even tried the test bat that had the line above from my main bat and it was skipped again (even though it works by itself)
     
    Last edited: Aug 9, 2013
  2. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,419
    try calling it with wscript rather than cscript

    can you post a basic example?
     
  3. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    Sure its a stupid program that is way to old and does not have a proper installer.

    It used to be run in 5 batch files and after that we would have to open a properties file and change some text relative to the user however I am trying to find a way to push this out though SCCM eventually so am trying to run it all from one bat file.

    Below is the whole bat for installing and it all works fine, I just need to get the one line "cscript %~dp0ReplaceTA.vbs" working

    Code:
    ECHO ON
    REM Install Java
    "%~dp0JRE15011\English\Disk1\Setup.exe" /s
    
    PING -n 20 127.0.0.1>nul
    
    REM Turbo Installation
    "%~dp0Turbo70\English\Disk1\Setup.exe" /s
    
    PING -n 20 127.0.0.1>nul
    
    REM Shared Data Installation
    "%~dp0Qik200704T70\English\Disk1\Setup.exe" /s
    
    REM File Copy
    Del /F "C:\Users\Public\Public Desktop\Turbo Sabre v7.0.lnk"
    Del /F "C:\Users\Public\Public Desktop\Traveller Plus.lnk"
    Del /F "C:\Users\%USERNAME%\Desktop\Traveller Plus.lnk"
    Del /F "C:\ProgramData\Microsoft\Windows\Start Menu\Traveller Plus.lnk"
    Del /F "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Traveller Plus.lnk"
    Del /F "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\\Traveller Plus.lnk"
    
    echo d | xcopy /C /E /Y "%~dp0C7.0\7.0" "C:\Sabre\Apps\Turbo\7.0"
    echo d | xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\Users\Public\Public Desktop\"
    xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\ProgramData\Microsoft\Windows\Start Menu\"
    xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\"
    xcopy /C /Y "%~dp0CWindows\Turboico.dll" "C:\Windows"
    xcopy /C /Y "\\127.0.0.1\cwtsource$\ThickClient\MatrixSupCode\Supplier*.xls" "C:\Sabre"
    echo d | xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\"
    
    PING -n 20 127.0.0.1>nul
    
    "%~dp0Script\qikchange.bat"
    
    PING -n 20 127.0.0.1>nul
    
    "cscript %~dp0ReplaceTA.vbs"
    
    REM Install Traveller Plus
    "%~dp0TravellerPlus.msi" /qn
    
    PING -n 40 127.0.0.1>nul
    
    Pause
    the only thing that I have changed is the ip to one of our servers, and you can ignore all the pings as they are just there while I am testing and I know that it works with out them.

    This is the vbs script that it calls.

    Code:
    Const ForReading = 1
    Const ForWriting = 2
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\SABRE\Apps\Turbo\7.0\app\qik.properties", ForReading)
    
    strText = objFile.ReadAll
    strNewTA = Inputbox("Please enter your Traveller Plus TA","TA Script","TA=ABC123")
    
    objFile.Close
    strNewText = Replace(strText, "TA=ABCDEF", strNewTA)
    
    Set objFile = objFSO.OpenTextFile("C:\SABRE\Apps\Turbo\7.0\app\qik.properties", ForWriting)
    objFile.WriteLine strNewText
    objFile.Close
    I just opens that file and finds the text TA=ABCDEF and changes it to whats in the inputbox. again this works by its self just not called from the main script above, although it does work from a bat with just the one line calling it..

    Just tried with wscript and same out come.
     
    Last edited: Aug 9, 2013
  4. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,419
    TestingVBS.bat
    Code:
    echo some stuff goes here
    echo some more stuff goes here
    pause
    echo now it should launch the vbs
    cscript "%~dp0test.vbs"
    echo %~dp0test.vbs
    pause
    
    
    test.vbs
    Code:
    result = InputBox("Hello", "Test", , 100, 200)
    msgbox(result)
    

    the following works ok for me on Windows 7, launched from both local drive, local drive with a space in the path, and UNC path.

    If %~dp0test.vbs ends up with to many chars ( >260) in it, it might break something.
     
  5. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    This what i don't get

    If i have a bat
    Code:
    "cscript %~dp0ReplaceTA.vbs"
    and use this to call the vbs it works fine.

    But when I put that line in the end of my script it just gets ignored.


    Is there a way to run the main script and some how find out why it is skipping? at the moment it just gets to the end and closes, even with the pause there.
     
  6. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,419
    Bad quotes

    chuck an 'echo' in front of it and see what its actually trying to execute. Does it work if you change it to

    cscript.exe "%~dp0ReplaceTA.vbs

    I debug my batch scripts with liberal use of echo and pause.
     
  7. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    Do you mean this

    Code:
    cscript.exe "%~dp0ReplaceTA.vbs"
    If so I tried that first.

    I'll try echo in a second, i just reverted to a snap shot and have to rejoin to domain..
     
  8. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    The main bat already has Echo On at the start is that what you mean?
     
  9. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,419
    Yes I do mean that :).

    Can does the simplified example I posted above work for you?

    What does qikchange.bat have in it?
     
  10. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,419
    Heres what I think is happening.

    Code:
    ECHO ON
    REM Install Java
    "%~dp0JRE15011\English\Disk1\Setup.exe" /s
    
    PING -n 20 127.0.0.1>nul
    
    REM Turbo Installation
    "%~dp0Turbo70\English\Disk1\Setup.exe" /s
    
    PING -n 20 127.0.0.1>nul
    
    REM Shared Data Installation
    "%~dp0Qik200704T70\English\Disk1\Setup.exe" /s
    
    REM File Copy
    Del /F "C:\Users\Public\Public Desktop\Turbo Sabre v7.0.lnk"
    Del /F "C:\Users\Public\Public Desktop\Traveller Plus.lnk"
    Del /F "C:\Users\%USERNAME%\Desktop\Traveller Plus.lnk"
    Del /F "C:\ProgramData\Microsoft\Windows\Start Menu\Traveller Plus.lnk"
    Del /F "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Traveller Plus.lnk"
    Del /F "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\\Traveller Plus.lnk"
    
    echo d | xcopy /C /E /Y "%~dp0C7.0\7.0" "C:\Sabre\Apps\Turbo\7.0"
    echo d | xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\Users\Public\Public Desktop\"
    xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\ProgramData\Microsoft\Windows\Start Menu\"
    xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\"
    xcopy /C /Y "%~dp0CWindows\Turboico.dll" "C:\Windows"
    xcopy /C /Y "\\127.0.0.1\cwtsource$\ThickClient\MatrixSupCode\Supplier*.xls" "C:\Sabre"
    echo d | xcopy /C /Y "%~dp0CDesktop\Traveller Plus.lnk" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\"
    
    PING -n 20 127.0.0.1>nul
    
    This bit installs all your shit.

    Code:
    "%~dp0Script\qikchange.bat"
    
    and then launches qikchange.bat once qikchange.bat completes. the script is considered complete. and...

    Code:
    PING -n 20 127.0.0.1>nul
    "cscript %~dp0ReplaceTA.vbs"
    REM Install Traveller Plus
    "%~dp0TravellerPlus.msi" /qn
    PING -n 40 127.0.0.1>nul
    Pause
    This bit never gets run.

    Change

    Code:
    "%~dp0Script\qikchange.bat"
    to

    Code:
    Call "%~dp0Script\qikchange.bat"
    And see if it works.

    http://stackoverflow.com/questions/...file-without-prepending-call-to-the-line-exit

    Also... instead of pinging around the place to introduce delays in your script. check timeout.exe

    Code:
    C:\Scripts>timeout /?
    
    TIMEOUT [/T] timeout [/NOBREAK]
    
    Description:
        This utility accepts a timeout parameter to wait for the specified
        time period (in seconds) or until any key is pressed. It also
        accepts a parameter to ignore the key press.
    
    Parameter List:
        /T        timeout       Specifies the number of seconds to wait.
                                Valid range is -1 to 99999 seconds.
    
        /NOBREAK                Ignore key presses and wait specified time.
    
        /?                      Displays this help message.
    
    NOTE: A timeout value of -1 means to wait indefinitely for a key press.
    
    Examples:
        TIMEOUT /?
        TIMEOUT /T 10
        TIMEOUT /T 300 /NOBREAK
        TIMEOUT /T -1
    
     
  11. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    qikchange.bat was not created by me, and to be honest I didn't look in it as It was all part of the original scripts. Now I am thinking that was a dumb mistake..

    how dumb?

    I just looked at it and it calls a VBS script that is doing what I am doing!

    It looks in the qik.properties file and finds a line called hostaccess.sabre.commlink.JSAPI.lineIATA=
    and just puts the damn ABCDEF at the end, then I was opening the same file after it and trying to change the ABCDEF to what the user inputs!

    OMG I am freaking dumb..

    I am going to see if I can just use the one script now..
     
  12. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,419
    launching scripts without reading/understanding them, especially on a Friday afternoon is fraught with danger :).

    I'd just get up the vendor and ask them to produce a sane installer.
     
  13. revhed

    revhed Member

    Joined:
    Aug 22, 2001
    Messages:
    1,528
    Location:
    Darwin
    So more Virgin flight disruptions this afternoon when the script goes live? :p
     
  14. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    We have been complaining to them forever, this app is from 2007 and has not been updated since then. Its a app that we use to connect to the airlines database to book flights etc, you would think that it would get regular updates.. Then we have another app that does the same thing and although that gets regular updates it has no way to install silently at all.

    I just removed his script and used mine as I know what it does and it works!
    I wasted so long on that, defiantly learned my lesson on this one!

    Stupid thing is that the old script was not even working, when the program installs it creates the file with the ABCDEF in it already so his script does nothing!!!

    Thanks for helping with this, I really could not work out what was going on and have been working on it all day.

    @ revhed Sabre makes this program and that was who went down the other day but that was at there end , this is another client that uses that same database.. see what I mean about expecting it to be updated more often :)

    Now I can get it installing with less head ache but all this just made me more scared of having to get it into SCCM with a variable from the user.
     
    Last edited: Aug 9, 2013
  15. revhed

    revhed Member

    Joined:
    Aug 22, 2001
    Messages:
    1,528
    Location:
    Darwin
    The aviation industry abhors change ;) Seriously though. I expect you're paying them truckloads of money for a service. That gives to the leverage to "encourage" them to fix their shit.
     
  16. PabloEscobar

    PabloEscobar Member

    Joined:
    Jan 28, 2008
    Messages:
    14,419
    Never trust user input!

    Can't you make a table of username:TA and have the script look it up?
     
  17. Tekin

    Tekin Member

    Joined:
    Nov 16, 2002
    Messages:
    4,039
    Location:
    Elsewhere.
    Wow. Batch scripting and vba. I thought I WAS the only one stupid enough to still be building them.

    Seconded to what pabloescabar says. Never trust user input. Either santise it so it only accepts the 'correct' input, or better yet have them select off options.

    Users are both stupid, and also incredibly bright of coming up with ways of breaking things that you never thought breakable.
     
  18. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    That would be awesome but so far above my skill level, I do have them all in a spread sheet so maybe I will look at it, but to be honest I wouldn't even know how to start..

    Let me see what Google has to say on the topic.
     
  19. heydonms

    heydonms Member

    Joined:
    Sep 15, 2008
    Messages:
    629
    Once you start reading data from spreadsheets, you really want to be using powershell rather than batch files.
     
  20. OP
    OP
    UserInterface

    UserInterface Member

    Joined:
    Jan 13, 2011
    Messages:
    360
    I was just looking at changing the script to look up the data instead of a prompt and found this scripting guy article that explains most of it, but I am unsure how to split the found cell to locate the one that I wish to copy

    i.e. I have this for a spread sheet

    Code:
    First, Last, UID, data, TA, data, data...
    I can get the UID and then find its cell in the case above C1, and return that as a variable, but how can I then use that variable and take the C and make it find C5

    sorry if this does not make sense, I'm not sure how to explain it..
     

Share This Page

Advertisement: