Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control DOS program with script

Status
Not open for further replies.

UltraSmooth

Programmer
Oct 28, 2002
97
CA
I need to loop through a set of commands in a dos program via script.

Bascially I need a script to do the following:

1. Launch DOS App

Loop N times
2. Enter text in window
3. Send "Enter Key" (x2)
4. Enter text
5. Send "Enter Key" (x3)
Exit Loop

6. Sent "F5 Key" (x2)
7. Send "Enter Key"

Is it possible to automate interaction with a dos program in such a way? I've tried googling it and haven't had much luck. If anyone has any links I can read on how to do it, if even possible, I'd really appreciate it.
 
It is possible. It is ugly and unreliable but possible. Search this forum for "SendKeys". You may also want to look at a product called AutoIt which makes this sort of thing easier.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
WshShell.Run "yourapp.exe"
AppActivate "yourappwindowname" 'shouldnt be required as focus should shift
WshShell.SendKeys "stuff"

i wrote a script which takes a txt file as input, the txt file takes run=,sendkeys=,msgbox=,wait4ie etc so you keep the script the same and change your input accordingly

or, you might want to consider AutoIt, i dont like it as you end up with exe which if something in your environment changes you have to create againk,,doesnt make it as flexible if you ask me
 
Option Explicit
Dim FSO, WshShell
Dim strTemp, strAnswerFile, strURL
Dim oSourceTS, sLine, sKey, sVal, intEquals, appIE
Dim Computer, strCurrentFolder

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
strCurrentFolder = UCase(Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName) - Len(Wscript.ScriptName) - 1))

If Wscript.Arguments.Count = 2 Then
strAnswerFile = Wscript.Arguments.Item(0)
strURL = Wscript.Arguments.Item(1)
Else
Msgbox "already installed, please uninstall"
Wscript.Echo 1 'wrong number of arguments
End If

'strFile2Launch = " to automate"
If Left(LCase(strURL), 4) = "http" Then
Set appIE = CreateObject("InternetExplorer.Application")
appIE.ToolBar = True
appIE.StatusBar = True
appIE.Visible = True
'appIE.FullScreen = True
appIE.Height = 640
appIE.Width = 480
appIE.Left = 50
appIE.Top = 50
appIE.Navigate strURL
Else
If FSO.FileExists(strURL) Then
appIE = WshShell.Run(strURL, 1, False)
End If
End If

If FSO.FileExists(strAnswerFile) Then
'Set Computer = New clsMachine
Set oSourceTS = FSO.OpenTextFile(strAnswerFile, 1, False)
Do While Not oSourceTS.AtEndOfStream
sLine = ""
sLine = Trim(oSourceTS.ReadLine)
If sLine <> "" Then
If Not Left(sLine, 1) = "'" Then
If InStr(sLine, " '") Then
sLine = Left(sLine, InStr(sLine, " '") - 1)
End If
sLine = WshShell.ExpandEnvironmentStrings(sLine)
intEquals = InStr(1, sLine, "=")
If (intEquals <= 1) Then
'msgbox "error: the following line is invalid " & sLine
Else
'weve found a valid line
sKey = Trim(LCase(Left(sLine, intEquals - 1)))
sVal = Right(sLine, Len(sLine) - intEquals)
'sLine = Replace(sLine, "^computername^", Computer.NetbiosName)
'sLine = Replace(sLine, "^fqdomainname^", Computer.FQDomainName)
'sLine = Replace(sLine, "^domainname^", Computer.DomainName)
Select Case sKey
Case "sendkeys"
If UCase(sVal) = "{ENTER}" Then
'Msgbox "going to press enter"
End If
'Msgbox sVal & "$"
WshShell.SendKeys sVal
Wscript.Sleep 500
Case "wait4ie"
waitBusy appIE
Case "sleep"
If IsNumeric(sVal) Then
If CInt(sVal) < 60001 Then
If CInt(sVal) > 0 Then
Wscript.Sleep sVal
End If
End If
End If
Case "msgbox"
Msgbox sVal
Case "popup"
WshShell.Popup sVal, 3
Case "appactivate"
WshShell.AppActivate sVal
Case "run"
If InStr(LCase(sVal), "%currentfolder%") Then
sVal = Replace(sVal, "%currentfolder%", strCurrentFolder)
End If
If Right(LCase(sVal), 5) = ";false" Then
WshShell.Run Left(sVal, Len(sVal) - 6), 1, False
Else
WshShell.Run sVal, 1, True
End If
Case "space"
WshShell.SendKeys " "
End Select
'
End If

End If
End If
Loop
oSourceTS.Close
Set oSourceTS = Nothing
End If

'Set Computer = Nothing
Set FSO = Nothing
Set WshShell = Nothing


'#####################################################################
'### sub to wait for IE to stop being busy..timeout?
Sub waitBusy(ByRef appIE)
Dim iCount
iCount = 0
If IsObject(appIE) Then
Do
If appIE.Busy = True Then
Wscript.Echo "ie is busy,,,sleeping"
Wscript.Sleep 500
Else
Exit Do
End If
'get out cluase?
iCount = iCount + 1
If iCount = 240 Then
Exit Do
End If
Loop
End If
End Sub


'example input file
AppActivate=Internet Explorer
sleep=10000
sendkeys={TAB 11}
sendkeys={ENTER}
run=
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top