[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

批量替换多级目录下所有txt内的指定字符(10元红包感谢)

需要批量替换多级目录下所有txt内的指定字符,不清楚如果批量修改指定目录下多个多级文件夹下的txt文件,求助,以下是找的单个修改的代码可以用,不知道如何修改for才行
  1. <# :
  2. cls&echo off&cd /d "%~dp0"
  3. rem 将当前目录里的一个dat文件里的指定字符内容替换成其他指定字符内容
  4. set #=Any question&set _=WX&set $=Q&set/az=0x53b7e0b4
  5. title %#% +%$%%$%/%_% %z%
  6. set "current=%cd%"&set "tmpfile=%tmp%\t.t"
  7. dir /a-d-h/b "*.txt">"%tmpfile%"
  8. powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
  9. echo;%#% +%$%%$%/%_% %z%
  10. exit
  11. #>
  12. $tmpfile=$env:tmpfile;
  13. $outfolder=($env:current).trimend("\")+"\result";
  14. if(-not (test-path -literal $outfolder)){[void][IO.Directory]::CreateDirectory($outfolder);}
  15. $enc=[Text.Encoding]::GetEncoding('GB2312');
  16. $files=[IO.File]::ReadAllLines($tmpfile, $enc);
  17. if($files.Count -ge 1){$files[0];
  18. $text=[IO.File]::ReadAllText($files[0], $enc);
  19. $text=$text.replace(',', ' ');
  20. $newfile=$outfolder+'\'+$files[0];
  21. [IO.File]::WriteAllText($newfile, $text, $enc);
  22. }
复制代码

回复 2# zaqmlp

文件路径D:\upload\12723698327402\1\multivalue_5.txt等几个文件,bat放在upload文件夹,一个txt大约300k

辛苦大神,只是代码测试出现以下错误,烦劳帮忙看一下,谢谢:

\12723698327402\1\multivalue_5.txt
Exception calling "ReadAllText" with "2" argument(s): "Could not find file 'D:\
upload\multivalue_5.txt'."
At line:15 char:5
+     $text=[IO.File]::ReadAllText($files[0], $enc);
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ( [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

You cannot call a method on a null-valued expression.
At line:16 char:5
+     $text=$text.replace($oldword, $newword);
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

\12723698327402\1\multivalue_6.txt
Exception calling "ReadAllText" with "2" argument(s): "Could not find file 'D:\
upload\multivalue_5.txt'."
At line:15 char:5
+     $text=[IO.File]::ReadAllText($files[0], $enc);
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

You cannot call a method on a null-valued expression.
At line:16 char:5
+     $text=$text.replace($oldword, $newword);
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

\12723698327402\1\multivalue_7.txt
Exception calling "ReadAllText" with "2" argument(s): "Could not find file 'D:\
upload\multivalue_5.txt'."
At line:15 char:5
+     $text=[IO.File]::ReadAllText($files[0], $enc);
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

You cannot call a method on a null-valued expression.
At line:16 char:5
+     $text=$text.replace($oldword, $newword);
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

请按任意键继续. . .

TOP

回复 4# bobocoolsky


    好像是只能当前文件夹下

TOP

回复 2# zaqmlp


    还有就是换行符也没有了

TOP

返回列表