返回列表 发帖

[文件操作] [已解决]如何根据文本条件判断,进行指定重命名?

本帖最后由 czvde 于 2022-3-24 17:25 编辑

例子
文件夹中有N个文件:
dbcd123.mp4
……
znn109.jpg

调用a.txt的数据进行添加
dbcd,1655一二
dfcdf99,012345DDD
znn1,DDD0123

bat运行效果:
视频1655一二__123--(720x480)——dbcd123.mp4
……
图片DDD0123__123--(1400x1200)——znn19.jpg

已解决!!

本帖最后由 idwma 于 2022-3-20 17:02 编辑
#@&cls&powershell "type %~s0|out-string|iex"&pause&exit
$a=gc a.txt
$shell=(new-object -com Shell.Application).namespace("$pwd")
$a|%{
$n=$_ -split ','
$shell.items()|?{$_.name -eq $(gi "$($n[0])*").name}|{
$wh=if($shell.GetDetailsOf($_, 31) -ne ''){
$shell.GetDetailsOf($_, 31) -split 'x' -replace '[^\d]'
$type='视频'
}elseif($shell.GetDetailsOf($_, 285) -ne ''){
$shell.GetDetailsOf($_, 285)
$shell.GetDetailsOf($_, 283)
$type='图片'
}
if($wh -ne $null){
$nn='{0:d6}' -f [int]$($_.name -replace "$n[0]" -split '.')[0]
ren $_.name $('{0}{1}__{2}--({3}x{4})--{5}' -f $type,$n[1],$nn,$wh[0],$wh[1],$_.name)
rv wh
}
}
}COPY
1

评分人数

    • czvde: 乐于助人技术 + 1

TOP

回复 2# idwma


    无法重命名,出现以下提示
Invoke-Expression : 只允许将表达式作为管道的第一个元素。
所在位置 行:1 字符: 36
+ type d:\测试\a.bat|out-string|iex <<<<
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseExcep
   tion
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline,Microsoft.Power
   Shell.Commands.InvokeExpressionCommand
请按任意键继续. . .COPY

TOP

回复 3# czvde


    第6行最后的{前面少了个%加上去
$shell.items()|?{$_.name -eq $(gi "$($n[0])*").name}|%{COPY

TOP

回复 4# idwma


    现在能运行
请按任意键继续. . .COPY
但运行批处理之后,所有文件名都没有变(还是原文件名,没有进行重命名)

TOP

回复 5# czvde


    再多试几次

TOP

回复 6# idwma


    多次运行批处理,还是没有(重命名)反应

TOP

回复 7# czvde

没有环境测试,可能代码不符合你的情况吧

TOP

本帖最后由 5i365 于 2022-3-20 20:34 编辑

回复 8# idwma


   大侠好, 我找了两段代码
图像分辨率获取
add-type -AssemblyName System.Drawing
$png = New-Object System.Drawing.Bitmap 'C:\365.png'
$png.Height
$png.Width



视频分辨率获取, 要下载个文件https://wss1.cn/f/7vlul0vhtt9 复制链接到浏览器打开
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 test.mp4
结果是csv格式的
1920,1080
--------------------------
另一种格式
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of default=nw=1 test.mp4

结果是这样的
width=1920
height=1080
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

test.bat
ansi编码,注意备份
#&cls&@cd /d "%~dp0" & powershell -c "Get-Content '%~0' | Out-String | Invoke-Expression" & pause&exit
$shell = New-Object -ComObject 'Shell.Application'
$folder = $shell.NameSpace($PWD.Path)
#打开txt
$txts = Get-Content 'a.txt'
#找索引
$i = $index1 = $index2 = $index3 = $index4 = 0
while(!($index1 -and $index2 -and $index3 -and $index4)){
if($folder.GetDetailsOf($null,$i) -eq '高度'){
$index1 = $i
}
elseif($folder.GetDetailsOf($null,$i) -eq '宽度'){
$index2 = $i
}
elseif($folder.GetDetailsOf($null,$i) -eq '帧高度'){
$index3 = $i
}
elseif($folder.GetDetailsOf($null,$i) -eq '帧宽度'){
$index4 = $i
}
++$i
}
#重命名
$folder.Items() | foreach {
$__ = $_
$t = $name = $null
if($_.Name -match '^(.*?)\d+\.((png)|(jpg))$'){
$t = '图片'; $name = $Matches[1]; $a = $index1; $b = $index2
}
elseif ($_.Name -match '^(.*?)\d+\.mp4$'){
$t = '视频'; $name = $Matches[1]; $a = $index3; $b = $index4
}
if($t -ne $null -and $a -match '\d'){
$b_find = $true
$txts | foreach{
if($b_find -and ($_ -match ('^{0}\d*,(.*?)$' -f $name)) ){
$h = $folder.GetDetailsOf($__,$a) -replace '[^\d]',''
$w = $folder.GetDetailsOf($__,$b) -replace '[^\d]',''
$new_name = '{0}{1}__123--({2}x{3})——{4}' -f $t,$Matches[1],$h,$w,$__.Name
$__.Name
$new_name
Rename-Item $__.Path $new_name
$b_find = $false
'---------------------------'
}
}
}
}COPY
1

评分人数

    • czvde: 乐于助人技术 + 1

TOP

回复 10# went
请按任意键继续. . .COPY
出现以下情况:
运行bat之后,所有文件名都没有变

TOP

回复 11# czvde


    图片,视频,a.txt,bat文件
    都放在一个文件夹里运行

TOP

本帖最后由 czvde 于 2022-3-22 21:32 编辑

回复 12# went


    都是放在同一个文件夹中测试,就是没有效果

TOP

回复 13# czvde


    测试正常,你只有想其他办法了

TOP

本帖最后由 czvde 于 2022-3-23 23:52 编辑

回复 14# went


    可能是我这边系统问题吧

TOP

返回列表