找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 20683|回复: 3

[文本处理] [已解决]如何用批处理根据内容 替换 指定内容?

[复制链接]
发表于 2022-5-3 10:45:06 | 显示全部楼层 |阅读模式
本帖最后由 czvde 于 2022-5-4 18:43 编辑

123文件夹如下:
aaa.txt
aaa-1.txt

例如

aaa.txt的内容如下:
0123456
111ccc0011ccc0020


调用12.txt的数据进行替换

运行批处理后的效果

aaa-1.txt的内容如下:
0123456
111ccc二零三零ccc三零111

已解决!
发表于 2022-5-4 14:22:36 | 显示全部楼层
  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. $a=gc '123.txt'
  3. dir '123\*.txt'|%{
  4.     $n=$_.fullname
  5.     while(test-path $n){
  6.         if($n -match '.*?-(\d+)\.txt$'){
  7.             $n=$matches[0] -replace '(\d+)(?=\.txt$)',([int]$matches[1]+1)
  8.         }else{
  9.             $n=$_.DirectoryName+'\'+$_.basename+'-1'+$_.extension
  10.         }
  11.     }
  12.     sc $n $(gc $_.fullname|%{
  13.         $c=$_
  14.         $a|%{
  15.             $d=$_ -split ','
  16.             $d|%{
  17.                 if($c -cmatch $d[$i++]){
  18.                     if($d[$i] -ne $null){
  19.                         $e=$c -creplace "$_",$d[$i]
  20.                     }else{
  21.                         $e=$c -creplace "$_",$d[0]
  22.                     }
  23.                 }
  24.             }
  25.             rv i
  26.         }
  27.         if($e -eq $null){$c}else{$e;rv e}
  28.     })
  29. }
复制代码

评分

参与人数 1技术 +1 收起 理由
czvde + 1 谢谢

查看全部评分

 楼主| 发表于 2022-5-4 16:43:40 | 显示全部楼层
本帖最后由 czvde 于 2022-5-4 18:37 编辑

回复 2# idwma


    方便实用,谢谢
发表于 2022-5-4 17:58:28 | 显示全部楼层
  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. $s=@{
  3. 1='001'
  4. 2='ABCD'
  5. }
  6. $s
  7. $s=$s[[int](read-host)]
  8. $a=gc '123.txt'
  9. dir '123\*.txt'|?{$_.name -notmatch '.*-\d+.txt'}|%{
  10.     $n=$_.fullname
  11.     while(test-path $n){
  12.         if($n -match '.*?-(\d+)\.txt$'){
  13.             $n=$matches[0] -replace '(\d+)(?=\.txt$)',([int]$matches[1]+1)
  14.         }else{
  15.             $n=$_.DirectoryName+'\'+$_.basename+'-1'+$_.extension
  16.         }
  17.     }
  18.     sc $n $(gc $_.fullname|%{
  19.         $e=$_
  20.         $a|%{
  21.             $d=$_ -split ','
  22.             if($d[1] -cmatch "$s---([^、]+)"){$e=$e -creplace $d[0],$matches[1]}
  23.         }
  24.         $e
  25.     })
  26. }
复制代码

评分

参与人数 1技术 +1 收起 理由
czvde + 1 乐于助人

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 00:51 , Processed in 0.025247 second(s), 12 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表