Jump to content

Delete folders found in a text file

Recommended Posts

Hi there!

I'm trying to delete old folders that are not in use anymore - the names of those folders are in a text file.

So what I want to do is have the script read the text file and compare the names in it to the actual folder names inside, and if the folder name from the list is found on the computer - delete it.

I am using filereadline to read from the text file, and _FileListToArrayRec to compare with the actual folders on the computer, but I need some help with the basic code. would appreciate the help..

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <file.au3>
#Include <Date.au3>
#include <Array.au3>

; ***** Create local logs
DirCreate (@scriptdir & "test")
$dir = (@scriptdir & "test"")
$log = FileOpen (@scriptdir & "test"\test.log",2)

; Function for reading - text file

Func Eof()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Global Const $sFilePath = @scriptdir & "\systems.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

    ; Read the contents of the file using the handle returned by FileOpen.
    Global $sFileRead = FileReadLine($hFileOpen, 1)

    ; Close the handle returned by FileOpen.

    ; Display the contents of the file.
    MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)


; put the root in a variable
$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, 2) ; _FileListToArray("path" [, "Filter" [, Flag]])
; Look at what _FileListToArray() puts into $aList

; This is a loop that runs from 1 to the number of items listed in the first element of the returned array
For $i = 1 To $aList[0]

If $sFileRead = $sRoot & "\" & $aList[$i] Then
            _filewritelog ($log, "Deleting, " & $sFileRead)

Link to post
Share on other sites

if your text file stores full path of folders (which is absolutely insane if it doesn't), then there is no need for _FileListToArrayRec().

instead, use FileExists() on every line read from the text file.

other than that, what is the difficulty you are experiencing?

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...