Sign in to follow this  
Followers 0
JacolBeyros

Removing Dir used by script

17 posts in this topic

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! :)

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Jacol, can you post a mini version of your code that can recreate the problem?

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by JacolBeyros

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 by rajeshontheweb

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Use a batch script to delete the required directory and execute it when the script is exited.



Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Posted Image

BTW, the video in the screen shot is about W7's AppLocker

Change 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 100

sleep(100)

if $i = 99 then

msgbox(0,"","back to the drawing board")

exit

endif

if DirRemove(@ScriptDir & "\folder", 1) then exitloop

next

If 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]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 ExitLoop

DirCreate(@ScriptDir & "\temp" & $v)

FileCopy(@DesktopDir & "\temp\" & $file, @ScriptDir & "\temp" & $v)

RunWait(@ScriptDir & '\program1.exe "' & @ScriptDir & "\temp" & $v & "\" & $file') ;This creates the folder, with some contents

RunWait(@ScriptDir & '\program2.exe " & @ScriptDir & "\temp" & $v & "\"folder\*.ext') ;This is the action inside of the folder

DirRemove(@ScriptDir & "\temp" & $v & "\folder", 1)

$v = $v + 1

Wend

The 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!

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