[英]Searching for files in a directory and sub-directories and editing specific lines within those files
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
I've searched on stackoverflow for answers and found this code: 我在stackoverflow上搜索了答案,并找到了以下代码:
for /R %f in (index.htm)" do "x"
and 和
findstr /v /i "body" index.htm > indexnew.htm
I came up with and failed with this code: 我想出了这个代码,但失败了:
"for /R %f in (index.htm)" do "findstr /v /i "shaw" index.htm > indexnew.htm"
pause
It fails. 它失败。
I need to manipulate files in main directory and sub-directories with the name index.htm to delete specific lines or lines with the word "shaw" within them. 我需要操作名称为index.htm的主目录和子目录中的文件,以删除其中的特定行或带有“ shaw”字样的行。
"
at odd positions -- do not do that! "
-请勿这样做! %
sign when using for
loops in a batch file, like for /R %%f
. for
循环时,例如for /R %%f
,将%
符号加倍。 for /R
does not actually search for matching files when there is no global wild-card like *
or ?
for /R
在没有*
或?
类的全局通配符时实际上不会搜索匹配文件?
; for /F "delims=" %%f in ('dir /B /S "index.htm"') do ...
. for /F "delims=" %%f in ('dir /B /S "index.htm"') do ...
。 findstr
command line into the body of the for
loop and use the for
variable reference %%f
as the file name. findstr
命令行放入for
循环的主体中for
并使用for
变量引用%%f
作为文件名。 All this means: 所有这些意味着:
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.