Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for delayrun.vbs

(view source code of delayrun.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim i, intDelay, strArguments, wshShell
  4.  
  5. ' Check command line arguments
  6. If WScript.Arguments.Count < 2           Then Syntax
  7. If InStr( WScript.Arguments(0), "/" )    Then Syntax
  8. If InStr( WScript.Arguments(0), "?" )    Then Syntax
  9. If InStr( WScript.Arguments(1), "/" )    Then Syntax
  10. If InStr( WScript.Arguments(1), "?" )    Then Syntax
  11. If Not IsNumeric( WScript.Arguments(0) ) Then Syntax
  12. If CInt( WScript.Arguments(0) ) <= 0     Then Syntax
  13.  
  14. ' First command line argument is the delay in seconds
  15. intDelay = 1000 * CInt( WScript.Arguments(0) )
  16.  
  17. ' The second and following arguments make up the command
  18. strArguments = """" & WScript.Arguments(1) & """"
  19. If WScript.Arguments.Count > 2 Then
  20. 	For i = 2 To WScript.Arguments.Count - 1
  21. 		strArguments = Trim( strArguments & " " & WScript.Arguments(i) )
  22. 	Next
  23. End If
  24.  
  25. ' Wait for the specified number of seconds
  26. WScript.Sleep intDelay
  27.  
  28. ' Start the command/program
  29. Set wshShell = CreateObject( "WScript.Shell" )
  30. wshShell.Run strArguments, 1, False
  31. Set wshShell = Nothing
  32.  
  33.  
  34. Sub Syntax
  35. 	Dim strMsg
  36. 	strMsg = "DelayRun.vbs,  Version 1.02" _
  37. 	       & vbCrLf _
  38. 	       & "Start a command after a delay" _
  39. 	       & vbCrLf & vbCrLf _
  40. 	       & "Usage:  DELAYRUN.VBS  delay  some_command  [ some_arguments ]" _
  41. 	       & vbCrLf & vbCrLf _
  42. 	       & "Where:  ""delay""           is the delay in seconds" _
  43. 	       & vbCrLf _
  44. 	       & "        ""some_command""    is the command to be run after the delay" _
  45. 	       & vbCrLf _
  46. 	       & "        ""some_arguments""  are optional arguments for ""some_command""" _
  47. 	       & vbCrLf & vbCrLf _
  48. 	       & "Notes:  Use this script to prevent ""traffic jams"" in your Startup folder:" _
  49. 	       & vbCrLf _
  50. 	       & "        modify the command line for each shortcut in the Startup folder," _
  51. 	       & vbCrLf _
  52. 	       & "        using this script to start each shortcut with a different delay." _
  53. 	       & vbCrLf _
  54. 	       & "        You may have to convert double quotes to single ones in the third" _
  55. 	       & vbCrLf _
  56. 	       & "        and following arguments." _
  57. 	       & vbCrLf _
  58. 	       & "        The working directory for ""some_command"" is this script's own" _
  59. 	       & vbCrLf _
  60. 	       & "        working directory (not to be confused with its own location)." _
  61. 	       & vbCrLf & vbCrLf _
  62. 	       & "Written by Rob van der Woude" & vbCrLf _
  63. 	       & "http://www.robvanderwoude.com"
  64. 	WScript.Echo strMsg
  65. 	WScript.Quit 1
  66. End Sub
  67.  

page last uploaded: 2016-12-15, 11:20