Sign in to follow this  
Followers 0
Tsiyoshi

Delete specific folder found in a text file

6 posts in this topic

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.

#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

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by ViciousXUSMC

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0