繁体   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