Jump to content

Deleting ONLY the contents of a directory ...

Recommended Posts

... but not the directory itself ...

DirRemove does exactly what I need to do, except that it also deletes the parent directory, and there is no option to leave the parent.

Why is this a problem?

Take a directory that has specific permissions set on it. The contents need to be deleted, but if the parent directory is deleted, then recreated under my program's user context, then the original permissions are lost.

What are my options? To enumerate the subs of the directory, and delete them individually?

Link to post
Share on other sites

May This Help U out

$Folder=FileSelectFolder('Folder Whose Content To Be Deleted','')
While True
If @error Then ExitLoop
If @extended Then

Thumbs Up If Helped

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to post
Share on other sites
  • Moderators

Hi, SlowCoder74. This is what I use to delete all files and subfolders from a user's Temp directory. You may be able to modify it for your needs.

#include <File.au3>
#include <Array.au3>
$_dir1 = _FileListToArray(@TempDir, "*", 2)

If IsArray($_dir1) Then
    For $n = 1 To $_dir1[0]
        $sPath = @TempDir & "" & $_dir1[$n]
        If FileExists($sPath & "") Then
            DirRemove($sPath, 1)

$_dir2 = _FileListToArray(@TempDir, "*", 1)
If IsArray($_dir2) Then
    For $n = 1 To $_dir2[0]
        $sPath = "C:Temp" & $_dir2[$n]
        If FileExists($sPath) Then
Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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...