Jump to content

YAODoOUF


ezzetabi
 Share

Recommended Posts

Comment uncomment, _DeleteFolder or _Deletefiles for testing.

This code is integrated with Run!, why not use it?

_DeleteFolder()
;_DeleteFiles()

Func _DeleteFiles()
   Local $sSelection, $aSelection, $c, $cDel = 0
   $sSelection = FileOpenDialog('What file(s) do you want to delete?', @HomeDrive, 'All (*)', 5)
   Select
      Case $sSelection = 1
         Return 0
      Case StringInStr($sSelection, '|')
         $aSelection = StringSplit($sSelection, '|')
         If StringRight($aSelection[1], 1) <> '\' Then $aSelection[1] = $aSelection[1] & '\'
         For $c = 2 To $aSelection[0]
            $cDel = $cDel + _DeleteFile($aSelection[1] & $aSelection[$c])
         Next
      Case Else;The user selected only a file.
         $cDel = _DeleteFile($sSelection)
   EndSelect
   Return $cDel
EndFunc  ;==>_DeleteFiles

Func _DeleteFolder()
   Local $sSelection, $iDelDir, $aFiles, $c, $cDel = 0, $sT, $iDist
   Local $msg
   $iDelDir = MsgBox(32 + 3, 'Question.', 'Do you want to delete also the folder itself?' & @LF & @LF & 'No, please just delete the files/folders inside it.' & @LF & 'Yes, erase also the folder after emptying it.')
   If $iDelDir = 2 Then Return 0
  ;6 = Yes, 7 = No
   $sSelection = FileSelectFolder('What folder do you want to delete?', @HomeDrive, 4)
   If @error Then Return 0
   If StringRight($sSelection, 1) <> '\' Then $sSelection = $sSelection & '\'
   $aFiles = _FileSearch($sSelection, 1)
   
   If $iDelDir = 6 Then;The folder itself is added if needed.
      ReDim $aFiles[UBound($aFiles)+1]
      $aFiles[0] = $aFiles[0] + 1
      $aFiles[$aFiles[0]] = $sSelection
   EndIf
   
  ; Sorting the array. Jon's way + Jbed idea.
   $c = 1
   $bSorted = 0
   $iDist = Round($aFiles[0] / 2)
   If (@OSTYPE = "WIN32_NT" And @OSVersion <> 'WIN_NT4') Then;For OS difference in W2k kernel we have to sort from longest to shortest...
      While 1
         $bSorted = 1
         For $c = 1 To $aFiles[0] - $iDist
            If StringLen($aFiles[$c]) < StringLen($aFiles[$c + $iDist]) Then
               $sT = $aFiles[$c + $iDist]
               $aFiles[$c + $iDist] = $aFiles[$c]
               $aFiles[$c] = $sT
               $bSorted = 0
            EndIf
         Next
         If $bSorted = 1 And $iDist = 1 Then ExitLoop;EXITLOOP is HERE
         
         If $bSorted = 1 Then $iDist = Round($iDist / 2)
         If $iDist < 1 Then $iDist = 1
      Wend
   Else ;...while in W9x/Nt by shortest to longest.
      While 1
         $bSorted = 1
         For $c = 1 To $aFiles[0] - $iDist
            If StringLen($aFiles[$c]) > StringLen($aFiles[$c + $iDist]) Then
               $sT = $aFiles[$c + $iDist]
               $aFiles[$c + $iDist] = $aFiles[$c]
               $aFiles[$c] = $sT
               $bSorted = 0
            EndIf
         Next
         If $bSorted = 1 And $iDist = 1 Then ExitLoop;EXITLOOP is HERE
         
         If $bSorted = 1 Then $iDist = Round($iDist / 2)
         If $iDist < 1 Then $iDist = 1
      Wend
   EndIf
  ; End sorting part
   
   For $c = 1 To $aFiles[0]
      $cDel = $cDel + _DeleteFile($aFiles[$c])
     ;If $cDel is still 0 then all files has been deleted already,
   Next
   
   Return $cDel
EndFunc  ;==>_DeleteFolder

Func _DeleteFile($sFile)
   
  ; Description:      Try to delete a file, if it fails it deletes it reboot time.
  ; Syntax:           _DeleteFile( $sFilename )
  ; Parameter(s):     $sFilename - The name of the file to delete. No wildcards.
  ; Return Value(s):  -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 $sPending, $sFile, $iLFpos
   If StringInStr($sFile, "*") Or StringInStr($sFile, "?") Then Return -2
   If Not StringInStr($sFile, "\") Then $sFile = @WorkingDir & "\" & $sFile;Adds the full path
   
   If Not FileExists($sFile) Then Return -1
   If StringInStr(FileGetAttrib($sFile), 'd') Then
      If DirRemove($sFile, 1) Then Return 0
   Else
      If FileDelete($sFile) Then Return 0
   EndIf
   
  ;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" And @OSVersion <> 'WIN_NT4' Then;The Kernel is W2000
      $sPending = RegRead("HKLM\System\CurrentControlSet\Control\Session Manager", _
            "PendingFileRenameOperations")
      If $sPending = "" Then
         RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager", _
               "PendingFileRenameOperations", "reg_multi_sz", _
               "\??\" & $sFile & @LF)
      Else
         $iLFpos = StringInStr($sPending, @LF, 0, -1)
         Select
            Case $iLFpos = 0 Or StringMid($sPending, $iLFpos - 1, 1) = @LF;Existing operation is a deletion
               RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager", _
                     "PendingFileRenameOperations", "reg_multi_sz", $sPending & @LF & @LF & "\??\" & _
                     $sFile & @LF)
            Case StringMid($sPending, $iLFpos - 1, 1) <> @LF;Existing operation is a rename
               RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager", _
                     "PendingFileRenameOperations", "reg_multi_sz", $sPending & @LF & "\??\" & _
                     $sFile & @LF)
         EndSelect
      EndIf
      
   Else;The Kernel is Win9x/Nt
      $sFile = FileGetShortName($sFile)
      $sPending = FileRead(@WindowsDir & "\wininit.ini", FileGetSize(@WindowsDir & "\wininit.ini"))
      If @error Then ;The file wininit.ini DOES NOT exist.
         FileWrite(@WindowsDir & "\wininit.ini", "[rename]" & @CRLF & _
               "NUL=" & $sFile)
      Else               ;The file wininit.ini EXISTS
         Local $iPos, $sOldFile
         $iPos = StringInStr($sPending, "[rename]")
         If $iPos = 0 Then;The [Rename] section is not there.
            FileWrite(@WindowsDir & "\wininit.ini", @CRLF & "[rename]" & @CRLF & _
                  "NUL=" & $sFile);... So it is just added at the end.
         Else;The section [rename] is there
            $iPos = $iPos + 9;(7 of the word [rename] and 2 of @crlf)
            $sOldFile = StringRight($sPending, StringLen($sPending) - $iPos)
            $sPending = StringTrimRight($sPending, StringLen($sPending) - $iPos)
           ;$sPending contain the whole wininit.ini, I removed the right part after [rename]
           ;after coping it in $sOldFile
            $sPending = $sPending & "NUL=" & $sFile & @CRLF & $sOldFile
           ;I add the line for deleting. And re-add the right part removed.
            FileDelete(@WindowsDir & "\wininit.ini")
            FileWrite(@WindowsDir & "\wininit.ini", $sPending)
           ;I recreate the file
         EndIf
      EndIf
   EndIf
   Return 1
EndFunc  ;==>_DeleteFile

Func _FileSearch($sIstr, $bSF)
  ; $bSF = 1 means looking in subfolders
  ; $sSF = 0 means looking only in the current folder.
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sIstr, $bSF, $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'
   
  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   While $sCS <> - 1
      $sCF = FileFindNextFile($sCS)
      If @error Then
         FileClose($sCS)
         ExitLoop
      EndIf
      If $sCF = '.' Or $sCF = '..' Then ContinueLoop
      $sOutPut = $sOutPut & $sCP & $sCF & @LF
   Wend
   
  ;And after, if needed, in the rest of the folders.
   If $bSF = 1 Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         While $iH <> - 1
            $sCF = FileFindNextFile($iH)
            If @error Then
               FileClose($iH)
               ExitLoop
            EndIf
            If $sCF = '.' Or $sCF = '..' Then ContinueLoop
            If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
               $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
               $sFP = $sCP & $sCF & '\';                            for future searches
               $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
               While $iH2 <> - 1
                  $sCF2 = FileFindNextFile($iH2)
                  If @error Then
                     FileClose($iH2)
                     ExitLoop
                  EndIf
                  If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                  $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
               Wend
            EndIf
         Wend
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf
   
   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch
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...