JacolBeyros Posted May 10, 2009 Share Posted May 10, 2009 Alright guys, I've been using Autoit for a short while, and while trying to write a program, I've ran into a little snag. I'm trying to use DirRemove($directory, 1) to remove a directory made by the executing script, but it doesnt work. Is it possible to delete it this way, or is there a better approach? Thanks in advance guys! Link to comment Share on other sites More sharing options...
Wanery Posted May 10, 2009 Share Posted May 10, 2009 Jacol, Try this example: ;Directory Create/Remove ;you can insert some simple code to troubleshoot the problem ; ;declare variables for directory name and error checking (see helpfile) local $directory="c:\test", $iErr1=0, $iErr2=0 ;capture error/no error returned by "DirCreate()" $iErr1=DirCreate($directory) ;setup string value of $sResult for message box display according to value of Err1 if $iErr1<1 then $sResult="Drectory create failed!" ElseIf $iErr1=1 Then $sResult="Drectory create succeeded!" endif MsgBox(1,"Directory Create",($sResult)) ;capture error/no error returned by "DirRemove()" $iErr2=DirRemove($directory,1) ;setup string value of $sResult for message box display according to value of Err2 ;value of success for DirRemove is opposite of DirCreate (see helpfile) if $iErr2>0 then $sResult="Drectory delete succeeded!" ElseIf $iErr2=1 Then $sResult="Drectory delete failed!" endif MsgBox(1,"Directory remove",($sResult)) Pay close attention to syntax and variable assignments in your code. Hope this helps, Wanery Link to comment Share on other sites More sharing options...
JacolBeyros Posted May 10, 2009 Author Share Posted May 10, 2009 Thank you for your response Wanery, but I know exactly what the problem is. The folder being created by my script is not being deleted by the same script. When the script is done running, I can then delete it by hand. In between creation and deletion, many things are happening inside of the folder, and i can't move anything anywhere else. So, the folder is "in use" while it shouldn't be. Is there any way to still delete this folder, or figure out what particular part is "using" it, and then stop it from doing that? Link to comment Share on other sites More sharing options...
MrMitchell Posted May 11, 2009 Share Posted May 11, 2009 Jacol, can you post a mini version of your code that can recreate the problem? Link to comment Share on other sites More sharing options...
JacolBeyros Posted May 13, 2009 Author Share Posted May 13, 2009 (edited) Here's a little mock-up: $directory = FileFindFirstFile(@DesktopDir & "\temp\*") While 1 $file = FileFindNextFile($directory) If @Error Then ExitLoop FileCopy(@DesktopDir & "\temp\" & $file, @ScriptDir) RunWait(@ScriptDir & "\program1.exe " & @ScriptDir & "\" & $file) ;This creates the folder, with some contents RunWait(@ScriptDir & "\program2.exe " & @ScriptDir & "\folder\*.ext") ;This is the action inside of the folder DirRemove(@ScriptDir & "\folder", 1) FileDelete(@ScriptDir & basename($file)) Wend This isnt perfect, I just scratched it out in a couple minutes. Here's the problem: DirRemove isnt removing the folder. Which causes program1.exe to fail in the next pass of the loop. I do get a "file in use" error, but what is using it, and how can I null that? Edited May 13, 2009 by JacolBeyros Link to comment Share on other sites More sharing options...
rajeshontheweb Posted May 13, 2009 Share Posted May 13, 2009 probably some one else might help you out on why it failed but can u try this and see if cmd works? ; untested code $rdFolder = @ScriptDir & "\folder" $rdSuccess = DirRemove($rdFolder, 1) If $rdSuccess = 0 Then Run(@Comspec & " /c " & "'rd " & $rdFolder & " /s /Q '", "", @SW_HIDE) Started late is much better than having never started it!!!!Failure is another step towards success. I've been messing around with: Adding Entry to 'Hosts'File Information Lister (Logger)Yet Another AutoIT Error Handler Yet Another AutoIT Error Handler & Debugger Control your App's TaskBar Button YCurrency Ticker (Latest Release : 16 Apr 2009)_WinInetInternetCheckConnection UDF Symantec Definitions Lister UDF _GetLocalIPAddresses UDF UDF to get Special Folder Information WMI_NetworkAdapterConfiguration2Array WMI_CDRomDriveCapabilities _ScriptExists - Check if your au3 script is running!! Uninstaller UDF Get Version for your application (at script level or compiled stage) Uninstaller Pro - faster alternative to windows application removal applet Link to comment Share on other sites More sharing options...
JacolBeyros Posted May 13, 2009 Author Share Posted May 13, 2009 Well, whatever I was supposed to see, if anything, wasn't there. I ran it as I see it, nothing. I changed @SW_HIDE to @SW_SHOW, and got a cmd screen flash. The folder stands strong. Link to comment Share on other sites More sharing options...
rajeshontheweb Posted May 13, 2009 Share Posted May 13, 2009 (edited) $directory = FileFindFirstFile(@DesktopDir & "\testing") $rdFolder = @DesktopDir & "\testing\NEWFOLDER" ConsoleWrite($rdFolder) While 1 $file = FileFindNextFile($directory) If @error Then ExitLoop FileCopy(@DesktopDir & "\temp\" & $file, @ScriptDir) DirCreate($rdFolder); this creates the folder.. ;~ RunWait(@ScriptDir & "\program1.exe " & @ScriptDir & "\" & $file);This creates the folder, with some contents ;~ RunWait(@ScriptDir & "\program2.exe " & @ScriptDir & "\folder\*.ext");This is the action inside of the folder $rdSuccess = DirRemove($rdFolder, 1) If $rdSuccess = 0 Then Run(@ComSpec & " /c " & "rd " & Chr(34) & $rdFolder & Chr(34) & " /s /Q ", "", @SW_HIDE) EndIf WEnd if u wanna see the errors / out put state just use Run(@ComSpec & " /k " & "rd " & Chr(34) & $rdFolder & Chr(34) & " /s /Q ", "", @SW_SHOW) Edited May 13, 2009 by rajeshontheweb Started late is much better than having never started it!!!!Failure is another step towards success. I've been messing around with: Adding Entry to 'Hosts'File Information Lister (Logger)Yet Another AutoIT Error Handler Yet Another AutoIT Error Handler & Debugger Control your App's TaskBar Button YCurrency Ticker (Latest Release : 16 Apr 2009)_WinInetInternetCheckConnection UDF Symantec Definitions Lister UDF _GetLocalIPAddresses UDF UDF to get Special Folder Information WMI_NetworkAdapterConfiguration2Array WMI_CDRomDriveCapabilities _ScriptExists - Check if your au3 script is running!! Uninstaller UDF Get Version for your application (at script level or compiled stage) Uninstaller Pro - faster alternative to windows application removal applet Link to comment Share on other sites More sharing options...
JacolBeyros Posted May 14, 2009 Author Share Posted May 14, 2009 rajeshontheweb, that's what I tried with the previous post. But I tried again with the "Chr(34)"s. Still nothing. I tried pausing the script and running the same in a batch file, but again, it didnt work. I'm starting to wonder if you can delete a folder/file that is in use by another program. I tried messing around with ProcessClose and ProcessExist, but didn't come up with anything. It may have been my limited knowloedge of those functions and Autoit itself, but I'm trying. Link to comment Share on other sites More sharing options...
BigDod Posted May 14, 2009 Share Posted May 14, 2009 Use a batch script to delete the required directory and execute it when the script is exited. Time you enjoyed wasting is not wasted time ......T.S. Elliot Suspense is worse than disappointment................Robert Burns God help the man who won't help himself, because no-one else will...........My Grandmother Link to comment Share on other sites More sharing options...
trancexx Posted May 14, 2009 Share Posted May 14, 2009 I know this may sound a bit plain, but before anything else you should try: ;... DirRemove($rdFolder, 1) DirRemove($rdFolder) ;Exit If that doesn't work, do other... stuff. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
rajeshontheweb Posted May 14, 2009 Share Posted May 14, 2009 the way i can think of is, using handle.exe and suspend those processes temporarily ??? (risky to kill them ,actually!) Started late is much better than having never started it!!!!Failure is another step towards success. I've been messing around with: Adding Entry to 'Hosts'File Information Lister (Logger)Yet Another AutoIT Error Handler Yet Another AutoIT Error Handler & Debugger Control your App's TaskBar Button YCurrency Ticker (Latest Release : 16 Apr 2009)_WinInetInternetCheckConnection UDF Symantec Definitions Lister UDF _GetLocalIPAddresses UDF UDF to get Special Folder Information WMI_NetworkAdapterConfiguration2Array WMI_CDRomDriveCapabilities _ScriptExists - Check if your au3 script is running!! Uninstaller UDF Get Version for your application (at script level or compiled stage) Uninstaller Pro - faster alternative to windows application removal applet Link to comment Share on other sites More sharing options...
herewasplato Posted May 14, 2009 Share Posted May 14, 2009 If your mock up code is close to real, then you maybe attempting the DirRemove too soon after program2.exe ends. It might take the OS a few milliseconds to release all of the handles.Of course, inserting a sleep line will tell you if that is the issue or you can try this:Install Unlocker 1.8.7 http://ccollomb.free.fr/unlocker/It does about the same thing as handle.exe but wrapped in a GUI.BTW, the video in the screen shot is about W7's AppLockerChange your code to something like this line...MsgBox(0,"",DirRemove(@ScriptDir & "\folder", 1))...so that the script will halt and...you will know the results of the DirRemove func.If the DirRemove func fails - leave the msgbox up.From within Windows explorer > right click on the folder that did not delete.Select the Unlocker tool from the context menu.Unlocker may tell you the app that has a handle locking things...Or the handle(s) might have cleared before you could get to it/them.You might have to loop the DirRemove func.for $i = 1 to 100sleep(100)if $i = 99 thenmsgbox(0,"","back to the drawing board") exit endifif DirRemove(@ScriptDir & "\folder", 1) then exitloopnextIf none of that works - spend some time......making a sample script for us that really does lock a folder.(Easy 4 me 2 say - maybe hard 4 you 2 do :-) [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
JacolBeyros Posted May 15, 2009 Author Share Posted May 15, 2009 BigDod, as I stated in my previous post, I did try a batch file. It failed. trancexx, I've also tried that. But if one doesn't work, 40,000 won't either. rajeshontheweb and herewasplato, I will try handle.exe out. Thank you. I will also try to make a script that does this. I appreciate all your help, guys/gals. Link to comment Share on other sites More sharing options...
rajeshontheweb Posted May 15, 2009 Share Posted May 15, 2009 hmm, also, just try to let u program sleep for a few seconds or two before u execute the next line, forgot to mention. (notified above..) Started late is much better than having never started it!!!!Failure is another step towards success. I've been messing around with: Adding Entry to 'Hosts'File Information Lister (Logger)Yet Another AutoIT Error Handler Yet Another AutoIT Error Handler & Debugger Control your App's TaskBar Button YCurrency Ticker (Latest Release : 16 Apr 2009)_WinInetInternetCheckConnection UDF Symantec Definitions Lister UDF _GetLocalIPAddresses UDF UDF to get Special Folder Information WMI_NetworkAdapterConfiguration2Array WMI_CDRomDriveCapabilities _ScriptExists - Check if your au3 script is running!! Uninstaller UDF Get Version for your application (at script level or compiled stage) Uninstaller Pro - faster alternative to windows application removal applet Link to comment Share on other sites More sharing options...
JacolBeyros Posted May 16, 2009 Author Share Posted May 16, 2009 Ugh. I tried using handle.exe and Unlocker, and handle.exe filled up the command prompt and then some, so I didn't get all the processes. Unlocker worked well, but said that the script ITSELF was using the folder. If I clicked on "kill", the exe shut down, of course, and was not able to finish. So it is back to the drawing board unless you know how to avoid this? I've already got some ideas if you don't. Link to comment Share on other sites More sharing options...
JacolBeyros Posted May 20, 2009 Author Share Posted May 20, 2009 Alright, after thinking about it, I finally figured out how to fix my problem. What I did was make each loop run in a new folder. Like so:$v = 0$directory = FileFindFirstFile(@DesktopDir & "\temp\*")While 1$file = FileFindNextFile($directory)If @Error Then ExitLoopDirCreate(@ScriptDir & "\temp" & $v)FileCopy(@DesktopDir & "\temp\" & $file, @ScriptDir & "\temp" & $v)RunWait(@ScriptDir & '\program1.exe "' & @ScriptDir & "\temp" & $v & "\" & $file') ;This creates the folder, with some contentsRunWait(@ScriptDir & '\program2.exe " & @ScriptDir & "\temp" & $v & "\"folder\*.ext') ;This is the action inside of the folderDirRemove(@ScriptDir & "\temp" & $v & "\folder", 1)$v = $v + 1WendThe bold parts are what I changed. Since the undeleted folder isn't needed anymore, I can just leave it there and let it be deleted by the next startup of the script. Actually, the DirRemove function isn't even needed anymore. Thanks all for your help! 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