none
Command prompt issue from VBscript RRS feed

  • Question

  • So I have this script that works perfectly for parsing out a COM number and then sending data to said COM port via the command line (things like 'ipconfig > //./COM15'). The issue is that it only works when I use the /K modifier which tells the command windows to stay open.  I don't want them to stay open, but whenever I remove the /K, it doesn't work. Nothing gets copied to the COM port. I suspect the cause of this is the command prompt closing before things like systeminfo can finish running. So is there a way to have the command prompt window stay open for a short amount of time and then close?

    'Part 1: FindCOM port
    fileName = "C:\rtlstuff\COM.txt"
    Set regex = New RegExp
    regex.Pattern = "COM(\d+)"
    regex.IgnoreCase = True
    regex.Global = True
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(fileName)
    text = file.ReadAll()
    file.Close
    Dim ComPort
    Set matches = regex.Execute(text)
    For Each m In matches
    ComPort = m.SubMatches(0)
    Next
    
    'Part 2: Set file dictionary
    set oDic = CreateObject("Scripting.Dictionary")
    oDic.Add 0, "ECHO %DATE% %TIME% > //./COM" 
    oDic.Add 1, "ipconfig > //./COM" 
    oDic.Add 2, "systeminfo > //./COM" 
    v = 3
    
    'Part 3: Set COM Variables
    Set oShell = WScript.CreateObject ("WScript.Shell")
    MODE = "MODE COM" & ComPort & " 9600,N,8,1,P"
    oShell.run "cmd.exe /C" & MODE
    
    'Part 4: Data transmission
    Dim Command
    x=0
    do while x < v
    Command = oDic.Item(x) & Comport
    msgbox Command
    oShell.run "cmd.exe " & Command
    x=x+1
    wscript.sleep 10000
    loop
    
    
     


    Wednesday, July 30, 2014 5:43 PM

Answers

  • Can you help me with syntax? I'm still not hugely familiar with VB and I'm not sure how to set this up. This current iteration does not work.

    . . . then you should download vbscript.chm from the Microsoft site and study its numerous examples. This is being pro-active vs. being helpless.

    Instead of writing
    oShell.run "cmd.exe /C" & MODE
    you should write
    oShell.run "cmd.exe /C " & MODE, 1, True

    Without examining your code in detail I suspect that you need to resolve a few minor problems, e.g.
    - Add at least one space after "cmd.exe /C"
    - Add the "/C " switch when using the Run method near the end of your code.

    Wednesday, July 30, 2014 8:27 PM

All replies

  • Here is the full syntax of the Run method, copied straight from the downloadable help file script56.chm. As you see, it uses a boolean switch to force a wait until the command has completed whatever it is doing.

    object.Run(strCommand, [intWindowStyle], [bWaitOnReturn])

    Wednesday, July 30, 2014 7:00 PM
  • Can you help me with syntax? I'm still not hugely familiar with VB and I'm not sure how to set this up. This current iteration does not work.

    'Part 1: FindCOM port
    fileName = "C:\rtlstuff\COM.txt"
    Set regex = New RegExp
    regex.Pattern = "COM(\d+)"
    regex.IgnoreCase = True
    regex.Global = True
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set file = fso.OpenTextFile(fileName)
    text = file.ReadAll()
    file.Close
    Dim ComPort
    Set matches = regex.Execute(text)
    For Each m In matches
    ComPort = m.SubMatches(0)
    Next
    
    'Part 2: Set file dictionary
    set oDic = CreateObject("Scripting.Dictionary")
    oDic.Add 0, "ECHO %DATE% %TIME% > //./COM" 
    oDic.Add 1, "ipconfig > //./COM" 
    oDic.Add 2, "systeminfo > //./COM" 
    v = 3
    
    'Part 3: Set COM Variables\par
    Set oShell = WScript.CreateObject ("WScript.Shell")
    MODE = "MODE COM" & ComPort & " 9600,N,8,1,P"
    oShell.run "cmd.exe /C" & MODE
    
    'Part 4: Data transmission
    Dim Command
    i = True
    x=0
    do while x < v
    Command = oDic.Item(x) & Comport
    msgbox Command
    oShell.run "cmd.exe " & Command , ,True
    x=x+1
    wscript.sleep 10000
    loop
    



    Wednesday, July 30, 2014 7:49 PM
  • Can you help me with syntax? I'm still not hugely familiar with VB and I'm not sure how to set this up. This current iteration does not work.

    . . . then you should download vbscript.chm from the Microsoft site and study its numerous examples. This is being pro-active vs. being helpless.

    Instead of writing
    oShell.run "cmd.exe /C" & MODE
    you should write
    oShell.run "cmd.exe /C " & MODE, 1, True

    Without examining your code in detail I suspect that you need to resolve a few minor problems, e.g.
    - Add at least one space after "cmd.exe /C"
    - Add the "/C " switch when using the Run method near the end of your code.

    Wednesday, July 30, 2014 8:27 PM
  • What's the point of this script?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 30, 2014 9:31 PM
    Moderator
  • It is a really bad way to try and send commands to a com port. It can more easily be done with a batch file.

    It is also possible to open the com port with the FileSystemObject and just write to the port with write commands,

    It is a "poor man's" com library.


    ¯\_(ツ)_/¯

    Wednesday, July 30, 2014 11:03 PM
  • Wednesday, July 30, 2014 11:05 PM
  • Thanks, I think I'll try writing a Batch file.
    Thursday, July 31, 2014 3:11 PM
  • Wow, batch makes this way easier, now the hard part will be working my regex to find the comport number into the batch.
    Thursday, July 31, 2014 3:17 PM