Tuesday, March 20, 2007 12:33 AM royashbrook

fork.vbs

this script has always had a fond place in my heart. here it is in it's original unadulterated glory!!!

' fork.vbs
' Roy Ashbrook - NT Sys Admin Group
' syntax:
'  - fork.vbs infile outfile command batchsize
' purpose:
'  - circumvent ability to only run one instance of a given shell command at a time.
' details:
'  - reads in a list of machines from the infile
'  - executes "command" however many times it needs to be run.
'  - groups the executions into sizes of "batchsize"
'  - at the end of every "batch" it reads the results in, then writes them to outfile
' notes:
'  - only works on files with a single argument of whatever is on the line in the input file.
'  - execution time, performance, etc depends on your computer and what you are doing.
'  - you'll probably have to experiment to get the best performance.
' created:
'  - 20030629
' last updated:
'  - 20030710

'strict
  Option Explicit

'check args
  IF wscript.arguments.count <> 4 THEN
    wscript.echo "syntax fork.vbs infile outfile command batchsize"
    wscript.quit
  END IF

wscript.echo "Setting up program environment...."
'dim
  dim strInFile, strOutFile, strCommand, strCurrentDirectory, strTempFolder, strTempFile, strPC, x, xx, strTemp
  dim arrIN, arrPL()
  dim fso, oShell, file, objOutFile, objTempFolder
  dim colFiles
  dim i, intTotalProcessCount, intProcessBatchAmount, pCount, intLastProcess, s, iL

'setup
  s = timer
  strInFile    = wscript.arguments.item(0)
  strOutFile    = wscript.arguments.item(1)
  strCommand    = wscript.arguments.item(2)
  intProcessBatchAmount  = Cint(wscript.arguments.item(3))
  set fso      = CreateObject("Scripting.FileSystemObject")
  set oShell    = CreateObject("Wscript.Shell")
  strCurrentDirectory  = fso.GetParentFolderName(wscript.scriptfullname)
  strInFile    = strCurrentDirectory & "\" & strInFile
  strOutFile    = strCurrentDirectory & "\" & strOutFile
  strTempFolder    = fso.GetSpecialFolder(2) & "\" & fso.GetTempName

wscript.echo "Deleting outfile if it exists...." & strOutFile
  if fso.fileexists(strOutFile) then fso.deletefile(strOutFile)

wscript.echo "Creating new outfile...." & strOutFile
  set objOutFile    = fso.OpenTextFile(strOutFile,8,true,0)

wscript.echo "Creating temp folder...." & strTempFolder
  fso.CreateFolder(strTempFolder)
  set objTempFolder  = fso.getfolder(strTempFolder)

wscript.echo "Preparing to fork the processes..."
  pCount      = 0
  arrIN      = fso.opentextfile(strInFile).readall
  arrIN      = split(arrIN,vbcrlf)
  intLastProcess    = ubound(arrIN)

wscript.echo "Begin Forking!."
'process
  for i = 0 to intLastProcess
    x = trim(ucase(arrIN(i)))
    pCount = pCount + 1
wscript.echo "processing " & i+1 & " of " & intLastProcess+1 & "CMD /C " & strCommand & " " & x & " > " & strTempFolder & "\" & i & ".tmp"
    oshell.run "CMD /C " & strCommand & " " & x & " > " & strTempFolder & "\" & i & ".tmp" ,0,false
    IF pCount = intProcessBatchAmount OR i = intLastProcess THEN
wscript.echo "batch number reached, reading in log files"
wscript.echo vbtab & "waiting for files to appear in the temp folder"
      while objTempFolder.Files.count = 0
        wscript.echo "no files yet, sleeping"
        wscript.sleep 10
      wend
wscript.echo vbtab & "files located, looping until they are all read in."
      while objTempFolder.files.count <> 0 and iL <> 10
wscript.echo vbtab & "loop start"
        for each file in objTempFolder.files
          xx = strTempFolder & "\" & file.name
wscript.echo vbtab & vbtab & "processing file " & xx & ", resetting file loop to 0"
iL = 0
          on error resume next
wscript.echo vbtab & vbtab & "starting loop for file " & xx & "will kick out when file doesn't exist, or it's looped 10 times"
          while fso.fileexists(xx) and iL <> 10
wscript.echo vbtab & vbtab & vbtab & "file exists! clearing any errors, then attempting to read it!"
            err.clear
            strTemp = ""
            strTemp = fso.opentextfile(xx,1).readall
            IF err.number = 0 then
wscript.echo vbtab & vbtab & vbtab & "file read! No Errors so far! Attempting to Delete File"
              fso.deletefile(xx)
              if err.number = 0 then
wscript.echo vbtab & vbtab & vbtab & "file deleted! writing strtemp to outfile"

wscript.echo "strtemp is " & strtemp
wscript.echo vbtab & vbtab & "writing this file out! looping until it writes!"
          while cint(objOutFile.Write(strTemp)) <> 0
wscript.echo vbtab & vbtab & "DANG! " & err.description & err.number & " file NOT written! sleeping for 1/4 second!"
            wscript.sleep 250
          wend
wscript.echo vbtab & vbtab & "done with this file!"
wscript.echo vbtab & vbtab & vbtab & "this loop should fail the condition check now"
              ELSE
wscript.echo vbtab & vbtab & vbtab & "DANG! " & err.description & err.number & " file NOT deleted! sleeping for 1/4 second! should loop again since file still exists"
              wscript.sleep 250
              end if
            ELSE
wscript.echo vbtab & vbtab & vbtab & "DANG! " & err.description & err.number & " file NOT read! sleeping for 1/4 second! should loop again since file still exists"
            wscript.sleep 250
            END IF
wscript.echo vbtab & vbtab & vbtab & "end file exists loop # " & iL & ", incrementing loop count"
iL = iL + 1
          wend
          on error goto 0
wscript.echo vbtab & vbtab & xx & "should be gone now, should be read in. or loop count was reached"
        next
wscript.echo vbtab & "loop end. done with all the files, checking folder file count to see if i need to loop again"
      if i = intLastProcess then iL = 0
      wend
wscript.echo vbtab & "done readin in files or loop counter was reached, reseting pcount and loop counter"
iL = 0
      pCount = 0
    END IF
  next

wscript.echo "End Forking!"

wscript.echo "Performing Cleanup..."
'cleanup
  fso.DeleteFolder(strTempFolder)
  objOutFile.Close
  set objTempFolder = NOTHING
  set fso    = Nothing
  set oShell  = Nothing

'EchoTimer
  s = timer - s
  wscript.echo "Execution Time: " & s & " s"

Filed under:

Comments

No Comments