簡體   English   中英

在目錄和子目錄中搜索文件,並編輯這些文件中的特定行

[英]Searching for files in a directory and sub-directories and editing specific lines within those files

在目錄和子目錄中搜索文件,然后編輯這些文件中的特定行。

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

我在stackoverflow上搜索了答案,並找到了以下代碼:

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的主目錄和子目錄中的文件,以刪除其中的特定行或帶有“ shaw”字樣的行。

  1. 您要在無用的引號上放置" -請勿這樣做!
  2. 在批處理文件中使用for循環時,例如for /R %%f ,將%符號加倍。
  3. for /R在沒有*?類的全局通配符時實際上不會搜索匹配文件? ; 因此最好在for /F "delims=" %%f in ('dir /B /S "index.htm"') do ...
  4. 只需將findstr命令行放入for循環的主體中for並使用for變量引用%%f作為文件名。
  5. 使用重定向將命令的輸出寫入文件; 但是您不能指定該命令已經處理的文件,因此您需要使用一個臨時文件,然后將其移至原始文件。

所有這些意味着:

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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM