trunc.ate Posted February 4, 2004 Share 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? Link to comment Share on other sites More sharing options...
trids Posted February 4, 2004 Share 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 .. ? Link to comment Share on other sites More sharing options...
trunc.ate Posted February 4, 2004 Author Share 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? Link to comment Share on other sites More sharing options...
Valik Posted February 4, 2004 Share Posted February 4, 2004 Isn't the temp directory a shell folder? If so, aren't they automatically recreated when they are needed, anyway? Link to comment Share on other sites More sharing options...
Administrators Jon Posted February 4, 2004 Administrators Share 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/ Link to comment Share on other sites More sharing options...
trunc.ate Posted February 4, 2004 Author Share Posted February 4, 2004 You mean...relying on Microsoft? I'll give it a try Link to comment Share on other sites More sharing options...
Administrators Jon Posted February 4, 2004 Administrators Share 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/ Link to comment Share on other sites More sharing options...
Administrators Jon Posted February 4, 2004 Administrators Share 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/ Link to comment Share on other sites More sharing options...
Administrators Jon Posted February 4, 2004 Administrators Share 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/ Link to comment Share on other sites More sharing options...
Administrators Jon Posted February 5, 2004 Administrators Share 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/ Link to comment Share on other sites More sharing options...
trunc.ate Posted February 5, 2004 Author Share Posted February 5, 2004 Works perfectly, thanks! Link to comment Share on other sites More sharing options...
trids Posted February 5, 2004 Share Posted February 5, 2004 I may change it to return a handle like the FileOpen function.Now THAT's a cool idea!!!! Link to comment Share on other sites More sharing options...
trunc.ate Posted February 6, 2004 Author Share 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? Link to comment Share on other sites More sharing options...
Valik Posted February 6, 2004 Share 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... Link to comment Share on other sites More sharing options...
trids Posted February 6, 2004 Share 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\*.*") Link to comment Share on other sites More sharing options...
trunc.ate Posted February 6, 2004 Author Share 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 Link to comment Share on other sites More sharing options...
trids Posted February 6, 2004 Share 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() Link to comment Share on other sites More sharing options...
trunc.ate Posted August 12, 2004 Author Share 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? Link to comment Share on other sites More sharing options...
trunc.ate Posted August 12, 2004 Author Share 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now