标题: 20求助批量更改文件名 [打印本页]
作者: chong08 时间: 2019-12-17 14:19 标题: 20求助批量更改文件名
具体报酬:20元
支付方式:支付宝
联系方式:QQ22210509
有效期限:无
需求描述:
(1)系统环境windows
(2)想要实现的功能的具体描述:做个bat文件。
将文件夹下包括子文件夹的tif文件名识别提取字符更名。
最终文件名为{名称}{序号}{当前日期}{色别}.tif
其中名称和序号留空行,方便我加入其他样本。
再次感谢。
源文件名:
16_13_光明日报-a2-a3_v00_1200_61240_Black.tif
16_13_光明日报-a2-a3_v00_1200_61240_Cyan.tif
16_13_光明日报-a2-a3_v00_1200_61240_Magenta.tif
16_13_光明日报-a2-a3_v00_1200_61240_Yellow.tif
16_13_光明日报-a10-a11_v00_1200_61240_Black.tif
16_13_光明日报-a10-a11_v00_1200_61240_Cyan.tif
16_13_光明日报-a10-a11_v00_1200_61240_Magenta.tif
16_13_光明日报-a10-a11_v00_1200_61240_Yellow.tif
18_15_第一财经-9-12_v00_1200_60588_Black.tif
18_15_第一财经-9-12_v00_1200_60588_Cyan.tif
18_15_第一财经-9-12_v00_1200_60588_Magenta.tif
18_15_第一财经-9-12_v00_1200_60588_Yellow.tif
18_15_第一财经-13-16_v00_1200_60588_Black.tif
18_15_第一财经-13-16_v00_1200_60588_Cyan.tif
18_15_第一财经-13-16_v00_1200_60588_Magenta.tif
18_15_第一财经-13-16_v00_1200_60588_Yellow.tif
修改后文件名:
gmrb-002-20191215-c.tif
gmrb-002-20191215-k.tif
gmrb-002-20191215-m.tif
gmrb-002-20191215-y.tif
gmrb-010-20191215-c.tif
gmrb-010-20191215-k.tif
gmrb-010-20191215-m.tif
gmrb-010-20191215-y.tif
dycj-009-20191215-c.tif
dycj-009-20191215-k.tif
dycj-009-20191215-m.tif
dycj-009-20191215-y.tif
dycj-013-20191215-c.tif
dycj-013-20191215-k.tif
dycj-013-20191215-m.tif
dycj-013-20191215-y.tif
作者: a20150604 时间: 2019-12-17 15:28
本帖最后由 a20150604 于 2019-12-17 15:36 编辑
回复 1# chong08 - Rem^ & @echo off & mode 300,9999
- Rem^ &cscript //NoLogo //E:VBScript "%~f0"
- Rem^ &pause
- Rem^ &goto :eof
-
- Public yyyyMMdd
- Call main
- Sub main()
- dttm = Now
- yyyyMMdd = Year(dttm) & "" & Right("0" & Month(dttm), 2) & "" & Right("0" & Day(dttm), 2)
- Set fso = CreateObject("Scripting.FileSystemObject")
- ' 脚本对象 和 当前目录
- Set obj_wsc_shell = CreateObject("wscript.shell")
- HostFolder = obj_wsc_shell.CurrentDirectory & "\"
- ' HostFolder = ThisWorkbook.Path & "\"
- Call DoFolder(fso.GetFolder(HostFolder))
- wscript.Quit
- End Sub
- Sub DoFolder(Folder)
- ' 在子目录中递归调用
- Dim SubFolder
- For Each SubFolder In Folder.SubFolders
- Call DoFolder(SubFolder)
- Next
- Dim file
- For Each file In Folder.Files
- Call doFile(file)
- Next
- End Sub
- Sub doFile(ByRef input_file)
- ' 分解获取 文件名 和 扩展名
- If InStr(input_file.Name, ".") > 0 Then
- arr = Split(input_file.Name, ".")
- ext_name = UCase(arr(UBound(arr)))
- dot_ext_name = "." & ext_name
- Else
- ext_name = ""
- dot_ext_name = ""
- End If
- ' 跳过 非 TIF 文件
- If Not ("TIF" = ext_name) Then
- Exit Sub
- End If
-
- file_name = Left(input_file.Name, Len(input_file.Name) - Len(dot_ext_name))
-
- Dim regEx
- Set regEx = New RegExp
- regEx.Pattern = "\d+_\d+_([^\-\s]+)-[^\d]*(\d+)[\s\S]*(Yellow|Black|Cyan|Magenta)"
- regEx.IgnoreCase = True
- Rem regEx.Global = True
- wscript.echo "file_name:" & file_name
- Set oMatches = regEx.Execute(file_name)
- If oMatches.Count > 0 Then
- Set oMatch = oMatches(0)
- wscript.echo "file_name:" & file_name & " 0:" & oMatch.SubMatches(0) & " 1:" & oMatch.SubMatches(1) & " 2:" & oMatch.SubMatches(2)
- new_name = ""
- Select Case oMatch.SubMatches(0)
- Case "光明日报"
- new_name = new_name & "gmrb" & "-"
- Case "第一财经"
- new_name = new_name & "dycj" & "-"
- End Select
- new_name = new_name & Right("00" & oMatch.SubMatches(1), 3) & "-" & yyyyMMdd
- Select Case UCase(oMatch.SubMatches(2))
- Case "BLACK"
- new_name = new_name & "-" & "k"
- Case "CYAN"
- new_name = new_name & "-" & "c"
- Case "MAGENTA"
- new_name = new_name & "-" & "m"
- Case "YELLOW"
- new_name = new_name & "-" & "y"
- End Select
- wscript.echo new_name
- input_file.Name = new_name & dot_ext_name
- Else
- wscript.echo input_file.Path & " 解析失败"
- End If
- End Sub
复制代码
作者: zaqmlp 时间: 2019-12-17 16:01
- <# :
- cls
- @echo off
- set info=互助互利,支付宝扫码头像,感谢赞助
- rem 有问题,可加QQ956535081及时沟通
- title %info%
- set "rootpath=%~dp0"
- if "%rootpath:~-1%" equ "\" (set "rootpath=%rootpath:~,-1%")
- cd /d "%rootpath%"
- powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%rootpath%'"
- echo;%info%
- pause
- exit
- #>
-
- $name='';
- $number='';
-
- $web=New-Object System.Net.WebClient;
- $web.Encoding=[Text.Encoding]::UTF8;
- $html=$web.DownloadString('http://csstools.chinaz.com/tools/js/pinyin.js');
- $m=[regex]::match($html, 'var pydic ?= ?"([^\"]+)"');
- $pydic='';
- if($m.success){
- $pydic=$m.groups[1].value;
- }else{
- write-host 'dict failed';
- exit;
- };
-
- $date=get-date;
- $fs=@(dir -liter $args[0] -recurse|?{('.tif' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
- for($i=0;$i -lt $fs.length;$i++){
- $m=[regex]::match($fs[$i].BaseName,'^[^_-]+_[^_-]+_([^_-]+)-([^_-]+)-[^_-]+(?:_[^_-]+){3}_([^_-]+)$');
- if($m.Success){
- $oldname=$fs[$i].Name;
- $a='';
- $fname=$m.groups[1].value;
- for($j=0;$j -lt $fname.length;$j++){
- $n=$pydic.indexOf($fname[$j]);
- if($fname[$j] -match '[\u4e00-\u9fa5]'){
- if($n -ge 0){
- $t=$pydic.Substring($n+1,1);
- $t=($t -replace '[āáǎà]','a') -replace '[ōóǒò]','o';
- $t=($t -replace '[ēéěè]','e');
- $a+=$t;
- };
- }else{$a+=$fname[$j]};
- };
- $b=($m.groups[2].value -replace '^\D*','').PadLeft(3,'0');
- $c=$date.toString('yyyyMMdd');
- $d=$m.groups[3].value.Substring(0,1);
- $newname=$name+$a+'-'+$number+$b+'-'+$c+'-'+$d.toLower()+$fs[$i].Extension;
- if($newname -ne $oldname){
- write-host ($fs[$i].FullName+' --> '+$newname);
- mv -liter $fs[$i].FullName ($fs[$i].Directory.FullName+'\'+$newname);
- };
- };
- }
复制代码
作者: chong08 时间: 2019-12-17 18:20
已解决,谢谢二楼
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |