Sign in to follow this  
Followers 0
lrstndm

Delete ONLY subfolder and files

3 posts in this topic

Hi,

I have an autoit script which have to delete all subfolders and all the files in the given directory.

but the selected/main folder needs to exists after deleting the subfolders and files.

for example:

i have the directory C:\test selected

the folder test needs to exists after deleting all the files and folders in the directory

is it possible to do this or is there an CMD command to do this?

Thanks in advance

Greetz,

lrstndm

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi, Irstndm. I have a script which performs system maintenance, including deleting C:Temp of all files and folders. Below is a snippet from that script; it should work for what you're trying to do.

$_dir1 = _FileListToArray(@TempDir, "*", 2)
$_dir2 = _FileListToArray(@TempDir, "*", 1)
 
If IsArray($_dir1) Then
  For $n = 1 To $_dir1[0]
   $sPath = @TempDir & "" & $_dir1[$n]
   If FileExists($sPath & "") Then
    DirRemove($sPath, 1)
   EndIf
  Next
EndIf

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 person likes this

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

Share this post


Link to post
Share on other sites

Thanks that is what i wanted but i changed it a little bit some errors found, but it helped me a lot.

#include <File.au3>
$_dir1 = _FileListToArray(@TempDir, "*", 2)
$_dir2 = _FileListToArray(@TempDir, "*", 1)
If IsArray($_dir1) Then
  For $n = 1 To $_dir1[0]
   $sPath = @TempDir & "" & $_dir1[$n]
   If FileExists($sPath & "") Then
    DirRemove($sPath, 1)
   EndIf
  Next
EndIf
If IsArray($_dir2) Then
  For $n = 1 To $_dir2[0]
   $sPath = @TempDir & '' & $_dir2[$n]
   If FileExists($sPath) Then
    FileDelete($sPath)
   EndIf
  Next
EndIf

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