Jump to content

Removing Directories


Recommended Posts

A question I had that was buried in a developers thread about "Filecopy, Filemove, Filedelete & Shfileoperation" and therefore kept unanswerd :whistle: :

DirRemove("c:\temp", 1) deletes all directories in C:\temp but it also deletes the TEMP directory.

How can I remove directories I don't know the names of in advance without killing the container itself?

Possible?

Link to comment
Share on other sites

I thought of that but I'm uncomfortable with deleting the Windows-TEMP directory. Then it would be temporarily unreachable even if only for a second.

Because in that special second... :whistle:

Is such a function really an exceptional desire? If not maybe something for the idea lab section?

Link to comment
Share on other sites

  • Administrators

Remove the MsgBox and uncomment the DirRemove and FileDelete when you are SURE you know how it works :whistle:

; Delete everything under temp, but not temp 
TempClear("z:\code\cvsroot\test\temp\")



Func TempClear($dir)

; Make sure the directory has a trailing \
  If StringRight($dir, 1) <> "\" Then $dir = $dir & "\"

  Local $pattern = $dir & "*.*"
  Local $file

; First delete the files in this directory
 ;FileDelete($pattern)
  MsgBox(4096, "Deleting files", $pattern)

; Now delete all subdirectories

; Get first file
  $file = FileFindFirstFile($pattern)
  If @error Then
    MsgBox(4096, "Error", "Path not found")
  EndIf
 
  While 1 
    If $file <> "." And $file <> ".." Then
      If StringInStr(FileGetAttrib($dir & $file), "D") Then
        MsgBox(0, "Directory to delete:", $dir & $file)
      ;DirRemove($dir & $file, 1)
      EndIf   

    EndIf

    $file = FileFindNextFile()
    If @error Then ExitLoop
  WEnd

EndFunc
Edited by Jon
Link to comment
Share on other sites

  • Administrators

Here is the same script modified to work with the new v3.0.92 unstable.

; Delete everything under temp, but not temp 
TempClear("z:\code\cvsroot\test\temp\")



Func TempClear($dir)

 ; Make sure the directory has a trailing \
  If StringRight($dir, 1) <> "\" Then $dir = $dir & "\"

  Local $pattern = $dir & "*.*"
  Local $file
  Local $handle

 ; First delete the files in this directory
  MsgBox(0, "Deleting files", $pattern)
 ;FileDelete($pattern)

 ; Now delete all subdirectories

 ; Initialize the search
  $handle = FileFindFirstFile($pattern)
  If @error Then
    MsgBox(4096, "Error", "Path not found")
  EndIf

  While 1
    $file = FileFindNextFile($handle)
    If @error Then ExitLoop

    If $file <> "." And $file <> ".." Then
      If StringInStr(FileGetAttrib($dir & $file), "D") Then
        MsgBox(0, "Directory to delete:", $dir & $file)
       ;DirRemove($dir & $file, 1)
      EndIf   

    EndIf

  WEnd

 ; Close our search handle
  FileClose($handle)

EndFunc

Link to comment
Share on other sites

I encountered that the FileDelete-function used in this script doesn't work as expected:

FileDelete("d:\temp\test\*.*")

Deletes all files in the test-directory as desired.

FileDelete("d:\temp\*.*")

Should work like the above. But since there are some locked files in this directory it should only delete the unlocked files. But it deletes nothing, not a single file.

del d:\temp\*.*

The old DOS-command does what I expected from FileDelete("d:\temp\*.*"): it deletes just the accessible files and leaves the other files untouched.

A bug in FileDelete? :whistle:

Link to comment
Share on other sites

del d:\temp\*.*

The old DOS-command does what I expected from FileDelete("d:\temp\*.*"): it deletes just the accessible files and leaves the other files untouched.

.. in which case, seems like this might be your workaround? ..

RunWait(@COMSPEC & " /c del d:\temp\*.*")
Link to comment
Share on other sites

  • 6 months later...

Sorry to bring this topic up again but I just switched to the new v3.0.102 and found that this script to to delete all files in a directory but not the directoy itself doesn't work anymore. :ph34r:

Have there been any changes to the File functions?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...