Jump to content

Delete Folder only when the file inside it has been finished using


Recommended Posts

I'm adding a Zip file named 'BATMAN.zip' in my script.

FileInstall("C:\Users\tanji\OneDrive\Desktop\BATMAN.zip", @ScriptDir & "\BATMAN.zip")

 

Then exctracting it.

Const $sZipFile = @ScriptDir & "\BATMAN.zip"
Const $sDestFolder = @ScriptDir
UnZip($sZipFile, $sDestFolder)
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error unzipping file : " & @error)
     
 Func UnZip($sZipFile, $sDestFolder)
  If Not FileExists($sZipFile) Then Return SetError (1) ; source file does not exists
  If Not FileExists($sDestFolder) Then
  If Not DirCreate($sDestFolder) Then Return SetError (2) ; unable to create destination
  Else
    If Not StringInStr(FileGetAttrib($sDestFolder), "D") Then Return SetError (3) ; destination not folder
  EndIf
  Local $oShell = ObjCreate("shell.application")
  Local $oZip = $oShell.NameSpace($sZipFile)
  Local $iZipFileCount = $oZip.items.Count
  If Not $iZipFileCount Then Return SetError (4) ; zip file empty
  For $oFile In $oZip.items
    $oShell.NameSpace($sDestFolder).copyhere($ofile)
  Next
EndFunc   ;==>UnZip

 

When extracted 'BATMAN.zip' it becomes -> 
Folder1\script.cmd. And I want to delete 'BATMAN.zip' and 'Folder1' and it's contents once my 'script.cmd' stops/closes running. To do that I'm adding 

Local $FilePath = @ScriptDir & "\BATMAN"
Local $DEL = @ScriptDir & "\BATMAN.zip"
Local $sFilePath = @ScriptDir & "\Folder1"

RunWait(@ScriptDir & "\Folder1\script.cmd","", @SW_HIDE)

Local $DeleteDEL = FileDelete($DEL)
Local $DELME = DirRemove($sFilePath, $DIR_REMOVE)


I've added these code to do this but my code deletes those contents before it's being run. So my script doesn't run, it shows can't find the file. Even though
I'm using RunWait/ShellExecuteWait, but it doesn't work. Folder gets deleted before the script inside it runs.

 

 

Link to comment
Share on other sites

42 minutes ago, Danp2 said:

See the remarks section for RunWait in the help file, specifically --

 

I've been trying to figure out for a long time. 

Note: I'm not running any commands directly, I'm running a batch file :( . Even tried ProcesssWaitClose("cmd.exe")

Link to comment
Share on other sites

3 hours ago, TanjimReza said:

I've been trying to figure out for a long time.

Here is a small test environment :
* create a test directory (this works as your @ScriptDir)
* copy Run-bat.au3 there
* copy Test-Batman.zip there and unzip it (Batman.zip inside is just a dummy for testing)

Run-bat.au3 :

#include <AutoItConstants.au3>
#include <WinAPIShPath.au3>

Local $sFilePath   = @ScriptDir & "\Folder1"
Local $sDeleteFile = @ScriptDir & "\Batman.zip"
Local $iPID = Run($sFilePath & "\script.cmd","", @SW_SHOW)  ; later @SW_HIDE
If Not @error Then
    If ProcessWaitClose($iPID) Then
        ConsoleWrite("+ >>>>>> @@DEBUG > OK : ProcessWaitClose " & @CRLF)

        If FileDelete($sDeleteFile) Then
            ConsoleWrite("+ >>>>>> @@DEBUG > OK : FileDelete " & @CRLF)
        Else
            ConsoleWrite("! >>>>>> @@DEBUG > ERROR : FileDelete " & @CRLF)
        EndIf

        If DirRemove(_WinAPI_PathAddBackslash($sFilePath), $DIR_REMOVE) Then
            ConsoleWrite("+ >>>>>> @@DEBUG > OK : DirRemove " & @CRLF)
        Else
            ConsoleWrite("! >>>>>> @@DEBUG > ERROR : DirRemove " & @CRLF)
        EndIf
    Else
        ConsoleWrite("! >>>>>> @@DEBUG > ERROR : ProcessWaitClose " & @CRLF)
    EndIf
    ConsoleWrite(@CRLF)
Else
    ConsoleWrite("! >>>>>> @@DEBUG > ERROR : RUN " & @CRLF & @CRLF)
EndIf

Test-Batman.zip

Edited by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

9 hours ago, Musashi said:

Here is a small test environment :
* create a test directory (this works as your @ScriptDir)
* copy Run-bat.au3 there
* copy Test-Batman.zip there and unzip it (Batman.zip inside is just a dummy for testing)

Run-bat.au3 :

#include <AutoItConstants.au3>
#include <WinAPIShPath.au3>

Local $sFilePath   = @ScriptDir & "\Folder1"
Local $sDeleteFile = @ScriptDir & "\Batman.zip"
Local $iPID = Run($sFilePath & "\script.cmd","", @SW_SHOW)  ; later @SW_HIDE
If Not @error Then
    If ProcessWaitClose($iPID) Then
        ConsoleWrite("+ >>>>>> @@DEBUG > OK : ProcessWaitClose " & @CRLF)

        If FileDelete($sDeleteFile) Then
            ConsoleWrite("+ >>>>>> @@DEBUG > OK : FileDelete " & @CRLF)
        Else
            ConsoleWrite("! >>>>>> @@DEBUG > ERROR : FileDelete " & @CRLF)
        EndIf

        If DirRemove(_WinAPI_PathAddBackslash($sFilePath), $DIR_REMOVE) Then
            ConsoleWrite("+ >>>>>> @@DEBUG > OK : DirRemove " & @CRLF)
        Else
            ConsoleWrite("! >>>>>> @@DEBUG > ERROR : DirRemove " & @CRLF)
        EndIf
    Else
        ConsoleWrite("! >>>>>> @@DEBUG > ERROR : ProcessWaitClose " & @CRLF)
    EndIf
    ConsoleWrite(@CRLF)
Else
    ConsoleWrite("! >>>>>> @@DEBUG > ERROR : RUN " & @CRLF & @CRLF)
EndIf

Test-Batman.zip 523 B · 0 downloads

I'll try it !! Thanks!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...