一段批处理脚本(for 嵌套)
需求:
1、服务器上有一堆按日期生成的目录,已经有N个月了,需要只取当前月份的目录。
2、目录中有一系列文件,文件名字不一样,但存在一定的重复规律。
3、需要从服务器上拷贝文件到本地,自动去重,拷贝到本地。
具体如下:
R:\>dir 驱动器 R 中的卷是 新加卷 卷的序列号是 3E8F-427A R:\ 的目录 2017-10-27 08:46 <DIR> . 2017-10-27 08:46 <DIR> .. 2017-07-24 11:52 <DIR> 20170515 2017-06-07 08:35 <DIR> 20170523 2017-07-27 09:42 <DIR> 20170526 2017-09-20 10:52 <DIR> 20170531 2017-07-24 09:04 <DIR> 20170601 2017-07-24 09:04 <DIR> 20170602 2017-07-27 09:42 <DIR> 20170605 2017-09-06 10:06 <DIR> 20170606 2017-07-27 09:42 <DIR> 20170607 2017-08-01 17:32 <DIR> 20170608 2017-07-27 09:42 <DIR> 20170609 2017-07-27 09:42 <DIR> 20170612 2017-08-31 13:23 <DIR> 20170613 2017-07-24 10:29 <DIR> 20170614 2017-08-04 10:15 <DIR> 20170615 2017-07-27 09:42 <DIR> 20170616 2017-08-04 11:10 <DIR> 20170618 2017-07-27 09:42 <DIR> 20170619 ... R:\>dir 20170619\INV*.jpg
驱动器 R 中的卷是 新加卷
卷的序列号是 3E8F-427A
R:\20170619 的目录
2017-06-19 11:07 159,232 INV_00083127_18_5000005628_104616884_730902_001_145453.jpg
2017-06-19 11:07 159,232 INV_00083127_18_5000005628_104616884_730903_001_145451.jpg
2017-06-19 13:55 313,689 INV_00160210_1_2052273001_000390931_689609_001_135633.jpg
2017-06-19 14:22 295,899 INV_00280566_1_2033012003_104611169_724697_001_144845.jpg
2017-06-19 14:22 302,428 INV_00280635_1_2033012003_104618040_729760_001_144831.jpg
2017-06-19 14:22 300,046 INV_00280779_1_2033012003_3160060115_730413_001_144815.jpg
2017-06-19 14:22 293,478 INV_00280780_1_2033012003_104608132_721592_001_144722.jpg
2017-06-19 11:06 154,666 INV_00465220_18_0000143436_104602580_730772_001_145518.jpg
2017-06-19 14:22 307,641 INV_00470110_1_2251011001_3170241511_730220_001_145329.jpg
2017-06-19 14:22 302,693 INV_00470111_1_2251011001_3170241521_729798_001_145315.jpg
2017-06-19 14:22 309,057 INV_00470113_1_2251011001_104620685_730084_001_145344.jpg
2017-06-19 14:22 303,410 INV_00470148_1_2251011001_3070121522_729778_001_145301.jpg
2017-06-19 14:22 303,410 INV_00470148_1_2251011001_3070121522_730221_001_145301.jpg
2017-06-19 14:22 301,973 INV_00470167_1_2251011001_3090041511_714462_001_145039.jpg
2017-06-19 14:22 304,724 INV_00470175_1_2251011001_4110141911_730103_001_145242.jpg
2017-06-19 14:18 326,760 INV_00479189_1_2011012030_104607136_729135_001_142610.jpg
2017-06-19 14:18 326,615 INV_00535438_1_5000001381_3192094314_716531_001_142416.jpg
2017-06-19 14:18 325,687 INV_00542079_1_2211012050_104617955_729006_001_142444.jpg ...
可以看到,按日期生成了目录。以20170619目录下的文件举例,前两个文件其实大小都一样,是重复的。判断规则就是红色部分重名。
查找百度并反复测试,最后实现脚本如下:
1 @echo off&setlocal enabledelayedexpansion 2 for /d %%s in (r:\201710*) do ( 3 rem echo s1%%s\inv*.jpg 4 for /f "delims==" %%i in ('dir %%s\inv*.jpg /b') do ( 5 rem echo i1%%i 6 for /f "tokens=3 delims=_ " %%j in ("%%i") do ( 7 if exist D:\invall\%%j.jpg ( 8 echo "pass copy file "%%s\%%i 9 ) else ( 10 echo "copy file "%%s\%%i 11 copy %%s\%%i D:\invall\%%j.jpg >nul 12 rem pause 13 ) 14 ) 15 ) 16 ) 17 goto :end 18 :end 19 pause
总结:
1、for循环中,变量只能1个字母,%%s是ok的,%%subdir不ok。
2、for /f "delims==" %%i in ('dir %%s\inv*.jpg /b') %%i 前不能有变量。
3、do () ,do后面必须有一个空格。
4、setlocal enabledelayedexpansion 加扩展延迟。
另一种写法:
1 @echo off 2 setlocal enabledelayedexpansion 3 for /d %%i in (r:\201710*) do ( 4 call :subdir %%i 5 ) 6 7 :subdir 8 echo subdir %1% 9 for /r "%1" %%b in (inv*.jpg) do ( 10 echo %%b 11 call :substr %%b 12 ) 13 :substr 14 for /f "tokens=3 delims=_" %%j in ("%1%") do ( 15 echo substrfor %%j 16 copy %1 D:\invall\%%j.jpg 17 ) 18 goto :eof 19 :eof 20 pause
以上……