Sign in to follow this  
Followers 0
Rav1

Removing Specific Folders

3 posts in this topic

#1 ·  Posted (edited)

Hey gang - I'm very noobish with Autoit and I need some help, please.

I trying to write a script that will delete all the folders in c:users that have folder names that contain numbers.  So user foldes with numbers (IE c:usersSamGue123) will be deleted but users folders without numbers (ie c:usersAdministrator) will be spared.  This script is what I have so far but it's not working as expected....help.

#include <File.au3>
#include <Array.au3>
#include <RecFileListToArray.au3> ; External UDF
$sString = '123456789'
$FileList = _RecFileListToArray("C:\test", "*", 2, 1, 0, 2, "", "")
   If @error = 1 Then
    MsgBox(0, "", "No FilesFolders Found.")
    Exit
 EndIf
_ArrayDisplay($FileList, "Folders", Default, 8)
Do

    $fLoop = True
    For $i = 1 To $FileList[0]
         If (StringInStr($FileList[$i],stringmid($sString,$i,1)))Then
         If DirRemove($FileList[$i],1) Then
                ConsoleWrite("Dir removed: " & $FileList[$i] & @CRLF)
            Else
                ConsoleWrite("Could not remove dir: " & $FileList[$i] & @CRLF)
                $fLoop = False
            EndIf
        Else
            EndIf
    Next

Until $fLoop = False
MsgBox(0, "Results", "Done")
Edited by Rav1

Share this post


Link to post
Share on other sites



you are using $i to index the folders list, and also to index the character in the numeric string. you need two indices in nested loops for this task.

also, for better readability, i'd put the checking for digits into a separate function or a RegExp.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#include <File.au3>
#include <Array.au3>

;~ build test data
DirCreate("C:\test\MyUser12345")
DirCreate("C:\test\MyUser67890")
DirCreate("C:\test\MyUserABC")
DirCreate("C:\test\MyUserXYZ")

;~ working code
$aFiles = _FileListToArray("C:\test","*",0,1)
If @error Then Exit MsgBox(0, "", "No FilesFolders Found.")
_ArrayDisplay($aFiles, "Folders", Default, 8)
For $i = 1 To $aFiles[0]
    StringSplit($aFiles[$i], "0123456789")
    If @error Then ContinueLoop
    ConsoleWrite("Dir " & $aFiles[$i] & (DirRemove($aFiles[$i], 1) ? "" : "NOT") & " removed. " & @CRLF)
Next
MsgBox(0, "Results", "Done")

;~ remove testdata
DirRemove("C:\test", 1)

Edited by Exit

My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

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