Board logo

标题: 批量替换多级目录下所有txt内的指定字符(10元红包感谢) [打印本页]

作者: bobocoolsky    时间: 2023-7-28 00:33     标题: 批量替换多级目录下所有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 01:50

本帖最后由 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. }
复制代码

作者: Batcher    时间: 2023-7-28 08:56

回复 1# bobocoolsky


创建任务计划请参考:
https://mp.weixin.qq.com/s/LKTmJuq45bZbxoGVTfla1w
作者: bobocoolsky    时间: 2023-7-28 09:27

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

请按任意键继续. . .
作者: bobocoolsky    时间: 2023-7-28 09:28

回复 4# bobocoolsky


    好像是只能当前文件夹下
作者: bobocoolsky    时间: 2023-7-28 10:09

回复 2# zaqmlp


    还有就是换行符也没有了
作者: zaqmlp    时间: 2023-7-28 12:32

回复 4# bobocoolsky

已修改




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