标题: 批量替换多级目录下所有txt内的指定字符(10元红包感谢) [打印本页]
作者: bobocoolsky 时间: 2023-7-28 00:33 标题: 批量替换多级目录下所有txt内的指定字符(10元红包感谢)
需要批量替换多级目录下所有txt内的指定字符,不清楚如果批量修改指定目录下多个多级文件夹下的txt文件,求助,以下是找的单个修改的代码可以用,不知道如何修改for才行- <# :
- cls&echo off&cd /d "%~dp0"
- rem 将当前目录里的一个dat文件里的指定字符内容替换成其他指定字符内容
- set #=Any question&set _=WX&set $=Q&set/az=0x53b7e0b4
- title %#% +%$%%$%/%_% %z%
- set "current=%cd%"&set "tmpfile=%tmp%\t.t"
- dir /a-d-h/b "*.txt">"%tmpfile%"
- powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
- echo;%#% +%$%%$%/%_% %z%
- exit
- #>
- $tmpfile=$env:tmpfile;
- $outfolder=($env:current).trimend("\")+"\result";
- if(-not (test-path -literal $outfolder)){[void][IO.Directory]::CreateDirectory($outfolder);}
- $enc=[Text.Encoding]::GetEncoding('GB2312');
- $files=[IO.File]::ReadAllLines($tmpfile, $enc);
- if($files.Count -ge 1){$files[0];
- $text=[IO.File]::ReadAllText($files[0], $enc);
- $text=$text.replace(',', ' ');
- $newfile=$outfolder+'\'+$files[0];
- [IO.File]::WriteAllText($newfile, $text, $enc);
- }
复制代码
作者: zaqmlp 时间: 2023-7-28 01:50
本帖最后由 zaqmlp 于 2023-7-28 12:31 编辑
.txt和.bat需存为ansi/gb2312编码- <# :
- cls&echo off&cd /d "%~dp0"&set "current=%cd%"
- path %SYSTEMROOT%\System32\WindowsPowerShell\v1.0;%path%
- powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
- pause
- exit
- #>
- $oldword=',';
- $newword=' ';
- $current=($env:current).trimend('\');
- $enc=[Text.Encoding]::GetEncoding('GB2312');
- $files=@(dir -literal $current -recurse|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
- for($i=0;$i -lt $files.length;$i++){
- write-host $files[$i].FullName.Substring($current.length);
- $text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
- $text=$text.replace($oldword, $newword);
- [IO.File]::WriteAllText($files[$i].FullName, $text, $enc);
- }
复制代码
作者: 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 |