[英]Create/find batch script which performs a 'find and replace' within a directory and sub-directories, for excel files with varied names
[英]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”字样的行。
"
-请勿这样做! for
循环时,例如for /R %%f
,将%
符号加倍。 for /R
在没有*
或?
类的全局通配符时实际上不会搜索匹配文件?
; 因此最好在for /F "delims=" %%f in ('dir /B /S "index.htm"') do ...
。 findstr
命令行放入for
循环的主体中for
并使用for
变量引用%%f
作为文件名。 所有这些意味着:
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.