Tsiyoshi Posted March 24, 2015 Share Posted March 24, 2015 Hello, I am trying to build a script that reads a text file with multiple lines (every line has a folder name that needs to be deleted). Then I want to look for the folder name in the folder list (using _FileListToArrayRec) and if the folder name is found - delete it. This is the code I have so far, but it fails on line 43 - "Array variable has incorrect number of subscripts or subscript dimension range exceeded" would appreciate some help. expandcollapse popup#include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <file.au3> #Include <Date.au3> #include <Array.au3> ; ***** Create local logs DirCreate (@WindowsDir & "\Log\") $dir = (@WindowsDir & "\Log\") $log = FileOpen (@WindowsDir & "\Log\log.log",2+256) Eof() Func Eof() ; Create a constant variable in Local scope of the filepath that will be read/written to. Global Const $sFilePath = @scriptdir & "\folders.txt" ; Open the file for reading and store the handle to a variable. Global $hFileOpen = FileOpen($sFilePath, $FO_READ) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. Global $aArray = FileReadToArray ($hFileOpen) For $i = 0 To UBound($aArray) - 1 ; Loop through the array. MsgBox($MB_SYSTEMMODAL, "", $aArray[$i]) Next $sRoot = (@ScriptDir) ; Retrieve a list of all the folders in $sRoot, and store them as an array in $aList Global $aList = _FileListToArrayRec ($sRoot, "*", 2, -2) ; _FileListToArray("path" [, "Filter" [, Flag]]) ; Look at what _FileListToArray() puts into $aList _ArrayDisplay($aList) For $i = 1 To $aList[0] If FileExists($sRoot & "\" & $aList[$i] & "\" & $aArray[$i]) Then _filewritelog ($log, "Deleting, " & $aArray[$i]) Next FileClose($hFileOpen) EndFunc Link to comment Share on other sites More sharing options...
jguinch Posted March 24, 2015 Share Posted March 24, 2015 Maybe something like this could me easier : Func Eof() Local Const $sFilePath = @scriptdir & "\folders.txt" Local Const $sRoot = @ScriptDir Local $aFolders = FileReadToArray($sFilePath) If @error Then Return SetError(1, 0, 0) For $i = 0 To UBound($aFolders) - 1 If FileExists($sRoot & "\" & $aFolders[$i]) Then _filewritelog ($log, "Deleting, " & $aFolders[$i]) Next EndFunc Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Solution mikell Posted March 24, 2015 Solution Share Posted March 24, 2015 As the 2 arrays have not the same number of elements you need to use 2 For loops Not tested but it should work : For $i = 1 To $aList[0] For $j = 0 To UBound($aArray) - 1 If FileExists($sRoot & "\" & $aList[$i] & "\" & $aArray[$j]) Then _filewritelog ($log, "Deleting, " & $aArray[$j]) Next Next Link to comment Share on other sites More sharing options...
orbs Posted March 24, 2015 Share Posted March 24, 2015 why is there a need to check for existence of the to-be-deleted folder? just FileReadToArray() , then loop the array and DirRemove() each item. if the folder doesn't exist, no harm done. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted March 24, 2015 Share Posted March 24, 2015 (edited) If I am not intruding, most of this is just a bit above my level but I was looking to soon take a script I use that I clean up files off of multiple users desktops to use a file list. My script looks like this. #RequireAdmin $DFile = InputBox("Black Magic Automation", "Enter File Name & Extension to delete from All Desktops", "HealthEMS Mobile.lnk") If @Error Then Exit If $DFile = "" Then $DFile = "youdidnotloseitall.txt" $folder = StringLeft(@UserProfileDir,StringInStr(@UserProfileDir,"\",0,-1)) $search = FileFindFirstFile($folder&"*") While 1 $profile = FileFindNextFile($search) If @error Then ExitLoop If FileExists($folder&$profile&"\desktop\" & $DFile) Then FileDelete($folder&$profile&"\desktop\" & $DFile) EndIf WEnd FileClose($search) MsgBox(0, "Black Magic Automation", "Cleanup Completed!") I wonder how hard it would be to take some of what is in this thread, and create a list of files/folders to delete from all users. BTW as I was building this script, atfirst it was just a hardcoded file, when I added the inputbox to make it more versitile as I was testing my exe to make sure it compiled properly, I did not want to actually delete anything just see if the inputbox would come up. I hit cancel and sent a blank string to the script and *POOF* my entire desktop dissapeared permanently deleted Lesson learned lol. Good thing it didnt take out my folders and that I do a backup every so often. Edit Pulled it off I am sure this is sloppy but its working. #RequireAdmin #Include <File.au3> #Include <Array.au3> $aFile = FileOpen(@ScriptDir & "\filelist.txt") $aFLIST = FileReadToArray($aFile) ;Debug See File List ;_ArrayDisplay($aFLIST) FileClose($aFile) For $i = 0 To UBound($aFLIST) -1 ;Debug See Current File Name ;MsgBox(0, "", $aFLIST[$i]) $Dfile = $aFLIST[$i] If $DFile = "" Then $DFile = "youdidnotloseitall.txt" $folder = StringLeft(@UserProfileDir,StringInStr(@UserProfileDir,"\",0,-1)) $search = FileFindFirstFile($folder&"*") While 1 $profile = FileFindNextFile($search) If @error Then ExitLoop If FileExists($folder&$profile&"\desktop\" & $DFile) Then FileDelete($folder&$profile&"\desktop\" & $DFile) EndIf WEnd FileClose($search) Next MsgBox(0, "Black Magic Automation", "Cleanup Completed!") I had some issues with _FileReadTo Array I guess I was not doing something right as it kept telling me I was using a varible without declaring it first, but I thought the whole point was reading the file to the array is declaring it. Lucky FileReadToArray worked for me easier. Edited March 24, 2015 by ViciousXUSMC Link to comment Share on other sites More sharing options...
Tsiyoshi Posted March 25, 2015 Author Share Posted March 25, 2015 Thank you jguinch, mikell, orbs. Appreciate your input. Like mikell said - the arrays didn't have the same elements, It was easiest to make that change and now it works fine. Thank you again. 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