2017-02-13 67 views
0

搜索這些文件中的目錄中的文件和子目錄和編輯特定行內搜索目錄中的文件和子目錄和編輯特定行。這些文件

C:\Users\user\Desktop\backup>dir 
Volume in drive C has no label. 
Volume Serial Number is C8D8-4C1B 

Directory of C:\Users\user\Desktop\backup 

02/13/2017 03:02 PM <DIR>   . 
02/13/2017 03:02 PM <DIR>   .. 
02/13/2017 02:21 PM <DIR>   blog 
02/13/2017 02:21 PM <DIR>   css 
02/13/2017 02:21 PM <DIR>   forgot 
02/13/2017 02:21 PM <DIR>   img 
02/13/2017 02:21 PM   13,845 index.htm 
02/13/2017 02:21 PM <DIR>   pages 
02/13/2017 02:21 PM <DIR>   photo 
02/13/2017 02:21 PM <DIR>   photos 
02/13/2017 02:21 PM <DIR>   profile 
02/13/2017 02:21 PM <DIR>   signin 
02/13/2017 03:11 PM    89 test.bat 
02/13/2017 02:21 PM <DIR>   theme 
02/13/2017 02:21 PM <DIR>   view 
2 File(s)   13,934 bytes 
13 Dir(s) 74,300,223,488 bytes free 

我搜索計算器上的答案,發現這個代碼:

for /R %f in (index.htm)" do "x" 

findstr /v /i "body" index.htm > indexnew.htm 

我想出了與此代碼失敗:

"for /R %f in (index.htm)" do "findstr /v /i "shaw" index.htm >  indexnew.htm" 

pause 

失敗。

我需要操作的主目錄和子目錄中的文件名爲index.htm的刪除特定的行或與在其中單詞「肖」。

+0

您需要使用變量作爲FINDSTR將解析的文件名。 – Squashman

+0

我將如何調用for變量? %F? –

回答

2
  1. 要放置無用引號"在奇數位置 - 不這樣做!
  2. 雙擊一個批處理文件中使用for循環時,像for /R %%f%跡象。
  3. for /R實際上並不搜索時沒有全局通配符像*?匹配的文件;所以最好使用for /F "delims=" %%f in ('dir /B /S "index.htm"') do ...
  4. 只需將findstr命令行放入for循環的主體中,並使用for變量引用%%f作爲文件名。
  5. 使用redirection寫命令的輸出到一個文件;但是你不能指定已經被命令處理過的同一個文件,所以你需要使用一個臨時文件,然後把它移到原來的文件上。

這一切都意味着:

for /F "delims=" %%f in ('dir /B /S "index.htm"') do (
    findstr /V /I /C:"shaw" "%%~f" > "%%~f.tmp" 
    move /Y "%%~f.tmp" "%%~f" > nul 
) 
相關問題