Sign in to follow this  
Followers 0
ezzetabi

Delete damn it!

10 posts in this topic

#1 ·  Posted (edited)

Thanks to the newest unstable it is possible make the DeleteOnReboot function completly in Autoit...

Here it is:

Func _DeleteFile($FILE) ;Only deletes a single file.
                        ;No error checking. Just select a file. Use loops if needed.
   Local $PENDING
   If Not FileExists($FILE) Then Return -1
   If FileDelete($FILE) Then Return 1
   
  ;If the code arrives here it means that the file is in-use and so
  ;it will be deleted at the reboot.
   
   If @OSTYPE = "WIN32_NT" Then
      $PENDING = RegRead("HKLM\System\CurrentControlSet\Control\Session Manager",_
      "PendingFileRenameOperations")
      If $PENDING = "" Then
         RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager",_
         "PendingFileRenameOperations", "reg_multi_sz",_
         "\??\" & FileGetLongName($FILE) & @LF)
      Else
         RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager",_
         "PendingFileRenameOperations", "reg_multi_sz", $PENDING & @LF & @LF & "\??\" &_
         FileGetLongName($FILE) & @LF)
      EndIf
   Else
      If @OSVersion = "WinME" Then Return 1024
      FileCopy("c:\autoexec.bat", "c:\delrbt.tmp",1)
      $PENDING = FileRead("c:\autoexec.bat", FileGetSize("c:\autoexec.bat"))
      FileDelete("c:\autoexec.bat")
      FileWrite("c:\autoexec.bat", "del " & FileGetShortName($FILE) & @CRLF)
      FileWrite("c:\autoexec.bat", $PENDING)
      FileWrite("c:\autoexec.bat", @CRLF & "move c:\delrbt.tmp c:\autoexec.bat")
   EndIf
   Return 0
EndFunc  ;==>_DeleteFile
Edited by ezzetabi

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

For Win9x there's a simmilar function: wininit.ini

Afair the syntax was

[rename]

NEWNAME1=OLDNAME1

NEWNAME2=OLDNAME2

and by setting NEWNAME to "NUL" the file would be deleted on the next reboot.

Since wininit.exe (which is used by windows to go through this file is run under real DOS, there are no long filenames!

Edited by sugi

Share this post


Link to post
Share on other sites

Since IniWrite cant works well in multiple key called NUL I'll keep my implementation.

Share this post


Link to post
Share on other sites

Thank you ezzetabi for that function.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ok sugi, you win. I did those for the unluckly ones that really use the evil WinMe.

Now I implemented the wininit.ini idea, much more professional like..

I also added initial comments in #include files style...

As usual copy it at the end of your script before using.

Func _DeleteFile($FILE);Only deletes a single file. Use loops if needed
   
  ; Description:      Try to delete a file, if it fails it deletes it reboot time.
  ; Syntax:           _DeleteFile( $filename )
  ; Parameter(s):     $filename - The name of the file to delete. No wildcards.
  ; Return Value(s):  -2 You used wildcards (* or ?), no action taken.
  ;                   -1 The file does not exist
  ;                    0 The file has already been deleted
  ;                    1 The file will be deleted boot time
  ; Author(s):        Ezzetabi ezzetabi@katamail.com and I like receving...
   
   Local $PENDING, $C
   If StringInStr($FILE, "*") Or StringInStr($FILE, "?") Then Return -2
   If Not StringInStr($FILE, "\") Then $FILE = @WorkingDir & "\" & $FILE;Adds the full path
   
   If Not FileExists($FILE) Then Return -1
   If FileDelete($FILE) Then Return 0
   
  ;If the code arrives here it means that the file is in-use and so
  ;it will be deleted at the reboot.
   
   If @OSTYPE = "WIN32_NT" Then;The Kernel is W2000
      $PENDING = RegRead("HKLM\System\CurrentControlSet\Control\Session Manager", _
            "PendingFileRenameOperations")
      If $PENDING = "" Then
         RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager", _
               "PendingFileRenameOperations", "reg_multi_sz", _
               "\??\" & $FILE & @LF)
      Else
         RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager", _
               "PendingFileRenameOperations", "reg_multi_sz", $PENDING & @LF & @LF & "\??\" & _
               $FILE & @LF)
      EndIf
   Else;The kernel is Win9x
      $FILE = FileGetShortName($FILE)
      $PENDING = FileRead(@WindowsDir & "\wininit.ini", FileGetSize(@WindowsDir & "\wininit.ini"))
      If @error = 1 Then  ;The file wininit.ini DOES NOT exist.
         FileWrite(@WindowsDir & "\wininit.ini", "[rename]" & @CRLF & _
               "NUL=" & $FILE)
      Else                ;The file wininit.ini EXISTS
         Local $POS, $C
         $POS = StringInStr($PENDING, "[rename]")
         If $POS = 0 Then;The [Rename] section is not there.
            FileWrite(@WindowsDir & "\wininit.ini", @CRLF & "[rename]" & @CRLF & _
                  "NUL=" & $FILE);... So it is just added at the end.
         Else;The section [rename] is there
            $POS = $POS + 9;(7 of the word [rename] and 2 of @crlf)
            $C = StringRight($PENDING, StringLen($PENDING) - $POS)
            $PENDING = StringTrimRight($PENDING, StringLen($PENDING) - $POS)
          ;$Pending contain the whole wininit.ini, I removed the right part after [rename]
          ;after coping it in $C
            $PENDING = $PENDING & "NUL=" & $FILE & @CRLF & $C
          ;I add the line for deleting. And re-add the right part removed.
            FileDelete(@WindowsDir & "\wininit.ini")
            FileWrite(@WindowsDir & "\wininit.ini", $PENDING)
          ;I recreate the file
         EndIf
      EndIf
   EndIf
   Return 1
EndFunc  ;==>_DeleteFile

Anyone with Win9x kernel SO are willing to test this? Please?

Edited by ezzetabi

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Ok... Final (I hope).

Edit: Changed the way the Func separe Path and Filename in a more clean and efficent way.

In this version you can ask to delete a single file with _DeleteFile() as you know and also delete wildcards of Files using _DeleteFiles().

_DeleteFiles() must go with _DeleteFile(). It WONT work alone.

_DeleteFiles() can't go in subdirs... Who know, maybe in the future.

Enjoy:

Func _DeleteFiles($FILE)
  ; Description:      Use loops if needed.. What empty words... Until now! :D
  ;                   this Func must be used with _DeleteFile()
  ;                   This Func DOES NOT go in subdirs.
  ; Syntax:           _DeleteFiles( $wildcards )
  ; Parameter(s):     $wildcards - The name of the files to delete.
  ; Return Value(s):  The number of files that will be deleted boot time.
  ;                   -1 if there was a problem.
  ; Author(s):        Ezzetabi ezzetabi@katamail.com
   
   Local $PATH, $FILENAME, $C, $RETURN = 0
   If Not StringInStr($FILE, "\") Then
      $PATH = @WorkingDir & "\"
    ;and $file = $file;-D
   Else
      $c = StringInStr($file,"\",0,-1)
      $PATH = StringLeft($FILE, $C)
      $FILE = StringTrimLeft($FILE, $C)
   EndIf
  ;Now finally we have $path and $file :D
   $C = FileFindFirstFile($PATH & $FILE)
   While 1
      If $C = -1 Then ExitLoop;There are problem with the search
      $FILENAME = FileFindNextFile($C)
      If @error = 1 Then ExitLoop
      If $FILENAME <> "." And $FILENAME <> ".." Then
         If Not StringInStr(FileGetAttrib($PATH & $FILENAME), "d") Then
            $RETURN = $RETURN + _DeleteFile($PATH & $FILENAME)
         EndIf
      EndIf
   Wend
   If $C <> - 1 Then
      FileClose($C)
   Else
      Return -1
   EndIf
   Return $RETURN
EndFunc  ;==>_DeleteFiles

Func _DeleteFile($FILE);Only deletes a single file. Use loops if needed
   
  ; Description:      Try to delete a file, if it fails it deletes it reboot time.
  ; Syntax:           _DeleteFile( $filename )
  ; Parameter(s):     $filename - The name of the file to delete. No wildcards.
  ; Return Value(s):  -2 You used wildcards (* or ?)
  ;                   -1 The file does not exist
  ;                    0 The file has already been deleted
  ;                    1 The file will be deleted boot time
  ; Author(s):        Ezzetabi ezzetabi@katamail.com
   
   Local $PENDING, $C
   If StringInStr($FILE, "*") Or StringInStr($FILE, "?") Then Return -2
   If Not StringInStr($FILE, "\") Then $FILE = @WorkingDir & "\" & $FILE;Adds the full path
   
   If Not FileExists($FILE) Then Return -1
   If FileDelete($FILE) Then Return 0
   
  ;If the code arrives here it means that the file is in-use and so
  ;it will be deleted at the reboot.
   
   If @OSTYPE = "WIN32_NT" Then;The Kernel is W2000
      $PENDING = RegRead("HKLM\System\CurrentControlSet\Control\Session Manager", _
            "PendingFileRenameOperations")
      If $PENDING = "" Then
         RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager", _
               "PendingFileRenameOperations", "reg_multi_sz", _
               "\??\" & $FILE & @LF)
      Else
         RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager", _
               "PendingFileRenameOperations", "reg_multi_sz", $PENDING & @LF & @LF & "\??\" & _
               $FILE & @LF)
      EndIf
   Else;The Kernel is Win9x
      $FILE = FileGetShortName($FILE)
      $PENDING = FileRead(@WindowsDir & "\wininit.ini", FileGetSize(@WindowsDir & "\wininit.ini"))
      If @error = 1 Then  ;The file wininit.ini DOES NOT exist.
         FileWrite(@WindowsDir & "\wininit.ini", "[rename]" & @CRLF & _
               "NUL=" & $FILE)
      Else                ;The file wininit.ini EXISTS
         Local $POS, $C
         $POS = StringInStr($PENDING, "[rename]")
         If $POS = 0 Then;The [Rename] section is not there.
            FileWrite(@WindowsDir & "\wininit.ini", @CRLF & "[rename]" & @CRLF & _
                  "NUL=" & $FILE);... So it is just added at the end.
         Else;The section [rename] is there
            $POS = $POS + 9;(7 of the word [rename] and 2 of @crlf)
            $C = StringRight($PENDING, StringLen($PENDING) - $POS)
            $PENDING = StringTrimRight($PENDING, StringLen($PENDING) - $POS)
          ;$Pending contain the whole wininit.ini, I removed the right part after [rename]
          ;after coping it in $C
            $PENDING = $PENDING & "NUL=" & $FILE & @CRLF & $C
          ;I add the line for deleting. And re-add the right part removed.
            FileDelete(@WindowsDir & "\wininit.ini")
            FileWrite(@WindowsDir & "\wininit.ini", $PENDING)
          ;I recreate the file
         EndIf
      EndIf
   EndIf
   Return 1
EndFunc  ;==>_DeleteFile
Edited by ezzetabi

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Do you think that adding some code for also sending a email if some files has not been deleted after the reboot is a good idea?

Edited by ezzetabi

Share this post


Link to post
Share on other sites

Do you think that adding some code for also sending a email if some files has not been deleted after the reboot is a good idea?

<{POST_SNAPBACK}>

Yessssssssssssssssssssssssss.

An ADVOCATE for AutoIT

Share this post


Link to post
Share on other sites

Does AutoIT have a function to send email?

Ian


"Blessed be the name of the Lord" - Job 1:21Check out Search IMF

Share this post


Link to post
Share on other sites

No. But I would use Blat for this task.

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