[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1669
  • Last Modified:

VBScript send keys to window based on window title

I have a small VBScript that I use to execute some backup operations on my hard drive by calling DriveImage XML. Basically, the VBS file creates some folders and runs DriveImageXML in order to make an image of the C:\ drive. A scheduled task runs it every couple of weeks.

By the way, this code works. You guys can use it if you like! :D

Now, this is usually all automated, however recently I've developed a bad spot on my hard drive, so I get "Data Cyclic Redundency" popups at about the 30% mark of doing the image. I have to click "Ignore All" to make the warning go away and finish the backup.

I'd like to make the VBScript check for the presence of that error window, and send the "I" key to the window to make it go away. Is there any way to do that? I think SendKeys() will work, but I don't know how to check the title of the active window.

Attached in my VBSCript code.
' Backup Script
' Performs a backup of C:\ using DriveImage XML.
 
  Option Explicit
  Dim oFSO
  Set oFSO = CreateObject("Scripting.FileSystemObject")
  Dim cDir
  cDir = "Z:\LatitudeImage-" & Year(Now()) & "-" & Month(Now()) & "-" & Day(Now())
 
  // Create the directory
  If Not oFSO.FolderExists(cDir) Then
    oFSO.CreateFolder(cDir)
 
    // Generate a file name
    Dim cFile
    cFile = cDir & "\Drive_C"
    Dim cCmd
 
     // This is the shell command to run diXML with appropriate command line params
     cCmd = """C:\Program files\Runtime Software\DriveImage XML\dixml.exe"" /bc /t""" & cFile & """"
     // GO!
     ExecuteShellProgram(cCmd)
     
  End If
 
   
  Function ExecuteShellProgram(ByVal sFilename)
    Dim poShell
    Dim poProcess
    Dim iStatus
 
    // Create a shell instance
    Set poShell = CreateObject("WScript.Shell")
 
    // This is asynchronous. Script ends immediately.
    Set poProcess = poShell.Exec(sFilename)
  End Function

Open in new window

0
Frosty555
Asked:
Frosty555
  • 3
  • 2
1 Solution
 
TakedaTCommented:
Give this a try.  I dont have the right setup to test the code myself, so let me know if it works or not.  All the changes I made were in the function at the bottom.  Why were you using // for comment lines?
' Backup Script
' Performs a backup of C:\ using DriveImage XML.
 
Option Explicit
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
Dim cDir
cDir = "Z:\LatitudeImage-" & Year(Now()) & "-" & Month(Now()) & "-" & Day(Now())
 
	  'Create the directory
If Not oFSO.FolderExists(cDir) Then
    oFSO.CreateFolder(cDir)
    'Generate a file name
	Dim cFile
	cFile = cDir & "\Drive_C"
	Dim cCmd
     'This is the shell command to run diXML with appropriate command line params
	cCmd = """C:\Program files\Runtime Software\DriveImage XML\dixml.exe"" /bc /t""" & cFile & """"
     'GO!
	ExecuteShellProgram(cCmd)
End If
 
Function ExecuteShellProgram(ByVal sFilename)
    Dim poShell, poProcess, iStatus, Success
    'Create a shell instance
    Set poShell = CreateObject("WScript.Shell")
     'This is asynchronous. Script ends immediately.
    Set poProcess = poShell.Exec(sFilename)
 	Do while poProcess.status = 0
		Success = False
		wscript.sleep 1000
		Success = poShell.AppActivate("WINDOWTITLEHERE")
		If Success then
			wscript.sleep 1000
			poShell.SendKeys "I{ENTER}"
		End if    
	Loop 
End Function

Open in new window

0
 
Frosty555Author Commented:
TakedaT,

I threw those comments in just before posting. Must have used C style comments instead of VB style comments.

The only issue I see with your code is that AppActivate is a sub. It throws an exception if it fails to find a process with the appropriate window title.

Is there an equivalent to try...catch in VBscript we can use to catch that error?
0
 
TakedaTCommented:
It shouldnt throw an exception, it should just keep looping until it does.  I wasnt sure if it would work with the exec method of the shell object.  Maybe we should try with the run method?  The reason I didnt use it at first is because there would then need to be a way to break the loop when the program is finished running.  

One thing I always ran into problems with is using extra double quotes on cmd lines.  I always found it best to use chr(34) instead in a lot of places to make sure they are passed to the run line properly.  Can you show me the error you get?
0
 
TakedaTCommented:
I just tested that portion of the script and it worked fine for me.  What OS are you using? and what is the window title?  Can you provide a screenshot of the popup?
0
 
Frosty555Author Commented:
Hmm... nevermind it does work!
That was surprising. Lol.

Thank you!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now