批處理文件將刪除批處理文件所在的所有文件和文件夾?

[英]Batch file that would delete all the files and folders that the batch file is located in?


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是您的批處理文件名。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/02/25/b245c461d4b57701da50c358d02394a4.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com