Jump to content

[Solved] Delete subfolders from a selected folders


Recommended Posts

Hi Experts,

I would like to ask for your help if this is possible to do in AutoIt.:>

I have these folders selected from a specified path and their folder names were saved in a "Folders.txt" file, now I want to delete their subfolders and retain only one specific subfolder which has a filename of "0000000000". I have my sample "Folders.txt" file below and the subfolder that should retain (highlighted in yellow).

Folders.txt

image.png.67f370c19c06ea124eaf2fde93eac0c9.png

 

Subfolder to retain in deletion:

image.png.ad2dd0fdbe44502429c3ef09d9c05aaa.png

 

Note: Those Folder names were taken from a specific path (see below), means not all the folders found from that path is/are included.

image.png.a0e7034033e13204e69db2ddf12d5411.png

 

Please let me know if you have any clarifications Experts, and by the way, I'm still searching for a sample code that I can start with:sweating:, kind of hard for me to compose one.

 

Thanks in advance Experts...

KS15

 

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Example:

#include <Array.au3>
#include <File.au3>

Global $bExample = True
Global $sRootDir = @ScriptDir & "\Example", $sKeepDir = "00000000000"
Global $sIniFile = @ScriptDir & "\Folders.ini"
Global $aDirList = IniReadSection($sIniFile, "Folders")
    If @error And $bExample Then
        _ExampleFolders()
    ElseIf @error Then
        Exit
    EndIf

Global $aFolderPaths = _FileListToArrayRec($sRootDir, _ArrayToString($aDirList, "*;", 1, -1, "*;", 0, 0) & "*", 2, 0, 0, 2)
If @error Then Exit
For $i = 1 To $aFolderPaths[0]
    If FileExists($aFolderPaths[$i]) Then
        $aSubFolders = _FileListToArrayRec($aFolderPaths[$i], "*|" & $sKeepDir & "*", 2, 0, 0, 0)
            If @error Then ContinueLoop
        For $j = 1 To $aSubFolders[0]
            If StringLeft($aSubFolders[$j], StringLen($sKeepDir)) = $sKeepDir Then ContinueLoop ;~ Shouldn't exist as it's already filtered out.
            SplashTextOn("Removing Subfolders", "Delete: " & $aFolderPaths[$i] & "\" & $aSubFolders[$j], 800, 45, Default, Default, Default, Default, 11)
            DirRemove($aFolderPaths[$i] & "\" & $aSubFolders[$j], 1)
        Next
    EndIf
Next

;~ ---------------  Create Demo Folders  --------------- ~;
Func _ExampleFolders()
    Local $sFolderPath
    For $i = 0 To 9
        $sSubLvl1 = _RandomFolder()
        If Random(0, 1, 1) = 1 Then
            IniWrite($sIniFile, "Folders", $sSubLvl1, "")
            $sSubLvl1 &= " " & _RandomFolder()
        EndIf
        For $j = 0 To 9
            $sSubLvl2 = FileExists($sRootDir & "\" & $sSubLvl1 & "\" & $sKeepDir & " 000000~1") ? _RandomFolder() : $sKeepDir & " 000000~1"
            $sFolderPath = $sRootDir & "\" & $sSubLvl1 & "\" & $sSubLvl2
            DirCreate($sFolderPath)
        Next
    Next
    $aDirList = IniReadSection($sIniFile, "Folders")
EndFunc

Func _RandomFolder()
    Local $sFolder = ""
    For $j = 1 To Random(5, 10, 1)
        $sFolder &= Chr(Random(65, 90, 1))
    Next
    Return $sFolder
EndFunc

 

Edited by Subz
Link to comment
Share on other sites

@Subz, thanks... you nailed it again:lol:... very nice example you have there subz everything is perfect as expected. THANK YOU SO MUCH!!!!:lmao:...

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@Subz, sorry if I need to ask this.

I tried changing the "$sRootDir" into GUICtrlRead($Input) but the GUI will close and stop executing.

From:

Global $sRootDir = @ScriptDir & "\Folder", $sKeepDir = "00000000000"

To this:

$Input = GUICtrlCreateInput("", 16, 128, 385, 20) ; input from my GUI
;
;
;
;

Global $sRootDir = GUICtrlRead($Input), $sKeepDir = "00000000000" ; wont run and gui will close automatically.

Is there any changes that should be changed to run this?:D I tried having it this way so that my FileSelectFolder() will be the path that should be taken by the program but I think there's something from your code that I couldn't see how to fix this. I already used this type of reading before and there's no issue.

In my "$Input" the path was already declared (see below).

image.png.5820e7b240b59f87a1320ba93fe1e1c5.png

 

Also, how can I change the .ini file that should not contain "=" after the folder name.

From This: "with equal sign"

image.png.210498f15479e83394543fd9a1565025.png

 

To This: "no equal sign"

image.png.df07f6532098cc70679d34e87ac057a7.png

 

Thanks!

KS15

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

You would have to show more of the script to diagnose, or add some error checking to your script to determine when the script exits.

If you don't want the ini format you would have to change IniReadSection to:

Global $aDirList
    _FileReadToArray($sIniFile, $aDirList, 0)
        If @error Then Exit
    $aDirList[0] = UBound($aDirList) - 1

You would also need to change all the variables for $aDirList[x][x] to $aDirList[x], since you've changed from a 2d array to 1d array.

Link to comment
Share on other sites

@Subz Here's what I did to your example, I just add it in a GUI.

Local $sSelection
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form", 590, 419, 268, 226)
GUISetBkColor(0xD7D7D2)
$Input = GUICtrlCreateInput("", 16, 50, 385, 20)
GUICtrlSetState($Input, $GUI_DISABLE)
$Fpath = GUICtrlCreateButton("Path Folder", 400, 48, 89, 25)
$Fdelte = GUICtrlCreateButton("Folder Delete", 498, 48, 89, 25)
$LogView = GUICtrlCreateButton("Log Viewer", 14, 368, 97, 33)
$ExitBut = GUICtrlCreateButton("Close", 497, 368, 89, 33)
$g_idEdit = GUICtrlCreateEdit("", 17, 75, 568, 290, BitOr($GUI_SS_DEFAULT_EDIT, $ES_READONLY))
_GUICtrlEdit_SetLimitText($g_idEdit, 64000)
_GUICtrlEdit_LineScroll($g_idEdit, 0, _GUICtrlEdit_GetLineCount($g_idEdit))
GUICtrlSetColor($g_idEdit, 0x5B0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $bExample = True
Global $sRootDir = @ScriptDir & "\Folder", $sKeepDir = "00000000000"
Global $sIniFile = @ScriptDir & "\Folders.ini"
Global $gLogFile = @ScriptDir & "\Deleted.ini"
Global $FileLog = FileOpen($gLogFile, 1)
Global $aDirList = IniReadSection($sIniFile, "Folders")
    If @error Then
       MsgBox(0,"Error","Error Reading Path!")
       Exit
    EndIf

Global $aFolderPaths = _FileListToArrayRec($sRootDir, _ArrayToString($aDirList, "*;", 1, -1, "*;", 0, 0) & "*", 2, 0, 0, 2)
If @error Then Exit

While 1
   Sleep(10)
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE, $ExitBut
                Exit
             Case $LogView
                  ShellExecute($gLogFile)
            Case $Fpath
               Local $sFileSelectFolder = FileSelectFolder("Select for your Folder-Source!", "::{7be9d83c-a729-4d97-b5a7-1b7313c39e0a}")
               If @error Then
                  MsgBox(64, "Warning", "No folder was selected.")
                  Back()
               Else
                  GUICtrlSetData($Input, $sFileSelectFolder)
                  GUICtrlSetState($Input, $GUI_Disable)
               EndIf
            Case $Fdelte
            For $i = 1 To $aFolderPaths[0]
               If FileExists($aFolderPaths[$i]) Then
                  $aSubFolders = _FileListToArrayRec($aFolderPaths[$i], "*|" & $sKeepDir & "*", 2, 0, 0, 0)
               If @error Then ContinueLoop
                  For $j = 1 To $aSubFolders[0]
                     If StringLeft($aSubFolders[$j], StringLen($sKeepDir)) = $sKeepDir Then ContinueLoop ;~ Shouldn't exist as it's already filtered out.
                     $_sLogData = "Delete: " & $aFolderPaths[$i] & "\" & $aSubFolders[$j]
                     _GUICtrlEdit_AppendText ($g_idEdit, $_sLogData & @CRLF)
                     DirRemove($aFolderPaths[$i] & "\" & $aSubFolders[$j], 1)
                  Next
               EndIf
            Next
            FileWrite($FileLog, "Username: " & @UserName & @CRLF & "ProcessDate: " & @MON & "/" & @MDAY & "/" & @YEAR & @CRLF & "Time: " & @HOUR & ":" & @MIN & ":" & @SEC & @CRLF & "====================" & @CRLF & GUICtrlRead($g_idEdit) &@CRLF& "===================="& @CRLF)
            MsgBox(0,"Completed","Done Processing!")
            Back()
        EndSwitch
WEnd

Func Back()
   Return
EndFunc

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

#include <Array.au3>
#include <EditConstants.au3>
#include <File.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>

Global $sFileSelectFolder, $aFolderPaths
Global $sKeepDir = "00000000000"
Global $sIniFile = @ScriptDir & "\Folders.ini"
Global $gLogFile = @ScriptDir & "\Deleted.ini"
Global $FileLog = FileOpen($gLogFile, 1)
Global $aDirList
    _FileReadToArray($sIniFile, $aDirList, 0)
        If @error Then Exit MsgBox(0,"Error","Error Reading Path!")
    $aDirList[0] = UBound($aDirList) - 1

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form", 590, 419, 268, 226)
GUISetBkColor(0xD7D7D2)
$Input = GUICtrlCreateInput("", 16, 50, 385, 20)
GUICtrlSetState($Input, $GUI_DISABLE)
$Fpath = GUICtrlCreateButton("Path Folder", 400, 48, 89, 25)
$Fdelte = GUICtrlCreateButton("Folder Delete", 498, 48, 89, 25)
$LogView = GUICtrlCreateButton("Log Viewer", 14, 368, 97, 33)
$ExitBut = GUICtrlCreateButton("Close", 497, 368, 89, 33)
$g_idEdit = GUICtrlCreateEdit("", 17, 75, 568, 290, BitOr($GUI_SS_DEFAULT_EDIT, $ES_READONLY))
_GUICtrlEdit_SetLimitText($g_idEdit, 64000)
_GUICtrlEdit_LineScroll($g_idEdit, 0, _GUICtrlEdit_GetLineCount($g_idEdit))
GUICtrlSetColor($g_idEdit, 0x5B0000)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE, $ExitBut
            Exit
        Case $LogView
            ShellExecute($gLogFile)
        Case $Fpath
            $sFileSelectFolder = FileSelectFolder("Select for your Folder-Source!", "::{7be9d83c-a729-4d97-b5a7-1b7313c39e0a}")
            If @error Then
                MsgBox(64, "Warning", "No folder was selected.")
                Back()
            Else
                GUICtrlSetData($Input, $sFileSelectFolder)
                GUICtrlSetState($Input, $GUI_Disable)
            EndIf
        Case $Fdelte
            $sInput = GUICtrlRead($Input)
            If FileExists($sInput) = 0 Then ContinueLoop MsgBox(4096, "Error", "Unable to find path: " & $sInput)
            $aFolderPaths = _FileListToArrayRec($sInput, _ArrayToString($aDirList, "*;", 1, -1, "*;", 0, 0) & "*", 2, 0, 0, 2)
                If @error Then ContinueLoop MsgBox(4096, "Error", "Error: " & _FileListToArrayError(@error))
            For $i = 1 To $aFolderPaths[0]
                If FileExists($aFolderPaths[$i]) Then
                    $aSubFolders = _FileListToArrayRec($aFolderPaths[$i], "*|" & $sKeepDir & "*", 2, 0, 0, 0)
                        If @error Then ContinueLoop
                    For $j = 1 To $aSubFolders[0]
                        If StringLeft($aSubFolders[$j], StringLen($sKeepDir)) = $sKeepDir Then ContinueLoop ;~ Shouldn't exist as it's already filtered out.
                        $_sLogData = "Delete: " & $aFolderPaths[$i] & "\" & $aSubFolders[$j]
                        _GUICtrlEdit_AppendText ($g_idEdit, $_sLogData & @CRLF)
                        DirRemove($aFolderPaths[$i] & "\" & $aSubFolders[$j], 1)
                    Next
                EndIf
            Next
            FileWrite($FileLog, "Username: " & @UserName & @CRLF & "ProcessDate: " & @MON & "/" & @MDAY & "/" & @YEAR & @CRLF & "Time: " & @HOUR & ":" & @MIN & ":" & @SEC & @CRLF & "====================" & @CRLF & GUICtrlRead($g_idEdit) &@CRLF& "===================="& @CRLF)
            MsgBox(0,"Completed","Done Processing!")
            Back()
    EndSwitch
WEnd

Func Back()
   Return
EndFunc

Func _FileListToArrayError($iError)
    Local $sError
    Switch $iError
        Case 1
            $sError = "Path not found or invalid"
        Case 2
            $sError = "Invalid Include parameter"
        Case 3
            $sError = "Invalid Exclude parameter"
        Case 4
            $sError = "Invalid Exclude_Folders parameter"
        Case 5
            $sError = "Invalid $iReturn parameter"
        Case 6
            $sError = "Invalid $iRecur parameter"
        Case 7
            $sError = "Invalid $iSort parameter"
        Case 8
            $sError = "Invalid $iReturnPath parameter"
        Case 9
            $sError = "No files/folders found"
        Case Else
            $sError = "Unknown"
    EndSwitch
    Return $sError
EndFunc

 

Edited by Subz
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...