Jump to content

Invalid file handle used ?


Recommended Posts

Howdy All!!

I am working on a script to create other scripts that will install printers on my network and I keep getting the error :

C:\make printscripts.au3 (23) : ==> Invalid file handle used.:

I can't understand why this is happening. The code is below.

$prsrver = InputBox("What Print Server ?", "Please enter the name of the print server that this list of printers is on/for :")

$serverDir = @SCRIPTDIR & '\'& $prsrver &'_Printers'
$mkSrvDir = DirCreate($serverDir)

$AddDir = $serverDir & "\Add"
$RemDir = $serverDir & "\Remove"

$mkAddDir = DirCreate($AddDir)
$mkRemDir = DirCreate($RemDir)


$rlist = FileOpen(@scriptdir & '\printer_list.txt',0)

; Check if file opened for reading OK
If $rlist = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $Qname = FileReadLine($rlist)
    If @error = -1 Then ExitLoop
        $nAs = $AddDir &'\'& $Qname &'_Add.au3'
    $newAddFile = FileOpen($nAs, 2)
    ; begin writing lines
            $line1 = FileWriteLine($newAddFile, ';  Script to add printer '& $Qname &' from '& $prsrver) 
            $line2 = FileWriteLine($newAddFile, ';  ')
            $line3 = FileWriteLine($newAddFile, '$cmd1 = "rundll32 PrintUI.dll,PrintUIEntry /ga /n \\'& $prsrver &'\'& $Qname &' , 1, ALL_USERS"')
            $line4 = FileWriteLine($newAddFile, ';  ')
            $line5 = FileWriteLine($newAddFile, '$go = run($cmd1)')
            $line6 = FileWriteLine($newAddFile, ';  ')
            $line7 = FileWriteLine($newAddFile, '$sptCmd = @COMSPEC &" /c NET STOP SPOOLER"')
            $line8 = FileWriteLine($newAddFile, '$spsCmd = @COMSPEC &" /c NET START SPOOLER"')
            $line9 = FileWriteLine($newAddFile, ';  ')
            $line10 = FileWriteLine($newAddFile, '$go1 = runwait($sptCmd, "", @SW_HIDE)')
            $line11 = FileWriteLine($newAddFile, '$go2 = runwait($spsCmd, "", @SW_HIDE)')
            $line12 = FileWriteLine($newAddFile, ';  ')
            $line13 = FileWriteLine($newAddFile, ';  ')
            $line14 = FileWriteLine($newAddFile, '$done = MsgBox(0, "Added !", "The printer '& $Qname &' on \\'& $prsrver &' has been Added to this machine!!", 2)')
            $line15 = FileWriteLine($newAddFile, ';  ')
            $line16 = FileWriteLine($newAddFile, 'exit')
    ; end writing lines
    $cl = FileClose($nAs)
    ;$OutExe = $AddDir &'\'& $Qname &'_Add.exe'
    ;$compileCmd = @COMSPEC & ' /c Aut2Exe.exe /in '& $nAs &' /out '& $OutExe 
    ;$go5 = runwait($compileCmd,"",@SW_HIDE)
    msgbox(0,"test",$Qname)
    $nRs = $RemDir &'\'& $Qname &'_Rem.au3'
    $newRemFile = FileOpen($nRs, 2)
    ; begin writing lines
            $line1 = FileWriteLine($newRemFile, ';  Script to remove printer '& $Qname &' from '& $prsrver)
            $line2 = FileWriteLine($newRemFile, ";  ")
            $line3 = FileWriteLine($newRemFile, '$cmd1 = "rundll32 PrintUI.dll,PrintUIEntry /gd /n \\'& $prsrver &'\'& $Qname &' , 1, ALL_USERS"')
            $line4 = FileWriteLine($newRemFile, ';  ')
            $line5 = FileWriteLine($newRemFile, '$go = run($cmd1)')
            $line6 = FileWriteLine($newRemFile, ';  ')
            $line7 = FileWriteLine($newRemFile, '$sptCmd = @COMSPEC &" /c NET STOP SPOOLER"')
            $line8 = FileWriteLine($newRemFile, '$spsCmd = @COMSPEC &" /c NET START SPOOLER"')
            $line9 = FileWriteLine($newRemFile, ';  ')
            $line10 = FileWriteLine($newRemFile, ';  ')
            $line11 = FileWriteLine($newRemFile, '$go1 = runwait($sptCmd, "", @SW_HIDE)')
            $line12 = FileWriteLine($newRemFile, '$go2 = runwait($spsCmd, "", @SW_HIDE)')
            $line13 = FileWriteLine($newRemFile, ';  ')
            $line14 = FileWriteLine($newRemFile, '$done = MsgBox(0, "Added !", "The printer '& $Qname &' on \\'& $prsrver &' has been Removed from this machine!!", 2)')
            $line15 = FileWriteLine($newRemFile, ';  ')
            $line16 = FileWriteLine($newRemFile, 'exit')
    ; end writing lines
    $cl = FileClose($nRs)
    ;$OutExe = $RemDir &'\'& $Qname &'_Rem.exe'
    ;$compileCmd = @COMSPEC & ' /c Aut2Exe.exe /in '& $nRs &' /out '& $OutExe 
    ;$go5 = runwait($compileCmd,"",@SW_HIDE)
    msgbox(0,"test",$Qname)

Wend

I'd rather laugh with the sinners than cry with the saints..... The sinners are much more fun....Only the good die young. -- Billy Joel

Link to comment
Share on other sites

This is just a shot in the dark, but because you did this:

$newRemFile = FileOpen($nRs, 2)

You should be doing this:

$cl = FileClose($newRemFile)

Not this:

$cl = FileClose($nRs)

and the same for this:

$cl = FileClose($nAs)

Should be:

$cl = FileClose($newAddFile)

Let me know if that fixes it?

Edited by automagician
Link to comment
Share on other sites

Thats the answer !!! Thanks ..... I guess I didn't quite get the FileClose() thing !!

This is just a shot in the dark, but because you did this:

$newRemFile = FileOpen($nRs, 2)

You should be doing this:

$cl = FileClose($newRemFile)

Not this:

$cl = FileClose($nRs)

and the same for this:

$cl = FileClose($nAs)

Should be:

$cl = FileClose($newAddFile)

<{POST_SNAPBACK}>

I'd rather laugh with the sinners than cry with the saints..... The sinners are much more fun....Only the good die young. -- Billy Joel

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...