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

[文本处理] [已解决]批处理根据文件名修改文件内容

文件夹内有5000个以内的文本,文本名称是6个数字串命名,000001.txt  300001.txt  600001.txt……。
文本内容如下:
20181217,0.000
20181218,0.000
20181219,0.000
20181220,0.000
20181221,3175862.750
20181224,0.000
20181225,0.000
20181226,0.000
20181227,0.000
....................
20211129,14264013.000
20211130,4888363.000
20211201,3961839.000
20211202,6931708.000
20211203,6048756.000
........
需将文本名称是0和3及8开头生成内容为 (将文本名称是0和3及8开头的,加入“0,文本名称6位数字串,”到文本内容中)
0,000001,20181217,0.000
0,000001,20181218,0.000
0,000001,20181219,0.000
0,000001,20181220,0.000
0,000001,20181221,3175862.750
0,000001,20181224,0.000
0,000001,20181225,0.000
0,000001,20181226,0.000
0,000001,20181227,0.000
....................
0,000001,20211129,14264013.000
0,000001,20211130,4888363.000
0,000001,20211201,3961839.000
0,000001,20211202,6931708.000
0,000001,20211203,6048756.000
........
需将文本名称是6和9开头生成内容为 (将文本名称是6和9开头的,加入“1,文本名称6位数字串,”到文本内容中)
1,600001,20181217,406515.000
1,600001,20181218,691664.000
1,600001,20181219,0.000
1,600001,20181220,0.000
........................
1,600001,20211126,82824.000
1,600001,20211129,107226
1,600001,20211130,69069
1,600001,20211201,37772
1,600001,20211202,26606
1,600001,20211203,253500
......
请老师帮忙写一个批理,谢谢!

本帖最后由 for_flr 于 2021-12-23 10:06 编辑

测试前先备份!!
测试前先备份!!
测试前先备份!!
  1. #&@cls&echo off&powershell -c "type %~0|out-string|iex"&pause&exit
  2. Function change($a,$b,$c){
  3.   $txt=gc $a
  4.   Clear-content $a
  5.   Foreach($line in $txt){
  6.     $newline="$c,$b,$line"
  7.     Add-content -path $a -value $newline
  8.   }
  9. }
  10. $folder="C:\Users\lenovo\Desktop\测试用例"  #定义文本所在文件夹
  11. $txts=Dir -path $folder -filter *.txt|?{$_.basename -match "^\d{6}$"}
  12. Foreach($a in $txts){
  13.   Switch($a.basename.substring(0,1)){
  14.     0{change $a.fullname $a.basename 0}
  15.     3{change $a.fullname $a.basename 0}
  16.     8{change $a.fullname $a.basename 0}
  17.     6{change $a.fullname $a.basename 1}
  18.     9{change $a.fullname $a.basename 1}
  19.   }
  20. }
复制代码
1

评分人数

    • PCL0769: 现在就试试,谢谢老师的帮助!技术 + 1

TOP

回复 2# for_flr
谢谢for_flr 老师的帮助!
代码完美执行,for_flr 老师辛苦你啦!万分感谢!

TOP

回复 2# for_flr
for_flr 老师能帮增加转换后合并生成到一个新的文本中吗!谢谢!

TOP

  1. #&@cls&echo off&powershell -c "type '%~0'|out-string|iex"&pause&exit
  2. Function change($a,$b,$c){
  3.   $txt=gc $a
  4.   Clear-content $a
  5.   Foreach($line in $txt){
  6.     $newline="$c,$b,$line"
  7.     Add-content -path $a -value $newline
  8.     }
  9.   (gc $a)|out-file -append -filepath $result #合并
  10. }
  11. $folder="C:\Users\lenovo\Desktop\测试用例"  #定义文本所在文件夹
  12. $result="C:\Users\lenovo\Desktop\测试用例\result_.txt" #定义合并文本路径
  13. $txts=Dir -path $folder -filter *.txt|?{$_.basename -match "^\d{6}$"}
  14. Foreach($a in $txts){
  15.   Switch($a.basename.substring(0,1)){
  16.     0{change $a.fullname $a.basename 0}
  17.     3{change $a.fullname $a.basename 0}
  18.     8{change $a.fullname $a.basename 0}
  19.     6{change $a.fullname $a.basename 1}
  20.     9{change $a.fullname $a.basename 1}
  21.   }
  22. }
复制代码
1

评分人数

    • PCL0769: 谢谢for_flr老师的帮助!代码完美运行,多谢 ...技术 + 1

TOP

返回列表