cherdeg Posted July 29, 2009 Share Posted July 29, 2009 (edited) Hi, In one of my scripts I am calling a function to create two text files, a .txt and a.bat. The .txt is the body of a mail to be sent by blat.exe. The .bat is the file that installs and executes blat.exe. The function's purpose is to create the files, run the batch and delete the files. Problem is that the files aren't deleted and I've got no clue why. Here's the code: expandcollapse popupResultMailer($s_BaseDir, $s_MailExe, $s_MailRelay, $s_CurrentMailFrom, $s_MailBody, $s_CurrentMailSubject, $s_MailTo, $s_CurrentReportDirectory & "\" & $a_Hostnames[$i_j] & "-LOGLOOKER" & "-" & $s_Date & "-summary.txt") ; The function ResultMailer() writes the batchfile to install and execute blat.exe ; ================================================================================================== Func ResultMailer($s_BaseDir, $s_MailerExecutableName, $s_MailRelay, $s_MailFrom, $s_MailBody, $s_MailSubject, $s_MailTo, $s_TextFile) ; Create the file containing the mails body text $MailBodyFile = $s_BaseDir & "\_MailBody.txt" _FileCreate($MailBodyFile) FileOpen($MailBodyFile, 2) FileWriteLine($MailBodyFile, $s_MailBody) FileClose($MailBodyFile) ; Assemble the commandlines to be written to the mailer-batch $s_cmd_blat_ins = $s_MailerExecutableName & " -install " & $s_MailRelay & " " & $s_MailFrom $s_cmd_blat_exe = $s_MailerExecutableName & ' ' & '"' & $MailBodyFile & '"' & ' -to ' & $s_MailTo & ' -subject ' & '"' & $s_MailSubject & '"' & ' -i ' & $s_MailFrom & ' -attach ' & '"' & $s_TextFile & '"' ; Create the individual mailer-batch to install and execute blat.exe $s_BatchFileName = $s_BaseDir & "\_MailBatch.bat" _FileCreate($s_BatchFileName) FileOpen($s_BatchFileName, 2) FileWriteLine($s_BatchFileName, "REM This file (" & $s_BatchFileName & ") was created by NTcontrol.") FileWriteLine($s_BatchFileName, "REM") FileWriteLine($s_BatchFileName, $s_cmd_blat_ins) FileWriteLine($s_BatchFileName, $s_cmd_blat_exe) FileWriteLine($s_BatchFileName, "REM") FileWriteLine($s_BatchFileName, "REM This file (" & $s_BatchFileName & ") was created by NTcontrol.") FileClose($s_BatchFileName) ; Execute the mailer-batch $s_cmd = $s_BatchFileName RunWait(@ComSpec & " /c " & $s_cmd, $s_BaseDir, @SW_SHOW) ; Delete the mailer-batch While FileExists($MailBodyFile) Or ($s_BatchFileName) FileDelete($MailBodyFile) FileDelete($s_BatchFileName) WEnd EndFunc Any ideas? Regards, Chris Edited July 29, 2009 by cherdeg Link to comment Share on other sites More sharing options...
somdcomputerguy Posted July 29, 2009 Share Posted July 29, 2009 Perhaps FileDelete and FileExists don't accept handles? In the FileWrite example, a handle is specified, but not in the other two examples. - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
99ojo Posted July 29, 2009 Share Posted July 29, 2009 Perhaps FileDelete and FileExists don't accept handles? In the FileWrite example, a handle is specified, but not in the other two examples.Hi,cherdeg uses not filehandles but declared variables. This should work.@cherdeg:1) Change your while loop:While FileExists($MailBodyFile) Or ($s_BatchFileName)toWhile FileExists($MailBodyFile) Or FileExists ($s_BatchFileName) MsgBox (0,"",$MailBodyFile & @CRLF & $s_BatchFileName) ; for debugging reason. See what is shown for your variablesBut why you don't use a simple if:If FileExist ($MailBodyFile) then FileDelete ($MailBodyFile)If FileExist ($s_BatchFileName) then FileDelete ($s_BatchFileName);-))Stefan Link to comment Share on other sites More sharing options...
cherdeg Posted July 29, 2009 Author Share Posted July 29, 2009 Perhaps FileDelete and FileExists don't accept handles? In the FileWrite example, a handle is specified, but not in the other two examples.Sorry, but nowhere in my code handles are used. The variables do only contain strings of type "drive:\path\filename". Link to comment Share on other sites More sharing options...
cherdeg Posted July 29, 2009 Author Share Posted July 29, 2009 1) Change your while loop:While FileExists($MailBodyFile) Or FileExists ($s_BatchFileName) MsgBox (0,"",$MailBodyFile & @CRLF & $s_BatchFileName) ; for debugging reason. See what is shown for your variablesWendI already did (I always do before writing to a forum): Variables are correct. >_<But why you don't use a simple if:If FileExist ($MailBodyFile) then FileDelete ($MailBodyFile)If FileExist ($s_BatchFileName) then FileDelete ($s_BatchFileName)I did (1st code), but the files weren't deleted using this method. So I thought: "Hmmm, maybe they are still in use by something, let's make a while loop to try until the locks are gone".What now? Link to comment Share on other sites More sharing options...
99ojo Posted July 29, 2009 Share Posted July 29, 2009 I already did (I always do before writing to a forum): Variables are correct. >_< I did (1st code), but the files weren't deleted using this method. So I thought: "Hmmm, maybe they are still in use by something, let's make a while loop to try until the locks are gone".What now?Hi,FileClose expects FileHandles -> so change FileOpen($MailBodyFile, 2) to $fileMailBody = FileOpen($MailBodyFile, 2) AndFileClose ($MailBodyFile) to FileClose ($fileMailBody)AndFileOpen($s_BatchFileName, 2) to $fileBatchFile = FileOpen($s_BatchFileName, 2)AndFileClose ($s_BatchFileName) to FileClose ($fileBatchFile)Then all files should be closed and your delete must work!;-))Stefan Link to comment Share on other sites More sharing options...
somdcomputerguy Posted July 29, 2009 Share Posted July 29, 2009 I was incorrectly using the term 'handle' when I should've said 'variable'. Seems I was wrong anyway. It was just an assumption though. Hope you get this issue resolved. - Bruce /*somdcomputerguy */ If you change the way you look at things, the things you look at change. Link to comment Share on other sites More sharing options...
cherdeg Posted July 29, 2009 Author Share Posted July 29, 2009 (edited) FileClose expects FileHandles -> so change Yep - you're right. Shit! >_< I could have found this easy solutions by myself. Using: $MailBodyFileHandle = FileOpen($MailBodyFile, 2) [...] $s_BatchFileNameHandle = FileOpen($s_BatchFileName, 2) [...] While FileExists($MailBodyFile) = 1 Or ($s_BatchFileName) = 1 FileDelete($MailBodyFile) FileDelete($s_BatchFileName) Sleep(200) WEnd ...works perfect! Never the less: thank you - YOU made my day! BTW. My original while...wend was crap also...it wouldn't ever have quit. This is not my day. Best Regards, Chris Edited July 29, 2009 by cherdeg Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now