快速删除C#项目中的bin和obj文件夹的方法
C#程序总会生成bin和obj文件夹,为了减小源码的大小,就有必要将这两个文件夹删除,于是想到用批处理文件来删除。
以下是批处理的代码:
@echo off
set nowPath=%cd%
cd /
cd %nowPath%
::delete specify file(*.pdb,*.vshost.*)
for /r %nowPath% %%i in (*.pdb,*.vshost.*) do (del %%i)
::delete specify folder(obj,bin)
for /r %nowPath% %%i in (obj,bin) do (IF EXIST %%i RD /s /q %%i)
echo OK
pause
注:
1、批处理中两个半角冒号开头表示注释掉这一行
2、将以上代码复制到记事本中,并且命名为bat后缀的文件,如clear.bat.
3、clear.bat最好放到要执行删除操作的目录(或上层目录)
如果只要删除某个文件,可以将for /r %nowPath% %%i in (obj,bin) do (IF EXIST %%i RD /s /q %%i) 这一句去掉,然后将for /r %nowPath% %%i in (*.pdb,*.vshost.*) do (del %%i) 这一句中的(*.pdb,*.vshost.*)改成要删除的文件。
出处:https://www.cnblogs.com/zjoch/p/5770409.html
=======================================================================================
我自己稍微修改和优化了下上面的代码,如下:
优化1
@echo off if [%1] equ [] echo 【未指定文件夹】 &goto end set fp=%1 set fp="%fp%" & set fp=%fp:"=% if "%fp%" neq "" if "%fp:~-1%" neq "\" set fp=%fp%\ set nowPath="%fp%" set /a findFileCount=0 set /a findFolderCount=0 set /a delFileCount=0 set /a delFolderCount=0 set delFile=*.vshost.* set delFolder=bin,obj echo 准备清理%nowPath%目录下的 echo 文件:%delFile% &echo 文件夹:%delFolder% echo; &echo -------------------------------------------------- rem delete specify folder(such as:obj,bin) ::for /r %nowPath% %%i in (obj,bin) do (IF EXIST "%%i" echo RD /s /q "%%i") echo 【待删除文件夹:%delFolder%】 & for /r %nowPath% %%i in (%delFolder%) do (IF EXIST "%%i" echo "%%i" &set /a findFolderCount+=1) echo; &echo -------------------------------------------------- rem delete specify file(such as:*.pdb,*.vshost.*) ::for /r %nowPath% %%i in (*.pdb,*.vshost.*) do (echo del "%%i") echo 【待删除文件:%delFile%】 & for /r %nowPath% %%i in (%delFile%) do (IF EXIST "%%i" echo "%%i" &set /a findFileCount+=1) echo; echo; if %findFileCount% equ 0 if %findFolderCount% equ 0 echo [file or folder not found] &goto end echo 共找到 %findFileCount% 个文件和 %findFolderCount% 个文件夹 set /p var=是否确定删除以上文件和文件夹?[Y/N]: if /i %var%==Y (goto DelFileAndFolder) else (goto cancelDel) :DelFileAndFolder echo Deleting file ...... rem delete specify folder(such as:obj,bin) ::for /r %nowPath% %%i in (obj,bin) do (IF EXIST "%%i" echo RD /s /q "%%i") for /r %nowPath% %%i in (%delFolder%) do (IF EXIST "%%i" RD /s /q "%%i" &set /a delFolderCount+=1) rem delete specify file(such as:*.pdb,*.vshost.*) ::for /r %nowPath% %%i in (*.pdb,*.vshost.*) do (IF EXIST "%%i" echo del "%%i") for /r %nowPath% %%i in (%delFile%) do (IF EXIST "%%i" del "%%i" &set /a delFileCount+=1) echo; &echo [Delete completed] echo delete %delFolderCount% folders &echo delete %delFileCount% files &echo; goto end :cancelDel echo; &echo [Cancel deletion] &echo; :end Pause
优化2
@echo off title 【删除bin和obj文件夹】 echo; if [%1] equ [] echo 【未指定文件夹】 &goto end set fp=%1 set fp="%fp%" & set fp=%fp:"=% if "%fp%" neq "" if "%fp:~-1%" neq "\" set fp=%fp%\ set nowPath="%fp%" set /a findFileCount=0 set /a findFolderCount=0 set /a findAllCount=0 set /a delFileCount=0 set /a delFolderCount=0 set delFileOrFolder=*.vshost.*,bin,obj echo; &echo 待清理目录:%nowPath% echo; &echo 删除指定目录下的: set delType=%delFileOrFolder% :loop for /f "tokens=1* delims=," %%a in ("%delType%") do echo %%a &set delType=%%b if defined delType goto :loop echo; &echo 按任意键将继续,退出请关闭窗口。 &pause>nul echo; &echo -------------------------------------------------- echo 待删除列表: rem search delete specify file or folder(such as:*.pdb,*.vshost.*) ::for /r %nowPath% %%i in (*.pdb,*.vshost.*) do (echo del "%%i") for /r %nowPath% %%i in (%delFileOrFolder%) do (IF EXIST "%%i" echo "%%i" &IF EXIST "%%i\.\" (set /a findFolderCount+=1) else set /a findFileCount+=1) set /a findAllCount=%findFolderCount%+%findFileCount% echo; if %findAllCount% equ 0 echo [file or folder not found] &goto end echo 共找到%findAllCount%个待删除项【文件:%findFileCount%个,文件夹:%findFolderCount%个】 echo -------------------------------------------------- set /p var=是否确定删除以上文件或文件夹?[Y/N]: if /i "%var%"=="Y" (goto DelFileAndFolder) else goto cancelDel :DelFileAndFolder echo [Deleting file] ...... rem delete specify file or folder(such as:obj,bin,*.pdb,*.vshost.*) ::for /r %nowPath% %%i in (%delFileOrFolder%) do (IF EXIST "%%i" IF EXIST "%%i\.\" (echo "%%i\.\") ELSE echo "%%i") for /r %nowPath% %%i in (%delFileOrFolder%) do (IF EXIST "%%i" IF EXIST "%%i\.\" (RD /s /q "%%i" &set /a delFolderCount+=1) else del "%%i" &set /a delFileCount+=1) echo; &echo [Delete completed] delete %delFileCount% files, delete %delFolderCount% folders &echo; goto end :cancelDel echo; &echo [Cancel deletion] &echo; :end Pause
优化3
优化了显示待删除文件或文件的总大小
优化了受set只支持32位的限制,可以支持大数字的加法运算
优化支出中文路径的文件大小的计算。
@echo off&chcp 936>nul&setlocal enabledelayedexpansion title 【删除bin和obj文件夹】 echo; if [%1] equ [] echo 【未指定文件夹】 &goto end set fp=%1 set fp="%fp%" & set fp=%fp:"=% if "%fp%" neq "" if "%fp:~-1%" neq "\" set fp=%fp%\ set nowPath="%fp%" set /a findFileCount=0 set /a findFolderCount=0 set /a findAllCount=0 set /a delFileCount=0 set /a delFolderCount=0 set /a fileSize=0 set delFileOrFolder=*.vshost.*,bin,obj echo; &echo 待清理目录:%nowPath% echo; &echo 删除指定目录下的: set delType=%delFileOrFolder% :loop for /f "tokens=1* delims=," %%a in ("%delType%") do echo %%a &set delType=%%b if defined delType goto :loop echo; &echo 按任意键将继续,退出请关闭窗口。 &pause>nul echo; &echo -------------------------------------------------- echo 待删除列表: rem search delete specify file or folder(such as:*.pdb,*.vshost.*) ::for /r %nowPath% %%i in (*.pdb,*.vshost.*) do (echo del "%%i") for /r %nowPath% %%i in (%delFileOrFolder%) do (IF EXIST "%%i" (echo "%%i"&IF EXIST "%%i\.\" (set /a findFolderCount+=1) else set /a findFileCount+=1 set fs=0&(for /f "tokens=3* delims= " %%a in ('dir "%%i" /a-d /s /-c ^| find /i "个文件"') do set fs=%%a)&call :bigNumAdd !fileSize! !fs! fileSize )) set /a findAllCount=%findFolderCount%+%findFileCount% echo;&if %findAllCount% equ 0 echo [file or folder not found] &goto end call :numToThousand %fileSize% fileSize echo 共找到%findAllCount%个待删除项【文件:%findFileCount%个,文件夹:%findFolderCount%个,总共大小:%fileSize% bytes】 echo -------------------------------------------------- set /p var=是否确定删除以上文件或文件夹?[Y/N]: if /i "%var%"=="Y" (goto DelFileAndFolder) else goto cancelDel :DelFileAndFolder rem delete specify file or folder(such as:obj,bin,*.pdb,*.vshost.*) ::for /r %nowPath% %%i in (%delFileOrFolder%) do (IF EXIST "%%i" IF EXIST "%%i\.\" (echo "%%i\.\") ELSE echo "%%i") for /r %nowPath% %%i in (%delFileOrFolder%) do (IF EXIST "%%i" (echo Deleting file "%%i"&IF EXIST "%%i\.\" (RD /s /q "%%i" &set /a delFolderCount+=1) else del "%%i" &set /a delFileCount+=1)) echo; &echo [Delete completed] delete %delFileCount% files, delete %delFolderCount% folders, total size %fileSize% bytes &echo; goto end :cancelDel echo; &echo [Cancel deletion] &echo; :end Pause goto :eof ::浮点加法 call _ADD <被加数> <加数> [返回变量] ::分别支持十进制的64位整数部分和64位小数部分,一次计算仅用0.01秒 :bigNumAdd <被加数> <加数> [返回变量] // by jack on 2021-09-03 (setlocal enabledelayedexpansion set L=&for /l %%a in (1,1,8) do set L=!L!00000000 for /f "tokens=1-3 delims=." %%a in ("!L!%1.!L!") do set at=%%a&set aw=%%b%%c for /f "tokens=1-3 delims=." %%a in ("!L!%2.!L!") do set bt=%%a&set bw=%%b%%c set a=!at:~-64!!aw:~,64!&set b=!bt:~-64!!bw:~,64!&set e=&set v=200000000 for /l %%a in (8,8,128)do set/a v=1!b:~-%%a,8!+1!a:~-%%a,8!+!v:~-9,-8!-2&set e=!v:~-8!!e! set e=!e:0= !&for /f "tokens=*" %%a in ("!e:~,-64!_.!e:~64!") do set e=%%~nxa set e=!e:_=!&for %%a in ("!e: =0!") do endlocal&(if %3.==. (echo %%~a) else set %3=%%~a) exit/b) ::整数字符转成千分符表示 :numToThousand <数字参数> [返回变量] (setlocal enabledelayedexpansion&set n=%~1 set len=0&for /l %%i in (0,1,1000) do if "!n:~%%i,1!."=="." set len=%%i&goto :breakFor :breakFor set nv=&for /l %%a in (1,1,%len%)do (set nv=!n:~-%%a,1!!nv!&set/a m=%%a%%3&if !m!==0 if not %%a==%len% set nv=,!nv!) endlocal&(if %2.==. (echo %nv%) else set %2=%nv%) exit/b)
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/15205470.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!
posted on 2021-08-30 13:22 jack_Meng 阅读(1544) 评论(0) 编辑 收藏 举报