Jump to content

Recommended Posts

Posted

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

  • Moderators
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

"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!

Posted

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

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