Windows batch script:findstr來搜尋指定的字串並判斷成功的範例

 已經習慣在Linux中使用grep的指令來搜尋字串,而在Windows系統中,可以用指令findstr來搜尋字串

下面是一個範例,使用errorlevel這個環境變數來判斷findstr沒有沒有找到指字的字串,如果成功,則會傳回0

findstr /m "Export.terminated" db_exp.log
if %errorlevel%==0 (
echo "found"
) else (
echo "not found"   
)

上面的範例是我用來判斷Oracle DB在exp資料後產生的log文字檔,是否有成功。

因為空白會被分詞,例如「Export terminated」就會被拆成「Export」、「terminated」兩個字,

只要符合其中一個就可以,所以我加上萬用字符「.」,讓程式知道「Export」、「terminated」中間還有一個字。


Oracle DB的listener.log清理