[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 请教各位大神如何缩编文件分类的批处理代码呢?

下面是写好的代码,已经可以分了,但是还有很多文件夹都是类似的命名规则,想请问下要如何缩减代码呢?
  1. cd /d "%~dp0"\H104
  2. for /f "delims=" %%a in ('dir /a-d /b "*D8050*" 2^>nul') do (
  3.     if not exist "D8050" md "D8050-D8051"
  4.     move "%%a" "D8050-D8051"
  5. )
  6. for /f "delims=" %%a in ('dir /a-d /b "*D8051*" 2^>nul') do (
  7.     if not exist "D8051" md "D8050-D8051"
  8.     move "%%a" "D8050-D8051"
  9. )
  10. for /f "delims=" %%a in ('dir /a-d /b "*D4060*" 2^>nul') do (
  11.     if not exist "D4060" md "D4060"
  12.     move "%%a" "D4060"
  13. )
  14. for /f "delims=" %%a in ('dir /a-d /b "*D4060*" 2^>nul') do (
  15.     if not exist "D4060" md "D4060"
  16.     move "%%a" "D4060"
  17. )
  18. for /f "delims=" %%a in ('dir /a-d /b "*D9061--36_x24*" 2^>nul') do (
  19.     if not exist "D9061--36_x24" md "D9061-36x24"
  20.     move "%%a" "D9061-36x24"
  21. )
  22. for /f "delims=" %%a in ('dir /a-d /b "*D9061--72_x48*" 2^>nul') do (
  23.     if not exist "D9061--72_x48" md "D9061-72x48"
  24.     move "%%a" "D9061-72x48"
  25. )
  26. for /f "delims=" %%a in ('dir /a-d /b "*D1612*" 2^>nul') do (
  27.     if not exist "D1612" md "D1612"
  28.     move "%%a" "D1612"
  29. )
  30. for /f "delims=" %%a in ('dir /a-d /b "*D4060*" 2^>nul') do (
  31.     if not exist "D4060" md "D4060"
  32.     move "%%a" "D4060"
  33. )
  34. for /f "delims=" %%a in ('dir /a-d /b "*D6190*" 2^>nul') do (
  35.     if not exist "D6190" md "D6190"
  36.     move "%%a" "D6190"
  37. )
  38. for /f "delims=" %%a in ('dir /a-d /b "*D5050*" 2^>nul') do (
  39.     if not exist "D5050" md "D5050"
  40.     move "%%a" "D5050"
  41. )
  42. for /f "delims=" %%a in ('dir /a-d /b "*D5040*" 2^>nul') do (
  43.     if not exist "D5040" md "D5040"
  44.     move "%%a" "D5040"
  45. )
  46. for /f "delims=" %%a in ('dir /a-d /b "*D5060*" 2^>nul') do (
  47.     if not exist "D5060" md "D5060"
  48.     move "%%a" "D5060"
  49. )
  50. cd /d "%~dp0"\H164
  51. for /f "delims=" %%a in ('dir /a-d /b "*ZY1370*" 2^>nul') do (
  52.     if not exist "ZY1370" md "ZY1370-ZY003-S"
  53.     move "%%a" "ZY1370-ZY003-S"
  54. )
  55. for /f "delims=" %%a in ('dir /a-d /b "*ZY003-S*" 2^>nul') do (
  56.     if not exist "ZY003-S" md "ZY1370-ZY003-S"
  57.     move "%%a" "ZY1370-ZY003-S"
  58. )
  59. for /f "delims=" %%a in ('dir /a-d /b "*ZY003-M*" 2^>nul') do (
  60.     if not exist "ZY003-M" md "ZY003-M"
  61.     move "%%a" "ZY003-M"
  62. )
  63. cd /d "%~dp0"\H174
  64. for /f "delims=" %%a in ('dir /a-d /b "*MMT01--50_x40*" 2^>nul') do (
  65.     if not exist "MMT01--50_x40" md "MMT01-50x40"
  66.     move "%%a" "MMT01-50x40"
  67. )
  68. for /f "delims=" %%a in ('dir /a-d /b "*MMT01--60_x50*" 2^>nul') do (
  69.     if not exist "MMT01--60_x50" md "MMT01-60x50"
  70.     move "%%a" "MMT01-60x50"
  71. )
  72. for /f "delims=" %%a in ('dir /a-d /b "*MMT01--80_x60*" 2^>nul') do (
  73.     if not exist "MMT01--80_x60" md "MMT01-80x60"
  74.     move "%%a" "MMT01-80x60"
  75. )
  76. for /f "delims=" %%a in ('dir /a-d /b "*MT002--50_x40*" 2^>nul') do (
  77.     if not exist "MT002--50_x40" md "MT002-50x40"
  78.     move "%%a" "MT002-50x40"
  79. )
  80. for /f "delims=" %%a in ('dir /a-d /b "*MT002--60_x50*" 2^>nul') do (
  81.     if not exist "MT002--60_x50" md "MT002-60x50"
  82.     move "%%a" "MT002-60x50"
  83. )
  84. for /f "delims=" %%a in ('dir /a-d /b "*MT002--80_x60*" 2^>nul') do (
  85.     if not exist "MT002--80_x60" md "MT002-80x60"
  86.     move "%%a" "MT002-80x60"
  87. )
  88. for /f "delims=" %%a in ('dir /a-d /b "*YG003-50_x40*" 2^>nul') do (
  89.     if not exist "YG003-50_x40" md "YG003-50x40"
  90.     move "%%a" "YG003-50x40"
  91. )
  92. for /f "delims=" %%a in ('dir /a-d /b "*YG003-60_x50*" 2^>nul') do (
  93.     if not exist "YG003-60_x50" md "YG003-60x50"
  94.     move "%%a" "YG003-60x50"
  95. )
  96. for /f "delims=" %%a in ('dir /a-d /b "*YG003-80_x60*" 2^>nul') do (
  97.     if not exist "YG003-80_x60" md "YG003-80x60"
  98.     move "%%a" "YG003-80x60"
  99. )
  100. for /f "delims=" %%a in ('dir /a-d /b "*MT3040*" 2^>nul') do (
  101.     if not exist "MT3040" md "MT3040"
  102.     move "%%a" "MT3040"
  103. )
  104. for /f "delims=" %%a in ('dir /a-d /b "*HT001-50_x40*" 2^>nul') do (
  105.     if not exist "HT001-50_x40" md "HT001-50x40"
  106.     move "%%a" "HT001-50x40"
  107. )
  108. for /f "delims=" %%a in ('dir /a-d /b "*HT001-60_x50*" 2^>nul') do (
  109.     if not exist "HT001-60_x50" md "HT001-60x50"
  110.     move "%%a" "HT001-60x50"
  111. )
  112. for /f "delims=" %%a in ('dir /a-d /b "*HT001-80x60*" 2^>nul') do (
  113.     if not exist "HT001-80_x60" md "HT001-80x60"
  114.     move "%%a" "HT001-80x60"
  115. )
  116. for /f "delims=" %%a in ('dir /a-d /b "*HT001-40_x30*" 2^>nul') do (
  117.     if not exist "HT001-40_x30" md "HT001-40x30"
  118.     move "%%a" "HT001-40x30"
  119. )
  120. for /f "delims=" %%a in ('dir /a-d /b "*HT001-80_x60*" 2^>nul') do (
  121.     if not exist "HT001-80_x60" md "HT001-80x60"
  122.     move "%%a" "HT001-80x60"
  123. )
  124. for /f "delims=" %%a in ('dir /a-d /b "*S4321*" 2^>nul') do (
  125.     if not exist "S4321" md "S4321"
  126.     move "%%a" "S4321"
  127. )
  128. for /f "delims=" %%a in ('dir /a-d /b "*S5020*" 2^>nul') do (
  129.     if not exist "S5020" md "S5020"
  130.     move "%%a" "S5020"
  131. )
复制代码
以下是各个文件夹的分类与名称规则,请大神赐教,万分感谢!!!!!
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 08:20 编辑

由于所提的要求规律性不强,下面代码和你的要求存在细微差异(文件夹名两个横杠只能用一个横杠代替;实现合并文件与精简代码矛盾,又会产生一堆代码):
未经测试
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /d %%x in (*) do (
  4.     pushd %%x
  5.     for %%i in (*) do (
  6.         set str=%%i
  7.         if "!str!"=="!str:_x=!" (
  8.             for /f "tokens=4 delims=-" %%a in ("%%i") do (
  9.                 if not exist "%%a" md "%%a"
  10.                 move "%%i" "%%a"
  11.             )
  12.         ) else (
  13.             for /f "tokens=4,5 delims=-" %%a in ("%%i") do (
  14.                 set y=%%b
  15.                 if not exist "%%a-!y:~,-1!" md "%%a-!y:~,-1!"
  16.                 move "%%i" "%%a-!y:~,-1!"
  17.             )
  18.         )
  19.     )
  20. popd
  21. )
  22. pause
复制代码

TOP

本帖最后由 feixiang8209 于 2021-4-6 08:52 编辑

回复 2# qixiaobin0715


   不行 HT001-40x30  YG003-50_x40像这样的文件名分出来把所有以这样命名的图片格式都改了,还有D8050与D8051合并成一个文件夹的功能也实现不了,烦请大神帮忙改一下,谢谢

TOP

回复 3# feixiang8209
问题出在你自身描述不明 含糊不清 别让大家猜你想做什么 往往重写脚本比看你的脚本来揣测意图来的快 把问题需求写具体 能尽快解决

TOP

严重同意!!!

TOP

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /d %%x in (*) do (
  4.     pushd %%x
  5.     for %%i in (*) do (
  6.         set str=%%i
  7.         if "!str!"=="!str:_x=!" (
  8.             for /f "tokens=4 delims=-" %%a in ("%%i") do (
  9.                 if not exist "%%a" md "%%a"
  10.                 move "%%i" "%%a"
  11.             )
  12.         ) else (
  13.             for /f "tokens=4,5,6 delims=-" %%a in ("%%i") do (
  14.                 echo,%%a|findstr /v /be [0-9][0-9]*>nul&&(set "str1=%%a"&set "str2=%%b")||(set "str1=%%b"&set "str2=%%c")
  15.                 if not exist "!str1!-!str2:~,-1!" md "!str1!-!str2:~,-1!"
  16.                 move "%%i" "!str1!-!str2:~,-1!"
  17.             )
  18.         )
  19.     )
  20. popd
  21. )
  22. pause
复制代码
回复 3# feixiang8209

TOP

回复 6# qixiaobin0715


    谢谢大神,现在就差一个文件夹合并的功能了,非常感谢!!!

TOP

回复 7# feixiang8209
你所说的文件夹合并,没看出什么规律来,写代码需要规则的。像这种情况手动合并即可。

TOP

回复 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两个文件夹

TOP

本帖最后由 qixiaobin0715 于 2021-4-6 14:49 编辑

回复 9# feixiang8209
思路都给你搞乱了,就这样了,应当还能精简,按正常思路顺序写的,凑合用吧。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /d %%i in (*) do (
  4.     pushd %%i
  5.     for %%j in (*) do (
  6.         for /f "tokens=4,5,6,7 delims=-" %%a in ("%%j") do (
  7.             set "x=%%d"&set "x=!x:~-3!"
  8.             set "x1=%%a"&set "x2=%%b"&set "x3=%%c"
  9.             if "!x!"=="DPI" (
  10.                 if "!x3!"=="OneSize" (
  11.                     if not exist "!x2!" md "!x2!"
  12.                     move "%%j" "!x2!"
  13.                 ) else if "!x3:~-4!"=="inch" (
  14.                     if not exist "!x3!" md "!x3!"
  15.                     move "%%j" "!x3!"
  16.                 ) else (
  17.                     if not exist "!x2!-!x3!" md "!x2!-!x3!"
  18.                     move "%%j" "!x2!-!x3!"
  19.                 )
  20.             ) else (
  21.                 if "!x2!"=="OneSize" (
  22.                     if not exist "!x1!" md "!x1!"
  23.                     move "%%j" "!x1!"
  24.                 ) else if "!x2:~-4!"=="inch" (
  25.                     if not exist "!x2!" md "!x2!"
  26.                     move "%%j" "!x2!"
  27.                 ) else (
  28.                     if not exist "!x1!-!x2!" md "!x1!-!x2!"
  29.                     move "%%j" "!x1!-!x2!"
  30.                 )
  31.             )
  32.         )
  33.     )
  34. popd
  35. )
  36. pause
复制代码

TOP

精简后:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /d %%i in (*) do (
  4.     pushd %%i
  5.     for %%j in (*) do (
  6.         for /f "tokens=4,5,6,7 delims=-" %%a in ("%%j") do (
  7.             set "x=%%d"&set "x=!x:~-3!"
  8.             if "!x!"=="DPI" (
  9.                 set "x1=%%b"&set "x2=%%c"
  10.             ) else (
  11.                 set "x1=%%a"&set "x2=%%b"
  12.             )
  13.             if "!x2!"=="OneSize" (
  14.                 if not exist "!x1!" md "!x1!"
  15.                 move "%%j" "!x1!"
  16.             ) else if "!x2:~-4!"=="inch" (
  17.                 if not exist "!x2!" md "!x2!"
  18.                 move "%%j" "!x2!"
  19.             ) else (
  20.                 if not exist "!x1!-!x2!" md "!x1!-!x2!"
  21.                 move "%%j" "!x1!-!x2!"
  22.             )
  23.         )
  24.     )
  25. popd
  26. )
  27. pause
复制代码

TOP

RE: 请教各位大神如何缩编文件分类的批处理代码呢?

回复 11# qixiaobin0715


    谢谢大神,完美解决了。谢谢!!!

TOP

本帖最后由 xp3000 于 2021-4-6 23:28 编辑

需要字符串搜索工具grep,放入C:\Windows\System32这个目录
论坛下载地址:
http://bcn.bathome.net/s/tool/index.html?down&key=grep
  1. @echo off
  2. @for /f "tokens=*" %%a in ('dir /b/a-d/oN *.jpg') do (
  3.     @for /f "delims=*" %%b in ('echo %%a^|grep -Eo "D805[01]"') do (
  4.     if not exist "%%b" echo 创建"D8050-D8051" & md "D8050-D8051"&move "%%a" "D8050-D8051"
  5.     )
  6.     @for /f "delims=*" %%c in ('echo %%a^|grep -oE "ZY1370|ZY003-S"') do (
  7.     if not exist "%%c" echo 创建"ZY1370-ZY003-S" & md "ZY1370-ZY003-S"&move "%%a" "ZY1370-ZY003-S"
  8.     )
  9.     @for /f "delims=*" %%d in ('echo %%a^|grep -oE "[A-Z]+[0-9]+-?-[0-9]+_x[0-9]+"') do (
  10.     if not exist "%%d" echo 创建"%%d" & md "%%d"&move "%%a" "%%d"
  11.     )
  12.     @for /f "delims=*" %%e in ('echo %%a^|grep -Po "(?<=-[1-9]-|LT001)ZY003-M|[^-]+inch"') do (
  13.     if not exist "%%e" echo 创建"%%e" & md "%%e"&move "%%a" "%%e"
  14.     )
  15. )
  16. @for /f "tokens=*" %%i in ('dir /b/a-d/oN *.jpg') do (
  17.     @for /f "delims=*" %%j in ('echo %%i^|grep -Po "(?<=-[1-9]-)[A-Z]+[0-9]+"') do (
  18.     if not exist "%%j" echo 创建"%%j" & md "%%j"&move "%%i" "%%j"
  19.     )
  20. )
  21. pause
复制代码

TOP

刚开始运行慢,一直不知道怎么回事,后来退出360安全喂屎后秒解决
杀毒软件不退出没事,

TOP

本帖最后由 qixiaobin0715 于 2021-4-7 17:33 编辑

回复 12# feixiang8209
合并文件夹,代码就要增加不少。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /d %%i in (*) do (
  4.     pushd %%i
  5.     for %%j in (*) do (
  6.         for /f "tokens=4,5,6,7 delims=-" %%a in ("%%j") do (
  7.             set "x=%%d"&set "x=!x:~-3!"
  8.             if "!x!"=="DPI" (
  9.                 set "x1=%%b"&set "x2=%%c"
  10.             ) else (
  11.                 set "x1=%%a"&set "x2=%%b"
  12.             )
  13.             if "!x2!"=="OneSize" (
  14.                 if "!x1!"=="D8050" (
  15.                     if not exist "D8050-D8051" md "D8050-D8051"
  16.                     move "%%j" "D8050-D8051"
  17.                 ) else if "!x1!"=="D8051" (
  18.                     if not exist "D8050-D8051" md "D8050-D8051"
  19.                     move "%%j" "D8050-D8051"
  20.                 ) else if "!x1!"=="ZY1370" (
  21.                     if not exist "ZY1370-ZY003-S" md "ZY1370-ZY003-S"
  22.                     move "%%j" "ZY1370-ZY003-S"
  23.                 ) else (
  24.                     if not exist "!x1!" md "!x1!"
  25.                     move "%%j" "!x1!"
  26.                 )
  27.             ) else if "!x2:~-4!"=="inch" (
  28.                 if not exist "!x2!" md "!x2!"
  29.                 move "%%j" "!x2!"
  30.             ) else (
  31.                 if "!x1!-!x2!"=="ZY003-S" (
  32.                     if not exist "ZY1370-ZY003-S" md "ZY1370-ZY003-S"
  33.                     move "%%j" "ZY1370-ZY003-S"
  34.                 ) else (
  35.                     if "!x2:~-1!"=="_" set "x2=!x2:~0,-1!"
  36.                     if not exist "!x1!-!x2!" md "!x1!-!x2!"
  37.                     move "%%j" "!x1!-!x2!"
  38.                 )
  39.             )
  40.         )
  41.     )
  42. popd
  43. )
  44. pause
复制代码

TOP

返回列表