标题: [文本处理] [已解决]批处理根据文件名修改文件内容 [打印本页]
作者: PCL0769 时间: 2021-12-22 22:16 标题: [已解决]批处理根据文件名修改文件内容
文件夹内有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:04
本帖最后由 for_flr 于 2021-12-23 10:06 编辑
测试前先备份!!
测试前先备份!!
测试前先备份!!- #&@cls&echo off&powershell -c "type %~0|out-string|iex"&pause&exit
- Function change($a,$b,$c){
- $txt=gc $a
- Clear-content $a
- Foreach($line in $txt){
- $newline="$c,$b,$line"
- Add-content -path $a -value $newline
- }
- }
- $folder="C:\Users\lenovo\Desktop\测试用例" #定义文本所在文件夹
- $txts=Dir -path $folder -filter *.txt|?{$_.basename -match "^\d{6}$"}
- Foreach($a in $txts){
- Switch($a.basename.substring(0,1)){
- 0{change $a.fullname $a.basename 0}
- 3{change $a.fullname $a.basename 0}
- 8{change $a.fullname $a.basename 0}
- 6{change $a.fullname $a.basename 1}
- 9{change $a.fullname $a.basename 1}
- }
- }
复制代码
作者: PCL0769 时间: 2021-12-24 13:44
回复 2# for_flr
谢谢for_flr 老师的帮助!
代码完美执行,for_flr 老师辛苦你啦!万分感谢!
作者: PCL0769 时间: 2021-12-24 13:46
回复 2# for_flr
for_flr 老师能帮增加转换后合并生成到一个新的文本中吗!谢谢!
作者: for_flr 时间: 2021-12-24 15:10
- #&@cls&echo off&powershell -c "type '%~0'|out-string|iex"&pause&exit
- Function change($a,$b,$c){
- $txt=gc $a
- Clear-content $a
- Foreach($line in $txt){
- $newline="$c,$b,$line"
- Add-content -path $a -value $newline
- }
- (gc $a)|out-file -append -filepath $result #合并
- }
- $folder="C:\Users\lenovo\Desktop\测试用例" #定义文本所在文件夹
- $result="C:\Users\lenovo\Desktop\测试用例\result_.txt" #定义合并文本路径
- $txts=Dir -path $folder -filter *.txt|?{$_.basename -match "^\d{6}$"}
- Foreach($a in $txts){
- Switch($a.basename.substring(0,1)){
- 0{change $a.fullname $a.basename 0}
- 3{change $a.fullname $a.basename 0}
- 8{change $a.fullname $a.basename 0}
- 6{change $a.fullname $a.basename 1}
- 9{change $a.fullname $a.basename 1}
- }
- }
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |