Board logo

标题: [文本处理] [已解决]批处理替换目录下所有文件指定行 [打印本页]

作者: 279491611    时间: 2019-6-26 11:17     标题: [已解决]批处理替换目录下所有文件指定行

本帖最后由 279491611 于 2019-6-26 13:13 编辑

求助各位帮忙,
批处理替换 目录下所有文件指定行 。例如 第5 行  替换为 两个空格加文件名。
作者: zaqmlp    时间: 2019-6-26 12:20

  1. @echo off
  2. set info=互助互利,支付宝扫码头像,感谢赞助
  3. rem 有问题,可加QQ956535081及时沟通
  4. title %info%
  5. cd /d "%~dp0"
  6. set n=5
  7. powershell -NoProfile -ExecutionPolicy bypass ^
  8.     $files=@(dir^|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});^
  9.     for($i=0;$i -lt $files.length;$i++){^
  10.         write-host $files[$i].Name;^
  11.         $text=[IO.File]::ReadAllLines($files[$i].FullName,[Text.Encoding]::Default);^
  12.         if($text.length -ge %n%){^
  13.             $text[%n%-1]=((' '*2)+$files[$i].BaseName);^
  14.             [IO.File]::WriteAllLines($files[$i].FullName, $text, [Text.Encoding]::Default);^
  15.         };^
  16.     };^
  17.     write-host '%info%' -ForegroundColor green;
  18. pause
复制代码

作者: 279491611    时间: 2019-6-26 13:06

本帖最后由 279491611 于 2019-6-26 13:15 编辑

回复 2# zaqmlp

感谢你的帮助。!power shell  果然强大。!
作者: Batcher    时间: 2019-6-26 13:22

推荐试试 sed
http://bcn.bathome.net/s/tool/index.html?key=sed
  1. @echo off
  2. for /f "delims=" %%i in ('dir /b /a-d *.txt') do (
  3.     sed -i "5s/.*/  %%i/" "%%i"
  4. )
复制代码

作者: zaqmlp    时间: 2019-6-26 13:57

回复 3# 279491611

口头说没什么用,赞助实际点




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2