简体   繁体   中英

How to delete a batch file along with all other files in the folder?

Can I delete a file that is the one running the batch that is deleting the file?

I need to make a batch file that will run if the username for the program isn't right. It's like recaptcha but more annoying, when it is run it deletes all the files in the same folder then self destructs.

To clarify, you are looking to remove the batch file after completion.

I think this StackOverflow post can help you.

EDIT

I don't know too much about Batch but a quick search gave me this:

@Echo off

set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

del "%~f0"

This would delete all files and folders inside the folder variable you specify.

Then you can append to this script with one of the answers from the StackOverflow post I mentioned at the start. I picked one at random, hence the del "%~f0"

It is pretty damn simple. Just write this into the batch and place it in the directory.

del /Q /F *

Which will delete everything in the folder, including the batch file itself.

Note!! I take no responsibility if you delete something you were not supposed to.

This is all you need at end of file

rmdir "C:\directory" /S /Q
del "C:\my.bat"

Make sure the program and the directory you are deleting are different. I personally have file in the startup to delete my program once set to self-destruct on restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM