trunc.ate 0 Posted February 4, 2004 A question I had that was buried in a developers thread about "Filecopy, Filemove, Filedelete & Shfileoperation" and therefore kept unanswerd : 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
trids 2 Posted February 4, 2004 If you want some fun, you could write a recursive procedure 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
trunc.ate 0 Posted February 4, 2004 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... 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
Valik 478 Posted February 4, 2004 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
Jon 1,009 Posted February 4, 2004 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. Deployment Blog:Â https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming:Â https://www.autoitconsulting.com/site/sccm-sdk/ Share this post Link to post Share on other sites
trunc.ate 0 Posted February 4, 2004 You mean...relying on Microsoft? I'll give it a try Share this post Link to post Share on other sites
Jon 1,009 Posted February 4, 2004 I'll post a user function to do a safe temp directory clean in a mo - just writing it. Deployment Blog:Â https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming:Â https://www.autoitconsulting.com/site/sccm-sdk/ Share this post Link to post Share on other sites
Jon 1,009 Posted February 4, 2004 (edited) Remove the MsgBox and uncomment the DirRemove and FileDelete when you are SURE you know how it works expandcollapse popup; 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 February 4, 2004 by Jon Deployment Blog:Â https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming:Â https://www.autoitconsulting.com/site/sccm-sdk/ Share this post Link to post Share on other sites
Jon 1,009 Posted February 4, 2004 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. Deployment Blog:Â https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming:Â https://www.autoitconsulting.com/site/sccm-sdk/ Share this post Link to post Share on other sites
Jon 1,009 Posted February 5, 2004 Here is the same script modified to work with the new v3.0.92 unstable. expandcollapse popup; 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 Deployment Blog:Â https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming:Â https://www.autoitconsulting.com/site/sccm-sdk/ Share this post Link to post Share on other sites
trunc.ate 0 Posted February 5, 2004 Works perfectly, thanks! Share this post Link to post Share on other sites
trids 2 Posted February 5, 2004 I may change it to return a handle like the FileOpen function.Now THAT's a cool idea!!!! Share this post Link to post Share on other sites
trunc.ate 0 Posted February 6, 2004 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? Share this post Link to post Share on other sites
Valik 478 Posted February 6, 2004 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
trids 2 Posted February 6, 2004 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
trunc.ate 0 Posted February 6, 2004 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
trids 2 Posted February 6, 2004 There we go then .. 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
trunc.ate 0 Posted August 12, 2004 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. Have there been any changes to the File functions? Share this post Link to post Share on other sites
trunc.ate 0 Posted August 12, 2004 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