penn1 Posted October 29, 2009 Share Posted October 29, 2009 I'm not a programmer. I use AutoIT to automate tasks using HotKeySet mainly. Occassionally, I'll have a while or for loop. We have an application that occassionally needs some cache folders deleted. I'm trying to create a little script that will: Display the folder contents, User clicks OK Remove the directories, Re-display the folder contents so the user sees that they have been removed, User clicks something to exit the script. I'm OK on everything but the display of the directory items [/u(In each directory, the contents will be a max of three folders, not a long list of files). I'm searching the examples and help files and this forum, but haven't stumbled on the correct thing yet. I did find a script that will show each file individually, but it's not exactly what I'm looking for. Thanks. Link to comment Share on other sites More sharing options...
cherdeg Posted October 29, 2009 Share Posted October 29, 2009 expandcollapse popupGlobal $s_CRLF = @CRLF Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling. $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler _DeleteFiles("C:\Documents and Settings\Administrator\Local Settings\Temp") ; The function _DeleteFiles() deletes files from given directory ; ================================================================================================== Func _DeleteFiles($s_Directory) ; Create a FileSystemObject Local $o_FileSystem = ObjCreate("Scripting.FileSystemObject") Local $o_Folder = $o_FileSystem.GetFolder($s_Directory) Local $o_Files = $o_Folder.Files Local $s_FileNames ; Put all file names into a string variable to display For $o_Item In $o_Files $s_FileNames &= $o_Item.name & $s_CRLF Next ; Display the msgbox $i_ReturnValue = MsgBox(36, "Delete Files?", "You entered the directory:" & $s_CRLF & $s_CRLF & $s_Directory & $s_CRLF & $s_CRLF & "It contains the followig files: " & $s_CRLF & $s_FileNames & $s_CRLF & "Do you want to delete these files?") If $i_ReturnValue = 6 Then For $o_Item In $o_Files ; delete the files $o_FileSystem.deletefile($o_Item) If $g_eventerror Then MsgBox(48, "Error.", $o_Item.name & $s_CRLF & $s_CRLF & "...could not be deleted." & $s_CRLF & $s_CRLF & "Maybe it's in use by some application?") Next Else Exit EndIf EndFunc ;==>_DeleteFiles ; A custom error handler ; ================================================================================================== Func MyErrFunc() $h_HexNumber = Hex($oMyError.number, 8) $g_eventerror = 1 ; something to check for when this function returns EndFunc ;==>MyErrFunc Link to comment Share on other sites More sharing options...
penn1 Posted October 29, 2009 Author Share Posted October 29, 2009 Thanks so much. This gets me much further down the road. I do have to ask if there is a way to adjust it though. To delete the cache, I have to remove folders, not files. I have been able to successfully use the DirRemove() command for the folder removal, but that doesn't let me show the user which folders are about to be deleted. Is there an o_Folders.Folders type command I could sub in place of the o_Folders.Files line? Is that an easy adjustment? As I said earlier, I'm most appreciative of the help, and I am researching solutions too. Thanks Link to comment Share on other sites More sharing options...
cherdeg Posted October 29, 2009 Share Posted October 29, 2009 (edited) I have to remove folders, not files. [...] Is there an o_Folders.Folders type command I could sub in place of the o_Folders.Files line? Yes, there is: http://msdn.microsoft.com/en-us/library/9kcx47hd%28VS.85%29.aspx So you could create a function that similarly checks for folders and returns a folder objects collection (currently we return a file objects collection). You could then cycle that collection and feed the _DeleteFiles() function with the found subfolder names. Last you need to delete the folders, which you could do either using AutoIt's function DirRemove() or call the folder object's .Delete method, see: http://msdn.microsoft.com/en-us/library/0k4wket3%28VS.85%29.aspx. Another way would be to just get folder and file object collections and do all the rest using AutoIt's functions DirRemove() and FileDelete(). The changes are quite easy (in my opinion). Regards, Chris Edited October 29, 2009 by cherdeg Link to comment Share on other sites More sharing options...
99ojo Posted October 29, 2009 Share Posted October 29, 2009 (edited) Hi, this is the modified code from cherdeg (thanks for the starting point) for deletion of all Subfolders beneath a given path: expandcollapse popupGlobal $s_CRLF = @CRLF Global $g_eventerror = 0 ; to be checked to know if com error occurs. Must be reset after handling. $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") ; Install a custom error handler _DeleteFolders("C:\Test") ; The function _DeleteFolders() deletes subfolders from given directory ; ================================================================================================== Func _DeleteFolders($s_Directory) ; Create a FileSystemObject Local $o_FileSystem = ObjCreate("Scripting.FileSystemObject") Local $o_Folder = $o_FileSystem.GetFolder($s_Directory) Local $o_SubFolders = $o_Folder.SubFolders Local $s_FolderNames ; Put all file names into a string variable to display For $o_Item In $o_SubFolders $s_FolderNames &= $o_Item.name & $s_CRLF Next ; Display the msgbox $i_ReturnValue = MsgBox(36, "Delete Folders?", "You entered the directory:" & $s_CRLF & $s_CRLF & $s_Directory & $s_CRLF & $s_CRLF & "It contains the followig folders: " & $s_CRLF & $s_FolderNames & $s_CRLF & "Do you want to delete these folders?") If $i_ReturnValue = 6 Then For $o_Item In $o_SubFolders ; delete the folders $o_FileSystem.deletefolder ($o_Item) If $g_eventerror Then MsgBox(48, "Error.", $o_Item.name & $s_CRLF & $s_CRLF & "...could not be deleted." & $s_CRLF & $s_CRLF & "Maybe it's in use by some application?") Next Else Exit EndIf EndFunc ;==>_DeleteFolders ; A custom error handler ; ================================================================================================== Func MyErrFunc() $h_HexNumber = Hex($oMyError.number, 8) $g_eventerror = 1 ; something to check for when this function returns EndFunc ;==>MyErrFunc Edited October 29, 2009 by 99ojo 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