标题: [文本处理] 求助批量文件改名并生成列表并计数 [打印本页]
作者: jave000 时间: 2021-6-28 09:47 标题: 求助批量文件改名并生成列表并计数
本帖最后由 jave000 于 2021-9-10 10:12 编辑
同类搜索简述:
将*.i??批量改为*_i??.dgn,其中??是两位数字,不能是字母。
改名后将其生成列表并排序,其中文件名字母全部大写,且按规则在名称中插入“-”。
并将同名文件前缀的按照i??的数目计数。
额外要求是在同名文本中抽取信息填入其他列,我自学成功后再把代码发上来。
感谢楼下诸位
@echo off
PowerShell "dir *.i[0-9][0-9] | group {$_.BaseName} | forEach {'' + ++$i + ',' + ($_.Name.ToUpper() -replace '^(....)(....)(....)(.....)(.+)$', '$1-$2-$3-$4-$5') + ',,,,,,' + $_.Count; $_.Group | ren -NewName {$_.Name -replace '\.(...)$', '_$1.dgn'}}" > z-iso.csv
for /f "delims=" %%j in ('dir /b /s /a-d^|findstr /r /e "\.b[0-9][0-9]* \.x[0-9][0-9]* \.bom \.err \.h \.idf \.log \.prt"') do del /a /f /q "%%j"
作者: idwma 时间: 2021-6-28 15:41
本帖最后由 idwma 于 2021-6-28 16:00 编辑
改完名的dgn文件这个列表是要文件的内容,还是只要文件名
先来个简单的
@echo off&setlocal enabledelayedexpansion
set out=list.txt
for /f "delims=" %%i in ('dir/b mdixv710va13lr0521.i?? ^| findstr "^.*i[0-9][0-9].*"') do (
set na=%%~ni
set a=%%~ni_%%~xi
set a=!a:.=!
ren %%~i !a!.dgn
set no+1=1
)
set na=!na:~0,4!-!na:~4,4!-!na:~8,4!-!na:~12,5!-!na:~17!
for %%m in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do call set na=%%na:%%m=%%m%%
echo !no! !na! >> !out!
pause
作者: newswan 时间: 2021-6-28 16:55
不太明白,,,,,,,,
作者: jave000 时间: 2021-6-28 22:23
不好意思之前我写的不清楚,已经修改。
以下是需求中第一条的已经写好的代码,有两种,需要继续增加内容:
for %%i in (*.i??) do (if not %%~xi==.idf (for /f "delims=." %%j in ("%%~xi") do (ren %%i %%~ni_%%j.dgn)))
@echo off & setlocal enabledelayedexpansion
for %%i in (*.i??) do (
set file=%%i
(echo !file:~-1! | findstr /R [0-9] >nul) && (echo !file:~-2,1! | findstr /R [0-9] >nul) && (
set postfix=%%~xi
ren %%i %%~ni_!postfix:~1!.dgn
)
)
作者: WHY 时间: 2021-6-29 00:28
"第一列为序号,第二列为大写的比如MDIX-V710-VA13-LR052-1"
那么第3列、4列、5列、6列、7列分别应该填什么呢?
"这里它有六份" 指的是哪6份?
以顶楼的文件清单为例,你需要得到的1~8列分别是什么?
作者: newswan 时间: 2021-6-29 00:44
回复 3# newswan
还是没说清楚啊,给一个结果示例
作者: newswan 时间: 2021-6-29 01:27
- $pathSour = "."
- $fileDest = "33"
-
- if (-not ([string]::IsNullOrEmpty($args[0])))
- {
- $pathSour = $args[0]
- }
-
- remove-item $fileDest*
-
- [System.Collections.ArrayList] $al = @()
-
- $renfs = "(\w{4})(\w{1,4})?(\w{1,4})?(\w{1,5})?(.*)"
- $renfd = "`$1-`$2-`$3-`$4-`$5"
- $rem = "^ (PIPING SPEC |THERMAL INSULATION SPEC |PIPE NPD |CL LENGTH )"
- get-childitem -path $pathSour "*.prt" | foreach-object {
- write-host " "$_.basename
- $nf = $_.basename.ToUpper() -replace $renfs,$renfd -replace "-+$",""
- Get-ChildItem -path ($_.basename +".i??") -Exclude "*.idf" | ForEach-Object{
- #rename-item $_.name -NewName ($_.name+ ".dgn")
- }
- $fc = (get-content -Encoding utf8 $_.fullname) -match $rem
- $c6 = $fc -match " PIPING SPEC " -replace " PIPING SPEC (\w+)","`$1"
- $c7 = $fc -match " THERMAL INSULATION SPEC " -replace " THERMAL INSULATION SPEC (\w).*","`$1"
- if ($c7.length -eq 0) { $c7= "-" }
- $c5 = $fc -match " PIPE NPD \(MM\) " -replace ".{14}(.+)$","`$1"
- $c10 = $fc -match " CL LENGTH \(M\) "-replace ".{14}(.+)$","`$1"
- $c5 = $c5 -join "`t" -replace "\s+","`t" -split "`t" | Sort-Object { [double] $_ }
- $c10 = $c10 -join "`t" -replace "\s+","`t" -split "`t" | Sort-Object { [double] $_ }
- $al.add($nf) | out-null
- $al.add(" #6 $c6") | out-null
- $al.add(" #7 $c7") | out-null
- $al.add(" #5 " + "sum: " + (($c5 | Measure-Object -Sum).sum) + ($c5 -join "`t")) | out-null
- $al.add(" #10 " + "sum: " + (($c10 | Measure-Object -Sum).sum) + ($c10 -join "`t")) | out-null
- }
-
- $al | out-file -encoding utf8 ($fileDest + "-1.txt")
复制代码
作者: WHY 时间: 2021-6-29 21:12
- @echo off
- PowerShell "dir *.i[0-9][0-9] | group {$_.BaseName} | forEach {'' + ++$i + ',' + ($_.Name.ToUpper() -replace '^(....)(....)(....)(.....)(.+)$', '$1-$2-$3-$4-$5') + ',,,,,,' + $_.Count; $_.Group | ren -NewName {$_.Name -replace '\.(...)$', '_$1.dgn'}}" > 1.csv
- pause
复制代码
作者: jave000 时间: 2021-6-30 09:51
回复 8# WHY
很完美,请问'_$1.dgn'表示什么?为什么有一个1
作者: jave000 时间: 2021-6-30 10:46
回复 2# idwma
hi,mdixv710va13lr0521.i??只是用来举例的,有很多类似文件
作者: idwma 时间: 2021-6-30 20:36
本帖最后由 idwma 于 2021-6-30 21:50 编辑
回复 10# jave000
尴尬...
改一下合并到一个单元格
@echo off&setlocal enabledelayedexpansion
set "第一列="
set "第二列="
set "第三列="
set "第四列="
set "第五列="
set "第六列="
set "第七列="
set "第八列="
set "第九列="
set "第十列="
set out=aaa.csv
for /f "delims=" %%i in ('dir/b *.i?? ^| findstr "^.*i[0-9][0-9].*"') do (
if not !文件名! == %%~ni set/a "第一列+=1"
set a=%%~ni_%%~xi
set a=!a:.=!
ren %%~i !a!.dgn >nul
set "第二列=%%~ni"
set 第二列=!第二列:~0,4!-!第二列:~4,4!-!第二列:~8,4!-!第二列:~12,5!-!第二列:~17!
for %%m in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do call set 第二列=%%第二列:%%m=%%m%%
if not !文件名! == %%~ni (
for /f "delims=" %%z in (%%~ni.prt) do (set/a 行数一+=1)
for /f "delims=" %%l in ('dir/b %%~ni.i?? ^| findstr "^.*i[0-9][0-9].*"') do (set/a "第八列+=1")
for /f "delims=" %%j in (%%~ni.prt) do (
set/a "行数二+=1"
set "当前行=%%j"
if "!当前行:~0,12!" == " PIPING SPEC" set "第六列=!当前行:~13!"
if "!当前行:~0,24!" == " THERMAL INSULATION SPEC" if "!当前行:~25,1!" == "" (set "第七列=-") else (set "第七列=!当前行:~25,1!")
if "!当前行:~0,14!" == " PIPE NPD (MM)" set "第五列=!第五列!\!当前行:~16!"
if "!当前行:~0,14!" == " CL LENGTH (M)" set "第十列=!第十列! !当前行:~16!" & for /f "tokens=1-10 delims= " %%a in ("!第十列!") do set 第十列求和==SUM(%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,^)
if "!行数一!" == "!行数二!" echo;"!第一列!","!第二列!","!第三列!","!第四列!","
if "!行数一!" == "!行数二!" for /f "tokens=1-10 delims=\" %%a in ("!第五列!") do (echo;%%a && echo;%%b && echo;%%c && echo;%%d)
if "!行数一!" == "!行数二!" echo;","!第六列!","!第七列!","!第八列!","!第九列!","!第十列!","!第十列求和!"
if "!行数一!" == "!行数二!" set 行数一= & set 行数二= & set 第五列= & set 第十列=
)
set 第八列=
)
set 文件名=%%~ni
)>>!out!
pause
作者: jave000 时间: 2021-7-1 09:06
回复 11# idwma
谢谢,功能很棒,只是有些识别错误和输出错误,得等我上传一些样本再说了。先这样,我学习一下。
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |