简体   繁体   English

在目录和子目录中搜索文件,并编辑这些文件中的特定行

[英]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”字样的行。

  1. You are placing useless quotation marks " at odd positions -- do not do that! 您要在无用的引号上放置" -请勿这样做!
  2. Double the % sign when using for loops in a batch file, like for /R %%f . 在批处理文件中使用for循环时,例如for /R %%f ,将%符号加倍。
  3. for /R does not actually search for matching files when there is no global wild-card like * or ? for /R在没有*?类的全局通配符时实际上不会搜索匹配文件? ; ; so it is better to use for /F "delims=" %%f in ('dir /B /S "index.htm"') do ... . 因此最好在for /F "delims=" %%f in ('dir /B /S "index.htm"') do ...
  4. Simply place the 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作为文件名。
  5. Use redirection to write the output of a command into a file; 使用重定向将命令的输出写入文件; but you cannot specify the same file that is already processed by the command, so you need to use a temporary file and move it onto the original one afterwards. 但是您不能指定该命令已经处理的文件,因此您需要使用一个临时文件,然后将其移至原始文件。

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.

相关问题 创建/查找批处理脚本,该脚本在目录和子目录中执行“查找和替换”,适用于具有不同名称的 excel 文件 - Create/find batch script which performs a 'find and replace' within a directory and sub-directories, for excel files with varied names 将多个子目录中的文件复制到具有匹配的子目录名称的另一个目录 - Copy files in multiple sub-directories to another directory with matching sub-directory names 删除所有子目录和子文件而不删除父/根目录? - Remove All Sub-Directories and Sub-Files Without Removing Parent/Root Directory? 如何使用批处理脚本从当前目录复制文件,而不复制子目录? - How do I copy files from the current directory, but not sub-directories using a batch script? 尝试使用Bat文件获取目录和子目录中所有文件的行数 - Trying to Get A Line Count for all Files in a Directory and Sub-Directories using a Bat File ImageMagick 的批处理命令在 Windows 上转换目录和子目录中的所有文件 - Batch command for ImageMagick to convert all files in a directory and sub-directories on windows 批处理脚本以查找文件所在的目录,将该目录和子目录中的所有文件复制到另一个目录 - batch script to find the directory where a file is located, copy all files located in that directory and sub-directories to another directory 重命名子目录中的文件 - Renaming files within sub directories 通过子目录将.wav文件批量转换为.mp3文件,然后删除其余的.wav文件 - Batch convert .wav files to .mp3 files through sub-directories then delete the remaining .wav files 使用批处理脚本将特定文件从源目录复制到目标目录和子目录 - Copy specific file from source directory to target directory and sub-directories using batch script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM