标题: [文件操作] 请教各位大神如何缩编文件分类的批处理代码呢? [打印本页]
作者: feixiang8209 时间: 2021-4-5 19:16 标题: 请教各位大神如何缩编文件分类的批处理代码呢?
下面是写好的代码,已经可以分了,但是还有很多文件夹都是类似的命名规则,想请问下要如何缩减代码呢?复制代码
以下是各个文件夹的分类与名称规则,请大神赐教,万分感谢!!!!!
H104文件夹分为:D8050与D8051合并、D9061--36_x24、D9061--72_x48、D1612、D4060、D6190、D5050、D5040、D50609个文件夹,文件名规则如下:
H1040001-UA3UKX-1-D8050--OneSize-90DPI-NO3-White-1-printArea-8568670.jpg
H1040003-EYT44Y-2-D9061--36_x24_-90DPI-NO1-White-1-printArea-8121215.jpg
H1040024-FJD6IK-1-D9061--72_x48_-90DPI-NO1-White-1-printArea-8373753.jpg
H1040027-ED3BSN-1-D1612--OneSize-90DPI-NO1-White-1-printArea-7856381.jpg
H1040047-7AO7KB-1-D4060-OneSize-90DPI-NO1-White-1-printArea-7866858.jpg
H1040072-UZNBVV-1-D6190-OneSize-90DPI-NO1-White-1-printArea-4601401.jpg
H1040077-PFT3UK-1-D8051-OneSize-90DPI-NO1-Black-1-printArea-6824873.jpg
H1040089-RNKNBY-1-D5050-OneSize-90DPI-NO1-Black-1-printArea-5968736.jpg
H1040117-4FAS7U-1-D5040-OneSize-90DPI-NO1-Black-1-printArea-8569865.jpg
H1040125-D5DXAT-1-D5060-OneSize-90DPI-NO1-Black-1-printArea-8573272.jpg
H144文件夹分为D2014、D6084两个文件夹,文件名规则如下:
H1440003-FEA9NL-1-D2014--OneSize-90DPI-NO1-White-1-printArea-8392841.jpg
H1440007-SGFRHR-1-D6084--OneSize-90DPI-NO1-Black-1-printArea-5723987.jpg
H164文件夹分为ZY1370与ZY003-S合并、ZY003-M两个文件夹
H1640008-E6PUSY-1-ZY1370-OneSize-90DPI-NO1-White-1-printArea-2428852.jpg
H1640059-ROULYY-1-ZY003-S-90DPI-NO1-White-1-printArea-8565020.jpg
H1640073-JF6TBD-1-ZY003-M-90DPI-NO1-White-1-printArea-8284474.jpg
H174文件夹分为MT002--50_x40、MMT01--50_x40、MMT01--60_x50、MMT01--80_x60、S4321、S5020、MT002--60_x50、MT002--80_x60、YG003-50_x40、YG003-60_x50、YG003-80_x60、MT3040、HT001-50_x40、HT001-60_x50、HT001-80_x60共15个文件夹,文件名称规则如下:
H1740003-P72JFB-4-MT002--50_x40_-90DPI-NO1-Black-1-printArea-7524221.jpg
H1740003-UEN42N-1-MMT01--50_x40_-90DPI-NO1-Black-1-printArea-8572489.jpg
H1740021-VUL38G-1-MMT01--60_x50_-90DPI-NO1-Black-1-printArea-6523533.jpg
H1740033-5EFMMG-1-MMT01--80_x60_-90DPI-NO1-Black-1-printArea-8570680.jpg
H1740034-O4HXFN-1-S4321--OneSize-100DPI-NO1-Black-1-printArea-5096708.jpg
H1740036-2X4B3H-1-S5020--OneSize-100DPI-NO1-Black-1-printArea-8045830.jpg
H1740062-GKKOBN-1-MT002--60_x50_-90DPI-NO1-Black-1-printArea-8557406.jpg
H1740190-LVF7DS-1-MT002--80_x60_-90DPI-NO1-Black-1-printArea-8570132.jpg
H1850002-ITTMFZ-1-1-YG003-50_x40_-125DPI-NO1-Black-1-printArea-8574558.jpg
H1850004-WGZ924-1-YG003-60_x50_-115DPI-NO1-Black-1-printArea-8575769.jpg
H1850012-FP8TMC-1-YG003-80_x60_-120DPI-NO1-Black-1-printArea-8560097.jpg
H1850021-YPVZLG-2-MT3040-OneSize-90DPI-NO23-Black-1-printArea-7823220.jpg
H2310002-M7D5QT-1-HT001-50_x40_-90DPI-NO1-Black-1-printArea-8567431.jpg
H2310005-X2AQ6H-1-1-HT001-60_x50_-90DPI-NO1-Black-1-printArea-8574980.jpg
H2310007-8ARAOY-1-HT001-80_x60_-90DPI-NO1-Black-1-printArea-8575963.jpg
作者: qixiaobin0715 时间: 2021-4-6 06:53
本帖最后由 qixiaobin0715 于 2021-4-6 08:20 编辑
由于所提的要求规律性不强,下面代码和你的要求存在细微差异(文件夹名两个横杠只能用一个横杠代替;实现合并文件与精简代码矛盾,又会产生一堆代码):
未经测试- @echo off
- setlocal enabledelayedexpansion
- for /d %%x in (*) do (
- pushd %%x
- for %%i in (*) do (
- set str=%%i
- if "!str!"=="!str:_x=!" (
- for /f "tokens=4 delims=-" %%a in ("%%i") do (
- if not exist "%%a" md "%%a"
- move "%%i" "%%a"
- )
- ) else (
- for /f "tokens=4,5 delims=-" %%a in ("%%i") do (
- set y=%%b
- if not exist "%%a-!y:~,-1!" md "%%a-!y:~,-1!"
- move "%%i" "%%a-!y:~,-1!"
- )
- )
- )
- popd
- )
- pause
复制代码
作者: feixiang8209 时间: 2021-4-6 08:48
本帖最后由 feixiang8209 于 2021-4-6 08:52 编辑
回复 2# qixiaobin0715
不行 HT001-40x30 YG003-50_x40像这样的文件名分出来把所有以这样命名的图片格式都改了,还有D8050与D8051合并成一个文件夹的功能也实现不了,烦请大神帮忙改一下,谢谢
作者: smss 时间: 2021-4-6 09:46
回复 3# feixiang8209
问题出在你自身描述不明 含糊不清 别让大家猜你想做什么 往往重写脚本比看你的脚本来揣测意图来的快 把问题需求写具体 能尽快解决
作者: qixiaobin0715 时间: 2021-4-6 10:33
严重同意!!!
作者: qixiaobin0715 时间: 2021-4-6 10:34
- @echo off
- setlocal enabledelayedexpansion
- for /d %%x in (*) do (
- pushd %%x
- for %%i in (*) do (
- set str=%%i
- if "!str!"=="!str:_x=!" (
- for /f "tokens=4 delims=-" %%a in ("%%i") do (
- if not exist "%%a" md "%%a"
- move "%%i" "%%a"
- )
- ) else (
- for /f "tokens=4,5,6 delims=-" %%a in ("%%i") do (
- echo,%%a|findstr /v /be [0-9][0-9]*>nul&&(set "str1=%%a"&set "str2=%%b")||(set "str1=%%b"&set "str2=%%c")
- if not exist "!str1!-!str2:~,-1!" md "!str1!-!str2:~,-1!"
- move "%%i" "!str1!-!str2:~,-1!"
- )
- )
- )
- popd
- )
- pause
复制代码
回复 3# feixiang8209
作者: feixiang8209 时间: 2021-4-6 12:28
回复 6# qixiaobin0715
谢谢大神,现在就差一个文件夹合并的功能了,非常感谢!!!
作者: qixiaobin0715 时间: 2021-4-6 12:41
回复 7# feixiang8209
你所说的文件夹合并,没看出什么规律来,写代码需要规则的。像这种情况手动合并即可。
作者: feixiang8209 时间: 2021-4-6 12:53
回复 8# qixiaobin0715
您好大神,能否把下面的这件名再加个分类规则进去?刚才试了就这几个分不了,谢谢大神了!
H1660001-RM9YLI-2-LT001-15inch-90DPI-NO1-White-1-printArea-8568072
H1660052-TJEHC5-5-LT001-14inch-90DPI-NO1-White-1-printArea-8564509
H1660080-RM9YLI-3-LT001-16inch-90DPI-NO1-White-1-printArea-8568071
H1660112-TJEHC5-4-LT001-17inch-90DPI-NO1-White-1-printArea-8564510
H1660162-HIO3PZ-1-ZY001--OneSize-90DPI-NO1-Black-1-printArea-8564409
还有这几个文件名格式的能否分成14inch、15inch、16inch、17inch、ZY001这样几个文件夹吗?
H1640059-ROULYY-1-ZY003-S-90DPI-NO1-White-1-printArea-8565020
H1640073-JF6TBD-1-ZY003-M-90DPI-NO1-White-1-printArea-8284474
下面这两个分成ZY003-S、ZY003-M两个文件夹
作者: qixiaobin0715 时间: 2021-4-6 14:46
本帖最后由 qixiaobin0715 于 2021-4-6 14:49 编辑
回复 9# feixiang8209
思路都给你搞乱了,就这样了,应当还能精简,按正常思路顺序写的,凑合用吧。- @echo off
- setlocal enabledelayedexpansion
- for /d %%i in (*) do (
- pushd %%i
- for %%j in (*) do (
- for /f "tokens=4,5,6,7 delims=-" %%a in ("%%j") do (
- set "x=%%d"&set "x=!x:~-3!"
- set "x1=%%a"&set "x2=%%b"&set "x3=%%c"
- if "!x!"=="DPI" (
- if "!x3!"=="OneSize" (
- if not exist "!x2!" md "!x2!"
- move "%%j" "!x2!"
- ) else if "!x3:~-4!"=="inch" (
- if not exist "!x3!" md "!x3!"
- move "%%j" "!x3!"
- ) else (
- if not exist "!x2!-!x3!" md "!x2!-!x3!"
- move "%%j" "!x2!-!x3!"
- )
- ) else (
- if "!x2!"=="OneSize" (
- if not exist "!x1!" md "!x1!"
- move "%%j" "!x1!"
- ) else if "!x2:~-4!"=="inch" (
- if not exist "!x2!" md "!x2!"
- move "%%j" "!x2!"
- ) else (
- if not exist "!x1!-!x2!" md "!x1!-!x2!"
- move "%%j" "!x1!-!x2!"
- )
- )
- )
- )
- popd
- )
- pause
复制代码
作者: qixiaobin0715 时间: 2021-4-6 15:06
精简后:- @echo off
- setlocal enabledelayedexpansion
- for /d %%i in (*) do (
- pushd %%i
- for %%j in (*) do (
- for /f "tokens=4,5,6,7 delims=-" %%a in ("%%j") do (
- set "x=%%d"&set "x=!x:~-3!"
- if "!x!"=="DPI" (
- set "x1=%%b"&set "x2=%%c"
- ) else (
- set "x1=%%a"&set "x2=%%b"
- )
- if "!x2!"=="OneSize" (
- if not exist "!x1!" md "!x1!"
- move "%%j" "!x1!"
- ) else if "!x2:~-4!"=="inch" (
- if not exist "!x2!" md "!x2!"
- move "%%j" "!x2!"
- ) else (
- if not exist "!x1!-!x2!" md "!x1!-!x2!"
- move "%%j" "!x1!-!x2!"
- )
- )
- )
- popd
- )
- pause
复制代码
作者: feixiang8209 时间: 2021-4-6 18:58 标题: RE: 请教各位大神如何缩编文件分类的批处理代码呢?
回复 11# qixiaobin0715
谢谢大神,完美解决了。谢谢!!!
作者: xp3000 时间: 2021-4-6 21:40
本帖最后由 xp3000 于 2021-4-6 23:28 编辑
需要字符串搜索工具grep,放入C:\Windows\System32这个目录
论坛下载地址:
http://bcn.bathome.net/s/tool/index.html?down&key=grep- @echo off
- @for /f "tokens=*" %%a in ('dir /b/a-d/oN *.jpg') do (
- @for /f "delims=*" %%b in ('echo %%a^|grep -Eo "D805[01]"') do (
- if not exist "%%b" echo 创建"D8050-D8051" & md "D8050-D8051"&move "%%a" "D8050-D8051"
- )
- @for /f "delims=*" %%c in ('echo %%a^|grep -oE "ZY1370|ZY003-S"') do (
- if not exist "%%c" echo 创建"ZY1370-ZY003-S" & md "ZY1370-ZY003-S"&move "%%a" "ZY1370-ZY003-S"
- )
- @for /f "delims=*" %%d in ('echo %%a^|grep -oE "[A-Z]+[0-9]+-?-[0-9]+_x[0-9]+"') do (
- if not exist "%%d" echo 创建"%%d" & md "%%d"&move "%%a" "%%d"
- )
- @for /f "delims=*" %%e in ('echo %%a^|grep -Po "(?<=-[1-9]-|LT001)ZY003-M|[^-]+inch"') do (
- if not exist "%%e" echo 创建"%%e" & md "%%e"&move "%%a" "%%e"
- )
- )
- @for /f "tokens=*" %%i in ('dir /b/a-d/oN *.jpg') do (
- @for /f "delims=*" %%j in ('echo %%i^|grep -Po "(?<=-[1-9]-)[A-Z]+[0-9]+"') do (
- if not exist "%%j" echo 创建"%%j" & md "%%j"&move "%%i" "%%j"
- )
- )
- pause
复制代码
作者: xp3000 时间: 2021-4-7 08:08
刚开始运行慢,一直不知道怎么回事,后来退出360安全喂屎后秒解决
杀毒软件不退出没事,
作者: qixiaobin0715 时间: 2021-4-7 11:19
本帖最后由 qixiaobin0715 于 2021-4-7 17:33 编辑
回复 12# feixiang8209
合并文件夹,代码就要增加不少。- @echo off
- setlocal enabledelayedexpansion
- for /d %%i in (*) do (
- pushd %%i
- for %%j in (*) do (
- for /f "tokens=4,5,6,7 delims=-" %%a in ("%%j") do (
- set "x=%%d"&set "x=!x:~-3!"
- if "!x!"=="DPI" (
- set "x1=%%b"&set "x2=%%c"
- ) else (
- set "x1=%%a"&set "x2=%%b"
- )
- if "!x2!"=="OneSize" (
- if "!x1!"=="D8050" (
- if not exist "D8050-D8051" md "D8050-D8051"
- move "%%j" "D8050-D8051"
- ) else if "!x1!"=="D8051" (
- if not exist "D8050-D8051" md "D8050-D8051"
- move "%%j" "D8050-D8051"
- ) else if "!x1!"=="ZY1370" (
- if not exist "ZY1370-ZY003-S" md "ZY1370-ZY003-S"
- move "%%j" "ZY1370-ZY003-S"
- ) else (
- if not exist "!x1!" md "!x1!"
- move "%%j" "!x1!"
- )
- ) else if "!x2:~-4!"=="inch" (
- if not exist "!x2!" md "!x2!"
- move "%%j" "!x2!"
- ) else (
- if "!x1!-!x2!"=="ZY003-S" (
- if not exist "ZY1370-ZY003-S" md "ZY1370-ZY003-S"
- move "%%j" "ZY1370-ZY003-S"
- ) else (
- if "!x2:~-1!"=="_" set "x2=!x2:~0,-1!"
- if not exist "!x1!-!x2!" md "!x1!-!x2!"
- move "%%j" "!x1!-!x2!"
- )
- )
- )
- )
- popd
- )
- pause
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |