kowala's home

kowala's home
這裡是我的學習筆記,陸續增加中。
http://kowala21.blogspot.com

2012-11-09

用批次檔來刪除檔案目錄

在寫程式時,常常需要反覆的測試,有時候就要常常建檔、建目錄等等,這時候批次檔就很好用了,下面舉一個例子來說明怎樣用批次檔來處理檔案目錄。

一開始的檔案目錄結構
 

接著是產生很多測試目錄及檔案後
 

 我們要刪掉上面紅線框起來的檔案或目錄,但有些目錄不是每次都會產生,所以要先檢查是否存在,若存在再刪。

下面是批次檔的寫法

BACK.bat
 ------------------------------------------------------
@echo off
echo 還原最初狀態... > msg.txt
echo 刪除 V-01 - V-04  >> msg.txt
del V* >> msg.txt
echo 刪除 MM14 TBAT  >> msg.txt
del MM14 TBAT >> msg.txt
echo 刪除 HISTORY >> msg.txt
del HISTORY >> msg.txt

:NNN
if not exist NNN goto MMM
echo 刪除 \NNN\*  >> msg.txt
cd NNN
del *
cd..
rd NNN

:MMM
if not exist M01 goto WWW
echo 刪除 \M01\* - \M04\*  >> msg.txt
cd M01
del *
cd..
rd M01

cd M02
del *
cd..
rd M02

cd M03
del *
cd..
rd M03

cd M04
del *
cd..
rd M04

:WWW
if not exist W01 goto VVV
echo 刪除 \W01\* - \W04\*  >> msg.txt
cd W01
del *
cd..
rd W01

cd W02
del *
cd..
rd W02

cd W03
del *
cd..
rd W03

cd W04
del *
cd..
rd W04

:VVV
if not exist V01 goto quit
echo 刪除 \V01\* - \V04\*  >> msg.txt
cd V01
del *
cd..
rd V01

cd V02
del *
cd..
rd V02

cd V03
del *
cd..
rd V03

cd V04
del *
cd..
rd V04

:quit
echo 還原初始值,copy test\101\* . >> msg.txt
del GO10*
copy test\101\* .

notepad msg.txt
del msg.*


執行結果


 順便看看訊息


清乾淨了,再把初始資料 copy 進來,有批次檔幫忙,工作簡單多了。