Jump to content
Sign in to follow this  
SlowCoder74

Deleting ONLY the contents of a directory ...

Recommended Posts

SlowCoder74

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

Share this post


Link to post
Share on other sites
Danyfirex

use FileDelete

filedelete("C:UsersusuarioDesktopfolder")

Share this post


Link to post
Share on other sites
PhoenixXL

May This Help U out

$Folder=FileSelectFolder('Folder Whose Content To Be Deleted','')
ConsoleWrite($Folder&@CRLF)
$Search=FileFindFirstFile($Folder&'*.*')
While True
$Found=FileFindNextFile($Search)
If @error Then ExitLoop
If @extended Then
  ConsoleWrite('DirRemove:'&DirRemove($Folder&''&$Found,1)&@CRLF)
  ContinueLoop
EndIf
ConsoleWrite('FileRemove:'&FileDelete($Folder&''&$Found)&@CRLF)
ConsoleWrite($Folder&''&$Found&@CRLF)
WEnd

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.

Share this post


Link to post
Share on other sites
JLogan3o13

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)
        EndIf
    Next
EndIf

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

√-1 2^3 ∑ π, and it was delicious!

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.