Sign in to follow this  
Followers 0
trunc.ate

Removing Directories

19 posts in this topic

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?

Share this post


Link to post
Share on other sites



If you want some fun, you could write a recursive procedure :whistle:

A quicker (but less interesting) lateral solution might be to remember the name of the folder, kill it, then remake it .. ?

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Isn't the temp directory a shell folder? If so, aren't they automatically recreated when they are needed, anyway?

Share this post


Link to post
Share on other sites

You mean...relying on Microsoft? :whistle:

I'll give it a try

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Works perfectly, thanks!

Share this post


Link to post
Share on other sites

I may change it to return a handle like the FileOpen function.

Now THAT's a cool idea!!!! :whistle:

Share this post


Link to post
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:

Share this post


Link to post
Share on other sites

Not even Explorer will delete all non-locked files when you try to do a multiple delete, it's probably an API thing and not a bug...

Share this post


Link to post
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\*.*")

Share this post


Link to post
Share on other sites

Almost. This will do it:

RunWait(@ComSpec & " /c " & 'del /Q d:\temp\*.*', "", @SW_HIDE)

/Q parameter is needed to bypass the user prompting when using wildcards

Share this post


Link to post
Share on other sites

There we go then :whistle: .. I think CyberSulg is looking for this kind of added value for his custom helpfile project. Might be a good Remark under the help for FileDelete()

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Never mind, I found that some of the files were write protected while others were locked. As for the write protected ones I guess this can be handled by FileSetAttrib.

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