Methods for vbs script and bat batch to delete itself

Time:2022-6-16

Vbs script and bat batch self deletion method (suicide)
Delete own: vbs
Save the following script as selfkill VBS or selfkill vbe:

Copy codeThe codes are as follows:


Set fso = CreateObject(“Scripting.FileSystemObject”)
f = fso.DeleteFile(WScript.ScriptName)
WScript.Echo( WScript.ScriptName)


Then run it to see if selfkill VBS magically disappeared? And the following dialog box is displayed normally^*^
The above script calls the FSO control and uses the scriptname attribute of the Wscript object in the WSH to get the file name of the script itself,
And call the deletefile method of FSO to delete itself!
Rewrite it a little:

Copy codeThe codes are as follows:


On error resume next ‘prevent errors
Set fso = CreateObject(“Scripting.FileSystemObject”)
WScript. Sleep 1000 ‘suspend script execution for 1 second
fso. Deletefile (wsscript.scriptname) ‘delete script itself <– more–>
If fso. FileExists(“c:selfkill.exe”) Then fso. Deletefile (“c:selfkill.exe”) ‘delete program


The program can dynamically generate a VBS self deletion script and call it to delete itself. The method is similar to the self deletion of batch files!
It should be noted that due to the misuse of scripts by viruses and worms, scripts may be mistaken for malicious code when deleting files!
Attachment: self deleting JS script:

Copy codeThe codes are as follows:


try{fso = new ActiveXObject(“Scripting.FileSystemObject”);
WScript. Sleep(1000);// Sleep for 1 second
fso. DeleteFile(WScript.ScriptName);// Delete script itself
fso. DeleteFile(“c:selfkill.exe”);// Remove program
}catch(e){}


Attachment: self deleting JS script:

Copy codeThe codes are as follows:


try{fso = new ActiveXObject(“Scripting.FileSystemObject”);
WScript. Sleep(1000);// Sleep for 1 second
fso. DeleteFile(WScript.ScriptName);// Delete script itself
fso. DeleteFile(“c:selfkill.exe”);// Remove program
}catch(e){}


Delete own: batch
Relevant knowledge:
%Application summary of 0
In batch processing,%0 represents the batch file itself.
In batch processing, you can delete yourself by using the command “del%0”.
With this feature, batch processing is often used as a full uninstall tool.
Of course, we can also use the extension for more purposes:
1. use “%~dp0” to get the path of the file;

Copy codeThe codes are as follows:


@echo off
echo %~dp0


2. use “%~nx0” to get the file name;

Copy codeThe codes are as follows:


@echo off
echo %~nx0


For example, there is a folder named AAA on my C drive, which contains many files
I want to delete all the files and folders in the g:aaa folder. How can I write a batch!
rd /s /q C:AAA
The above line of code just solves the problem! (the AAA folder is also deleted) see the explanation for details:
Delete a directory.
RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path
/S in addition to the directory itself, all subdirectories and
Documents. Used to delete the directory tree.
/Q quiet mode, no confirmation is required when deleting the directory tree with /s
Reference: rd/?
@echo off
echo %~nx0
You’d better put quotation marks on this one, or you won’t be able to kill it if there is a space in the middle of the name
I killed “copy 123.bat”
@echo off
del “%~nx0”