Jump to content

Recommended Posts

Posted

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?

Posted

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

Posted

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?

  • Administrators
Posted

I'd be _very_ suprised if you manage to delete all the temp folder, there's nearly always something in use in there that aborts the operation.


 

  • Administrators
Posted

I'll post a user function to do a safe temp directory clean in a mo - just writing it.


 

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


 

  • Administrators
Posted

Actually this shows up that you can only have one FileFind active at a time, I may change it to return a handle like the FileOpen function.


 

  • Administrators
Posted

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


 

Posted

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:

Posted

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

Posted

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\*.*")
Posted

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
Posted

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()

  • 6 months later...
Posted

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?

Posted

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.

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