KickStarter15 Posted January 25, 2019 Share Posted January 25, 2019 (edited) 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 Subfolder to retain in deletion: Note: Those Folder names were taken from a specific path (see below), means not all the folders found from that path is/are included. 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, kind of hard for me to compose one. Thanks in advance Experts... KS15 Edited January 28, 2019 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 More sharing options...
Subz Posted January 25, 2019 Share Posted January 25, 2019 (edited) Example: expandcollapse popup#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 January 25, 2019 by Subz KickStarter15 1 Link to comment Share on other sites More sharing options...
KickStarter15 Posted January 26, 2019 Author Share Posted January 26, 2019 @Subz, thanks... you nailed it again... very nice example you have there subz everything is perfect as expected. THANK YOU SO MUCH!!!!... 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 More sharing options...
KickStarter15 Posted January 26, 2019 Author Share Posted January 26, 2019 (edited) @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? 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). Also, how can I change the .ini file that should not contain "=" after the folder name. From This: "with equal sign" To This: "no equal sign" Thanks! KS15 Edited January 26, 2019 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 More sharing options...
Subz Posted January 26, 2019 Share Posted January 26, 2019 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 More sharing options...
KickStarter15 Posted January 26, 2019 Author Share Posted January 26, 2019 @Subz Here's what I did to your example, I just add it in a GUI. expandcollapse popupLocal $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 More sharing options...
Subz Posted January 26, 2019 Share Posted January 26, 2019 (edited) expandcollapse popup#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 January 26, 2019 by Subz Fr33b0w and KickStarter15 2 Link to comment Share on other sites More sharing options...
KickStarter15 Posted January 26, 2019 Author Share Posted January 26, 2019 That's it @Subz, thank you so much... didn't think of doing it like this and was never put in my mind like this. Cheers! tnx again. 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 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