I have an inherited script that currently will not close excel and if a temp file is missing it throws an error.
I need an If/Else and then send the script back to the beginning of the loop.
I am going to add the lines for closing excel inside the loop.
This is the script - I didn't include the full sqlcmd lines because the lines are huge.
 Code:
$setup = "\\server\share\folder\names.txt"

$thismo = @monthno -1

IF $thismo = 0
  $month = "12"

ELSE
  IF $thismo > 9
    $month = CSTR($thismo)
  ELSE
    $month = "0" + CSTR($thismo)
  ENDIF
ENDIF
$year = IIf(@MONTHNO = 1, @YEAR -1, @YEAR)

;open txt file with doc names 
IF Open(1, "$setup") <> 0
BEEP
? "spec file not opened, error code: [" + @ERROR + "]"
sleep 5
EXIT
ENDIF
Dim $accessApp

$accessApp = CreateObject("Access.Application")
;$accessApp.visible = 1
$accessApp.OpenCurrentDatabase("\\server\share\folder\file.mdb")

$accessApp.DoCmd.RunMacro("exportdocs")

sleep 10

;main program loop

WHILE $readerr = 0
$x = ReadLine(1)
$readerr = @ERROR
IF $readerr <> 0
EXIT
ENDIF
$key = SUBSTR($x, 1, INSTR($x, ",")-1)
$x = SUBSTR($x, INSTR($x, ",")+1, LEN($x) - INSTR($x, ","))
$drname = SUBSTR($x, 1, LEN($x) - INSTR($x, ","))



$accessApp.DoCmd.RunSQL("SELECT [prod2]![drno] AS [Key], prod2.class, Sum(prod2.Oct_amt) AS Oct_amt, Sum(prod2.Sep_amt) AS Sep_amt, Sum(prod2.Tot_amt) AS Tot_amt INTO tempsum
FROM prod2
GROUP BY [prod2]![no], prod2.class
HAVING ((([prod2]![no])=$key));")
Sleep 1

$accessApp.DoCmd.RunSQL("SELECT tempsum.Key, Sum(tempsum.Oct_amt) AS Oct_amt, Sum(tempsum.Sep_amt) AS Sep_amt, Sum(tempsum.Tot_amt) AS Tot_amt INTO temptot
FROM tempsum
GROUP BY tempsum.Key;")

sleep 1

$accessApp.DoCmd.RunSQL("SELECT tempsum.Key, classdesc.description, tempsum.Oct_amt, [tempsum]![Oct_amt]/[temptot]![Oct_amt] AS [Oct_pct],  tempsum.Sep_amt,  [tempsum]![Sep_amt]/[temptot]![Sep_amt] AS [Sep_pct], tempsum.Tot_amt, [tempsum]![Tot_amt]/[temptot]![Tot_amt] AS [Tot_pct] INTO docpct
FROM temptot, tempsum INNER JOIN classdesc ON tempsum.class = classdesc.class;")
sleep 1

$accessApp.DoCmd.RunMacro("exportnamepct")


sleep 1

;added below to loop
$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0

; open excel file apptutil created by Access macro above
;  format and save with correct name
$app = CreateObject("Excel.Application")
;$app.visible = 1

;Create the Workbooks object
$wbs = $app.Workbooks
;this is what I need the If Else statement for
$rc = $wbs.Open("\\server\share\folder\temp.xls")


$app.Range("d1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("f1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("h1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("j1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("l1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("n1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("p1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("r1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("t1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("v1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("x1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("z1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("ab1").Entirecolumn.NumberFormat = "0.0%"
$app.Range("c1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("e1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("g1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("i1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("k1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("m1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("o1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("q1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("s1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("u1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("w1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("y1").Entirecolumn.NumberFormat = "#,##0"
$app.Range("aa1").Entirecolumn.NumberFormat = "#,##0"


$app.Range("c24").Formula = "=SUM(c1:c23)"
$app.Range("e24").Formula = "=SUM(e1:e23)"
$app.Range("g24").Formula = "=SUM(g1:g23)"
$app.Range("i24").Formula = "=SUM(i1:i23)"
$app.Range("k24").Formula = "=SUM(k1:k23)"
$app.Range("m24").Formula = "=SUM(m1:m23)"
$app.Range("o24").Formula = "=SUM(o1:o23)"
$app.Range("q24").Formula = "=SUM(q1:q23)"
$app.Range("s24").Formula = "=SUM(s1:s23)"
$app.Range("u24").Formula = "=SUM(u1:u23)"
$app.Range("w24").Formula = "=SUM(w1:w23)"
$app.Range("y24").Formula = "=SUM(y1:y23)"
$app.Range("aa24").Formula = "=SUM(aa1:aa23)"

$app.Range("A1:AB1").insert
$app.Range("A1:AB1").merge
$array = "Fiscal Year 2011"
$app.Range("A1:AB2").HorizontalAlignment = 3 
$app.Range("A1:AB1").Value = $array
$app.Range("A1:AB2").Font.Bold = 1
$app.Range("A1:AB2").Borders.LineStyle = 1
$app.Range("A1:AB2").Borders.Weight = 1
$app.Range("A1:AB1").EntireColumn.AutoFit
;$app.Range("A1:AB2").Interior.ColorIndex = 15

$filename = "\\server\shares\folder\$name-$key-$year-$month.xls"
$app.ActiveWorkbook.SaveAs("$filename",-4143) 


Del "\\server\share\folder\temp.xls"
$wbs = 0
;added below to loop
$app.DisplayAlerts = 0
$app.Quit
$app = 0

LOOP

;IMPORTANT--YOU MUST CLOSE THE WORKBOOKS OBJECT OR ELSE
;			THE EXCEL PROCESS WILL NOT TERMINATE!!!
$wbs.Close

;Destroy the Worksheet, Workbooks, and Application objects

$wbs = 0
$app.DisplayAlerts = 0
$app.Quit
$app = 0

$accessApp.DisplayAlerts = 0
$accessApp.Quit
$accessApp = 0



sleep 1



So I need to check if the temp.xls is there, if not, return to the top of the loop.
I think I can remove the following at the end of the script:
 Code:
;IMPORTANT--YOU MUST CLOSE THE WORKBOOKS OBJECT OR ELSE
;			THE EXCEL PROCESS WILL NOT TERMINATE!!!
$wbs.Close

;Destroy the Worksheet, Workbooks, and Application objects

$wbs = 0
$app.DisplayAlerts = 0
$app.Quit
$app = 0