WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Make WinBatch Delete itself

 Keywords:  delete EXE BAT SelfDelete self-delete DLLs Complete done clean up cleanup

Question:

I compiled a winbatch program, I would like to delete it after finish running.

I go this way...

----------------snip-----------
winbatch code here
.....
EXE_PATH = FILEPATH(WINEXENAME(""))
DEL_FILES_PATH = "C:\DELFILES.BAT"
ENTER = @CRLF

HANDLE = FILEOPEN(DEL_FILES_PATH, "WRITE")
FILEWRITE(HANDLE, "@ECHO OFF%ENTER%REM ******THIS FILE IS SAFE
TO DELETE******%ENTER%%ENTER%ECHO ON")
FILEWRITE(HANDLE, STRCAT("DEL /Q ",'"',EXE_PATH,"WW*I.DLL",'"'))
FILEWRITE(HANDLE, STRCAT("DEL /Q ",'"',EXE_PATH,"W*32I.DLL",'"'))
FILEWRITE(HANDLE, STRCAT("DEL /Q ",'"',WINEXENAME(""),'"'))
FILECLOSE(HANDLE)

RUNHIDE(DEL_FILES_PATH,"")
EXIT
--------------end----------
The problem was the EXE and core DLL files can't be deleted successfully.(File in use)

Any suggestion? Any hits? :-)

Answer:

The WinBatch part seems basically OK.

You need a better BAT file

Something like

:loop
era filea.exe
era fileb.dll
if exist filea.exe goto loop
if exist fileb.dll goto loop
era thisfile.bat
exit
So the BAT file will keep trying until the files are deleted, so it can wait for the Winbatch exe program to properly exit.
Here is some sample code that deletes the WIL dlls that get generated by a compiled EXE.
del_files_temp = FileCreateTemp("~ww")                            ; get a .tmp file
del_files_path=StrReplace(StrLower(del_files_temp),".tmp",".bat") ; create bat file name
FileRename(del_files_temp,del_files_path)                         ; rename .tmp to .bat file

handle = FileOpen(del_files_path, "WRITE")                        ; open bat file for write

FileWrite(handle, "@ECHO OFF%@CRLF%REM ******THIS FILE IS SAFE TO DELETE******%@CRLF%%@CRLF%ECHO ON")

FileWrite(handle, ":loop")                                        ; delete older dlls (if there) 2004A-
FileWrite(handle, "era wbd*34i.dll")                              ; delete dlls
FileWrite(handle, "era wbo*34i.dll")                              
FileWrite(handle, "if exist wbd*34i.dll goto loop")               ; if file not deleted, loop and try again
FileWrite(handle, "if exist wbo*34i.dll goto loop")

FileWrite(handle, ":loop2")                                       ; delete newer dlls  2004B+
FileWrite(handle, "era wbd*44i.dll")                              ; delete dlls
FileWrite(handle, "era wbo*44i.dll")                              
FileWrite(handle, "if exist wbd*44i.dll goto loop2")               
FileWrite(handle, "if exist wbo*44i.dll goto loop2")              ; if file not deleted, loop and try again

FileWrite(handle, ":loop3")                                       ; delete other files
FileWrite(handle, "era yourfile1.txt")                            ; delete your files
FileWrite(handle, "era yourfile2.txt")                            ; add other files to delete here
FileWrite(handle, "if exist yourfile1.txt goto loop3")           ; if file not deleted, loop and try again    
FileWrite(handle, "if exist yourfile2.txt goto loop3")
                                                                  ; delete *this* bat file
FileWrite(handle, StrCat("era ",del_files_path))

FileClose(handle)

RunHide(del_files_path,"")
Exit

Article ID:   W14857
File Created: 2012:10:18:10:50:12
Last Updated: 2012:10:18:10:50:12