[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]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. }
复制代码

本帖最后由 zaqmlp 于 2023-7-28 12:31 编辑

.txt和.bat需存为ansi/gb2312编码
  1. <# :
  2. cls&echo off&cd /d "%~dp0"&set "current=%cd%"
  3. path %SYSTEMROOT%\System32\WindowsPowerShell\v1.0;%path%
  4. powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
  5. pause
  6. exit
  7. #>
  8. $oldword=',';
  9. $newword=' ';
  10. $current=($env:current).trimend('\');
  11. $enc=[Text.Encoding]::GetEncoding('GB2312');
  12. $files=@(dir -literal $current -recurse|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
  13. for($i=0;$i -lt $files.length;$i++){
  14.     write-host $files[$i].FullName.Substring($current.length);
  15.     $text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
  16.     $text=$text.replace($oldword, $newword);
  17.     [IO.File]::WriteAllText($files[$i].FullName, $text, $enc);
  18. }
复制代码
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

回复 1# bobocoolsky


创建任务计划请参考:
https://mp.weixin.qq.com/s/LKTmJuq45bZbxoGVTfla1w
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 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

回复 4# bobocoolsky

已修改
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

返回列表