three simple batch helpers

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

three simple batch helpers

Post by blueznl »

not sure where to put this, but these three little files make my life a lot easier when dealing with batch files and (auto) launching windows...

when you put stuff in your start menu, it's hard to tell what runs first, also i found out that windows (for whatever reason) launches items from the startup part SLOWER than from a batch file (beats me why)

here's a sample startup.bat which is the only entry listed in the startup menu (yeah, sounds like back to the old autoexec.bat days, doesn't it? :-))

as programming examples these are not great, but they show one aspect of purebasic that i like: it's darn easy to build your own little command line tools

below, i use skiprun.exe to check for a window, and if it isn't there i launch a program, quite simple
c:
cd "c:\software\backgnds"
wallnut.exe
skiprun synergy* "c:\program files\synergy\synergys.exe" -c synergy.sgc -d WARNING
skiprun dtemp* "c:\software\dtemp\dtemp.exe"
skiprun cacheman* "c:\program files\cacheman\cacheman.exe"
skiprun ultramon* "c:\program files\ultramon\ultramon.exe"
net stop browser
net use x: \\serv1\e$ wizzars1 /USER:admin /PERSISTENT:YES
net use y: \\serv1\f$ wizzars1 /USER:admin /PERSISTENT:YES
net use z: \\serv1\g$ wizzars1 /USER:admin /PERSISTENT:YES
rundll32.exe user32.dll, LockWorkStation
and now the three programs...

findwindow

Code: Select all

IncludeFile "..\x_lib\x_lib.pb"
x_init()
;
p.s = ProgramParameter()
;
OpenConsole()
If p = "/?" Or p = "?" Or p = ""
  PrintN("")
  PrintN("findwindow v1.1 c2004 WackoWare:")
  PrintN("  check existence of a window from a batch file")
  PrintN("  returns error level 1 when found")
  PrintN("")
  PrintN("wildcards supported:")
  PrintN("  * - any combination and number of characters")
  PrintN("  ? - any single character")
  PrintN ("")
  PrintN("syntax:")
  PrintN("  findwindow <windowname>")
  PrintN("")
  PrintN("example of usage in a batchfile:")
  PrintN("  findwindow trillian")
  PrintN("  if errorlevel 1 goto trillianloaded")
  PrintN("    trillian.lnk")
  PrintN("  :trillianloaded")
  PrintN("")
  PrintN("notes:")
  PrintN("  enclose parameters with quotes to include spaces")
  PrintN("  see also waitrun and skiprun")
  PrintN("")
Else
  ;
  Print("looking for '"+p+"'... ")
  If x_findwindow(p,#True) <> 0
    PrintN("found")
    r = 1
  Else
    PrintN("not found")
    r = 0
  EndIf
EndIf
;
x_end()
;
End

skiprun

Code: Select all

IncludeFile "..\x_lib\x_lib.pb"
x_init()
;
w.s = ProgramParameter()
f.s = ProgramParameter()
q.s = Chr(34)
;
OpenConsole()
If w = "/?" Or w = "?" Or w = "" Or f = ""
  ;
  ; show help
  ;
  PrintN("")
  PrintN("skiprun v1.0 c2006 WackoWare:")
  PrintN("  check existence of a window from a batch file")
  PrintN("  run the specified program when not found")
  PrintN("")
  PrintN("wildcards supported (as windowname):")
  PrintN("  * - any combination and number of characters")
  PrintN("  ? - any single character")
  PrintN ("")
  PrintN("syntax:")
  PrintN("  findwindow <windowname> <path+program>")
  PrintN("")
  PrintN("example of usage in a batchfile:")
  PrintN("  skiprun trillian "+q+"c:\program files\trllian\trillian.exe"+q)
  PrintN("")
  PrintN("notes:")
  PrintN("  skiprun will NOT wait for the called program to finish")
  PrintN("  skiprun will run the called program inside its own folder")
  PrintN("  enclose parameters with quotes to include spaces")
  PrintN("  trayicons often have associated windows")
  PrintN("  see also findwindow and waitrun")
  PrintN("")
Else
  ;
  ; check if the window exists
  ;
  Print("looking for '"+w+"'... ")
  If x_findwindow(w,#True) <> 0
    ;
    ; exists, do nothing
    ;
    PrintN("found")
    r = 1
  Else
    ;
    ; doesn't exist, so start the program
    ;
    PrintN("not found")
    c.s = ""
    p.s = ""
    Repeat                                        ; grab all parameters
      c = ProgramParameter()
      If FindString(c," ",1) > 0                  ; if the parameter contains spaces then pass it on with surrounding quotes (makes sense doesn't it?)
        c = q+c+q
      EndIf
      If c > ""
        p = p+" "+c
      EndIf
    Until c = ""                                  ; got all parameters
    PrintN("launching "+q+f+q+" "+P)
    RunProgram(q+f+q,p,GetPathPart(f),0)          ; now launch it
    r = 0
  EndIf
EndIf
;
x_end()
;
End r
waitrun

Code: Select all

x.s = ProgramParameter()
n.l = Val(x)
f.s = ProgramParameter()
q.s = Chr(34)
;
OpenConsole()
If x = "/?" Or x = "?" Or x = ""
  ;
  ; show help
  ;
  PrintN("")
  PrintN("waitrun v2.0 c2004..2006 WackoWare:")
  PrintN("  wait for 'n' seconds then launch specified program")
  PrintN("")
  PrintN("syntax:")
  PrintN("  waitrun <delay> [ <path+program> [ <parameters> ]]")
  PrintN("")
  PrintN("example of usage in a batchfile:")
  PrintN("  c:\software\utils\waitrun.exe 3 "+q+"c:\program files\ultramon\ultramon.exe"+q+" /r")
  PrintN("")
  PrintN("notes:")
  PrintN("  waitrun will NOT wait for the called program to finish")
  PrintN("  waitrun will run the called program inside its own folder")
  PrintN("  enclose parameters with quotes to include spaces")
  PrintN("  see also findwindow and skiprun")
  PrintN("")
Else
  ;
  ; do the wait :-)
  ;
  If n > 0
    Print("waiting "+Str(n)+" seconds...")
    nn = 0
    While nn < n
      WindowEvent()
      Delay(1024)
      nn = nn+1
      Print(".")
    Wend
    PrintN("")
    PrintN("done")
  EndIf
  ;
  ; start the specified program
  ;
  If f > ""
    c.s = ""
    p.s = ""
    Repeat                                        ; grab all parameters
      c = ProgramParameter()
      If FindString(c," ",1) > 0                  ; if the parameter contains spaces then pass it on with surrounding quotes (makes sense doesn't it?)
        c = q+c+q
      EndIf
      If c > ""
        p = p+" "+c
      EndIf
    Until c = ""                                  ; got all parameters
    PrintN("launching "+q+f+q+" "+p)
    RunProgram(q+f+q,p,GetPathPart(f),0)          ; now launch it
  EndIf
EndIf
;
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )