2017-08-11 256 views
2

我有一個函數已經能夠刪除所有在幾個文件中不包含字符串的行,它對於使用普通字符串非常有效:Windows批處理:在文件中查找字符串中帶有「[」的字符串

@echo off 
set "string_to_find=level.waypoints[" 
for /f "tokens=*" %%a in ('dir /B *.gsc') do (
      set "tempfile=%temp%\%%a" 
      if exist "%tempfile%" del "%tempfile%" >NUL 
      findstr /C:"%string_to_find%" "%~dp0\%%a" >> "%tempfile%" 
      if not errorlevel 1 (
         del "%%a" >NUL 
         move /Y "%tempfile%" "%~dp0\%%a" >NUL 
         if exist "%tempfile%" del "%tempfile%" >NUL 
         echo File "%~dp0\%%a" processed successfully 
      ) else (
      echo Problem processing file "%~dp0\%%a" 
      ) 
) 

如果我搜索只是「level.waypoints」(沒有「[」)工作正常,但我不想保留的幾行不會被刪除。我需要搜索「level.waypoints [」來真正刪除我需要的所有行,但是因爲char「[」會使所有內容出錯,搞亂臨時文件,最後給出錯誤「file not發現「...

我認爲應該有一些字符,我需要把之前」[「像」[「使它的工作,但我找不到它...嘗試已經很多,但沒有運氣。 :/

那麼我怎樣才能搜索字符串「level.waypoints [」和工作?

感謝

+1

你會想要一個轉義字符。看到這個︰https://ss64.com/nt/syntax-esc.html – jmoon

+2

haven'tr嘗試過你的腳本,但'findstr/c:「level.waypoints [」* .txt'在我的位置正常工作。無論如何,你將有更好的運氣與PowerShell –

+0

@jmoon謝謝!在那個頁面中,我發現爲什麼「^」不起作用,只需要使用「\」。現在工作很好!乾杯! – Hajas

回答

2

論@jmoon發送的鏈接說:

特殊情況 命令少數遵循的規則略有不同,FINDSTR,REG和RUNAS所有使用\作爲轉義字符,而不是的^

嘗試和工作!所以結束字符串是「level.waypoints \ [」

乾杯!

相關問題