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

Killing and Terminating Apps

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

WININIT and Deleting or Moving Files that are in Use

Keywords:      delete winbatch script after use

Question:

Is there any way to DELETE the script itself that's CURRENTLY running in memory? I tried it but it won't let you delete it since it is in use.

Answer:

Method #1:

If you are running Windows NT, there is the IntControl(30.... function, which performs a delayed file move.

The file is not actually moved until the operating system is restarted. This can be useful for replacing system files. Under Windows 95, and in the 16-bit version, this function performs a regular (non-delayed) FileMove.


Method #2:

If you are running Win95 there is the WININIT.INI file - which incidentally is NOT and ini file and cannot be successfully edited with INI functions where you can put information to rename or delete files.

More information on WININIT is available from:



http://www.microsoft.com/msdn/sdk/inetsdk/help/itt/wininet/functions/general.htm#wininet_functions
One method is to have:

	thisfile = filefullname(winexename(""))
	windir = StrCat(DirWindows(0),"wininit.ini")
	hnd = FileOpen(windir, "APPEND") ; in case exists
	line = StrCat ("NUL=",thisfile)
	FileWrite(hnd, line)
	FileClose(hnd)
The file will be deleted on the next startup of the computer.

If the WININIT.INI file already exists, the above code will work, but if it does not exist, then


	[rename]
ought to be the first line in the file (see below).

Note that the WININIT.INI file is NOT an INI file (be careful) and as such cannot be reliably written to via the iniwritepvt function.

If you create a file called WININIT.INI in the Windows directory, files listed will be renamed during bootup. (If you look at the WININIT.INI and .BAK files in your Windows directory you can figure out how this works.)

An example WININIT.INI file would be:


     [rename] 
     C:\Fred\Fault.exe=C:\Fred\Fault.1 
to delete a file use:

     [rename]
     NULL=C:\Fred\Fault.exe
Note: I believe the syntax is destination = source. Looks kind of backwards. Also, this doesn't support long file names.

Method #3:

Another method that works is to remember that Dos Batch files can delete themselves if there is NO CRLF after the last line, and so the following script works:

; create a temporary batch file
hnd = FileOpen("c:\temp\wbtest\temp.bat", "WRITE")
FileWrite(hnd, "@echo off")
FileWrite(hnd, "for %%f in (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1) do dir
>nul")
; above line si a dos-only time-waster to allow for the winbatch exe to complete
FileWrite(hnd, "del test14.exe")
FileWrite(hnd, "del temp.bat")
FileClose(hnd)
fs = filesize("temp.bat")
bin = binaryalloc(fs)
binaryread(bin,"c:\temp\wbtest\temp.bat")
binaryeodset(bin,fs-2)
binarywrite(bin, "c:\temp\wbtest\temp.bat")
; note: binary functions not tested. The idea
is to delete the last CRLF from temp.bat
; may be better done with in one swoop with
binaryPokeStr instead of filewrite statements

dirchange("c:\temp\wbtest") ; or put in the path in the del statements above
Run("command.com", "/c c:\temp\wbtest\temp.bat")
exit ; this line should go RIGHT after the call to the batch file
I have tried the above except for the binary statements (having manually created the temp.bat file) and it successfully deletes both itself and the winbatch exe file, and closes the Dos box.
Article ID:   W13261
Filename:   WININIT and Moving or Deleting Files that are in Use.txt
File Created: 2001:01:26:13:59:34
Last Updated: 2001:01:26:13:59:34