Is it possible to make a batch file that would delete all the files and folders that the batch file is located in?
是否可以生成一个批处理文件,删除批处理文件所在的所有文件和文件夹?
I.E: I place the batch file into folder with useless files, I run it, and it deletes all the files and folders in that folder.
I.E:我将批处理文件放入包含无用文件的文件夹中,我运行它,并删除该文件夹中的所有文件和文件夹。
Then I can just move the batch file to another folder and do the same there...
然后我可以将批处理文件移动到另一个文件夹并在那里做同样的事情......
Would really help me out! I need this to delete temporary files on other people's computers before installing new files... But sadly I am not very familiar with batch.
真的会帮助我!我需要这个在安装新文件之前删除其他人的计算机上的临时文件......但遗憾的是我对批处理并不是很熟悉。
1 个解决方案
#1
2
To remove all the files in the current directory except your batch file use:
要删除当前目录中除批处理文件以外的所有文件,请使用:
echo off
for %%i in (*.*) do if not "%%i"=="del.bat" del /q "%%i"
Note that the "del.bat" is the name of the batch file you save this as.
请注意,“del.bat”是您将其另存为的批处理文件的名称。
As another side note you could add after the del /q the "/p" command to make this a little bit safer. That way it will prompt you before deleting each file.
另请注意,您可以在del / q之后添加“/ p”命令以使其更安全一些。这样它会在删除每个文件之前提示您。
To remove everything in the folder including the .bat file use
要删除包含.bat文件的文件夹中的所有内容
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
To remove everything including folders except the .bat file use
删除除.bat文件以外的所有文件夹
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || if not "%%i"=="del.bat" del "%%i" /s/q)
Where del.bat is your batch file name.
其中del.bat是您的批处理文件名。
#1
2
To remove all the files in the current directory except your batch file use:
要删除当前目录中除批处理文件以外的所有文件,请使用:
echo off
for %%i in (*.*) do if not "%%i"=="del.bat" del /q "%%i"
Note that the "del.bat" is the name of the batch file you save this as.
请注意,“del.bat”是您将其另存为的批处理文件的名称。
As another side note you could add after the del /q the "/p" command to make this a little bit safer. That way it will prompt you before deleting each file.
另请注意,您可以在del / q之后添加“/ p”命令以使其更安全一些。这样它会在删除每个文件之前提示您。
To remove everything in the folder including the .bat file use
要删除包含.bat文件的文件夹中的所有内容
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
To remove everything including folders except the .bat file use
删除除.bat文件以外的所有文件夹
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || if not "%%i"=="del.bat" del "%%i" /s/q)
Where del.bat is your batch file name.
其中del.bat是您的批处理文件名。