Sign in to follow this  
Followers 0
cherdeg

filedelete problem

8 posts in this topic

#1 ·  Posted (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:

ResultMailer($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 by cherdeg

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

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)

to

While FileExists($MailBodyFile) Or FileExists ($s_BatchFileName)

MsgBox (0,"",$MailBodyFile & @CRLF & $s_BatchFileName) ; for debugging reason. See what is shown for your variables

But why you don't use a simple if:

If FileExist ($MailBodyFile) then FileDelete ($MailBodyFile)

If FileExist ($s_BatchFileName) then FileDelete ($s_BatchFileName)

;-))

Stefan

Share this post


Link to post
Share on other sites

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".

Share this post


Link to post
Share on other sites

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 variables

Wend

I 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?

Share this post


Link to post
Share on other sites

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)

And

FileClose ($MailBodyFile) to FileClose ($fileMailBody)

And

FileOpen($s_BatchFileName, 2) to $fileBatchFile = FileOpen($s_BatchFileName, 2)

And

FileClose ($s_BatchFileName) to FileClose ($fileBatchFile)

Then all files should be closed and your delete must work!

;-))

Stefan

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 by cherdeg

Share this post


Link to post
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
Sign in to follow this  
Followers 0