Sign in to follow this  
Followers 0
mist3r

Delete all but a couple of files?

6 posts in this topic

Hey,

I've been reading the help file looking at the file commands and trying to peice together how i'd use them to basically delete everything in a folder except a couple of folders.

Its basically going to remove all user profiles except "administrator, all users, default user"

I take it loop and look for matches.

IF $filename doesnt equal "administrator"

FileDelete ( "$filename )

Im just struggling mainly on how to actually start the loop and get the filenames.

Please help, any advice is certainitly appreciated.

Thanks! :P

Share this post


Link to post
Share on other sites



A small change to the helpfile example of FileFindFirstFile() may help.

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    ; Restart the loop if select folders not found
    If  $file <> "Administrator" And _
        $file <> "All Users" And _
        $file <> "Default User" Then
        ContinueLoop
    EndIf
    ; Check if is directory and remove if it is
    If StringInStr(FileGetAttrib($file), "D") Then
        DirRemove($file, 1)
    EndIf
;~     MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

You would stil need to FileChangeDir() or use fullpaths to the Homepath as the example is relative only.

Share this post


Link to post
Share on other sites

Aaah excellent! :P I love it. Im still working on it as it's actually deleting the administrator folders as well. I think i'll figure it out.

Oh 1 last thing. the "AND _" i take it that means.. AND IF $file.."

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The underscore ( _ ) is just a way of splitting one line of code into multiple lines, making it more readable. These three lines:

If  $file <> "Administrator" And _
    $file <> "All Users" And _
    $file <> "Default User" Then
Edited by Sokko

Share this post


Link to post
Share on other sites

Aah! Thanks ever so much u 2. I really really appreciate your help. Ive finally done it and its just a life saver tbh.

The Microsoft DelProf doesnt do a very good job of removing half the stuff so this should work :P Thanks again

Share this post


Link to post
Share on other sites

Beware of deleting some of the system (background) user (LocalService, NetworkService, etc) profiles.

Note that you may not have access to all the folders (by design). Use the CACLS utility wisely. :P

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