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

最近空时间多,来凑个热闹,希望能拿个第三名吧!
所有代码在win7下测试通过。
有什么问题还望Batcher指点。
帖子代码的显示稍微窄了点儿,长一点的句子显示出来不整齐。
总共才300kb,结果附件不能大于50kb,不得不压缩成7个包... ...
1、
  1. @echo off
  2. call :rd ".\测试文件夹"&exit
  3. :rd
  4. for /f "delims=" %%a in ('dir/a-d/b %1 2^>nul') do if not "%%a"=="" del "%~1\%%a" /f
  5. for /f "delims=" %%a in ('dir/ad/b %1 2^>nul') do if not "%%a"=="" call :rd "%~1\%%a"
  6. rd %1 /q 2>nul
复制代码
2、
  1. @echo off
  2. for /f "skip=4 delims=" %%i in ('net user') do (
  3. if not "%%i"=="命令成功完成。" (
  4.     (for %%j in (%%i) do echo %%j)>User_List.txt
  5. ))
  6. (findstr /v /g:User_List.txt DBatHome_User_List.txt)>Gone_Users.txt
  7. (findstr /v /g:DBatHome_User_List.txt User_List.txt)>New_Users.txt
复制代码
3、
  1. @echo off
  2. set str=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%%^^^&*+
  3. setlocal enabledelayedexpansion
  4. for /l %%a in (1001,1,1100) do (
  5.     call :get_rnd
  6.     set password=%%a!rnd!
  7.     set username=%%a&set username=!username:~1!&set username=BatHome!username!
  8.     echo net user !username! !password! /add
  9. )
  10. pause&exit
  11. :get_rnd 生成四位带一个大写、一个小写字母、一个数字、一个特殊字符的不重复字符串
  12. set rnd=
  13. set /a l=!random!%%26,u=!random!%%26+26,n=!random!%%10+52,t=!random!%%10+62
  14. for %%a in (!l!,!u!,!n!,!t!) do (
  15.     set rnd=!rnd!!str:~%%a,1!
  16. )
  17. if not defined !rnd! (set !rnd!=yes
  18. ) else (call :get_rnd)
复制代码
4、
  1. @echo off&title DateToSecs函数 BY Batcher Else BY wankoilz
  2. set "len=for /f "tokens=1-2" %%1 in ("$") do (set ##=!%%1!&set %%2=&(for %%a in (4096,2048,1024,512,256,128,64,32,16,8,4,2,1) do (if not "!%%1:~%%a,1!"=="" (set/a %%2+=%%a&set %%1=!%%1:~%%a!)))&(if not "!%%1!"=="" set/a %%2+=1)&set %%1=!##!)"
  3. setlocal enabledelayedexpansion
  4. echo MemoryInfo:
  5. call :Show_MemoryInfo
  6. echo;&echo DiskInfo(Wait a moment):
  7. call :Show_DiskInfo
  8. echo;&echo SystemRunningTimeDiff:
  9. call :Show_SystemRunningTimeDiff
  10. echo;&pause&goto :eof
  11. :Show_MemoryInfo
  12. for %%a in (物理内存总量 可用的物理内存) do (
  13.     set /a n+=1
  14.     for /f "tokens=2" %%b in ('systeminfo ^| findstr "%%a"') do (
  15.         set mem!n!=%%b
  16.     )
  17. )
  18. set rest=!mem2:^,=!&set total=!mem1:^,=!
  19. set/a used=!total!-!rest!
  20. set used=!used!0000
  21. set/a percent=!used!/!total!
  22. set percent=0000!percent!
  23. set percent_int=!percent:~0,-2!&set percent_float=!percent:~-2!
  24. call :trim percent_int !percent_int!
  25. set percent=!percent_int!.!percent_float!%
  26. set total=!total!0000
  27. set/a total=!total!/1024
  28. set total=0000!total!
  29. set int_total=!total:~0,-4!&set float_total=!total:~-4!
  30. call :trim int_total !int_total!
  31. set total_GB=!int_total!.!float_total!
  32. echo     总内存:!total_GB!GB
  33. echo 内存使用率:!percent!
  34. goto :eof
  35. :Show_DiskInfo
  36. for /f "skip=1 tokens=1-3" %%a in ('wmic logicaldisk get name^,size^,freespace') do (
  37.     if not "%%c"=="" (
  38.         set quotient=&call :division %%c 1073741824
  39.         set size=!quotient! GB
  40.         set quotient=&call :division %%a %%c
  41.         set quotient=!quotient:^.=!&set quotient=!quotient:~0,5!
  42.         call :trim quotient !quotient!
  43.         set/a used_per=10000-!quotient!
  44.         set used_per=0000!used_per!&set used_per=!used_per:~-4!
  45.         set used_per_int=!used_per:~0,2!&set used_per_float=!used_per:~2!
  46.         call :trim used_per_int !used_per_int!
  47.         set used_per=!used_per_int!.!used_per_float!
  48.         for /l %%a in (1,1,13) do set size= !size!
  49.         echo %%b盘容量!size:~-13!   使用率!used_per!%
  50.     )
  51. )
  52. goto :eof
  53. :Show_SystemRunningTimeDiff
  54. for /f "tokens=2-3" %%a in ('net statistics workstation^|findstr "统计数据开始于"') do (
  55.     set StartTime=%%a %%b
  56. )
  57. for /f "tokens=1" %%a in ("%date%") do set date=%%a
  58. set EndTime=%date% %time:~0,-3%
  59. echo 最近一次开机时间:%starttime%
  60. echo         当前时间:%endtime%
  61. call :DateDiff %StartTime% %EndTime%
  62. echo   系统已运行时间:%Y%年%D%日%H%时%M%分%S%秒
  63. goto :eof
  64. :DateDiff !StartTime! !EndTime!
  65. set StartTime=%StartTime:-= %
  66. set StartTime=%StartTime:/= %
  67. set StartTime=%StartTime::= %
  68. set EndTime=%EndTime:-= %
  69. set EndTime=%EndTime:/= %
  70. set EndTime=%EndTime::= %
  71. call :DateToSecs %StartTime% StartSecs
  72. call :DateToSecs %EndTime% EndSecs
  73. set /a DiffSecs=EndSecs-StartSecs
  74. set DiffSecs=%DiffSecs:-= %
  75. set /a Y=DiffSecs/31536000
  76. set /a D=(DiffSecs%%31536000)/86400
  77. set /a H=(DiffSecs%%86400)/3600
  78. set /a M=(DiffSecs%%3600)/60
  79. set /a S=DiffSecs%%60
  80. goto :eof
  81. :DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
  82. setlocal ENABLEEXTENSIONS
  83. set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
  84. if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
  85. set /a dd=100%dd%%%100,mm=100%mm%%%100
  86. set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  87. set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  88. if 1%hh% LSS 20 set hh=0%hh%
  89. if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
  90. if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
  91. if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
  92. set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
  93. set /a j=j*86400+hh*3600+nn*60+ss
  94. endlocal&set %7=%j%&goto :eof
  95. :division
  96. setlocal
  97. set dividend=%1&set divisor=%2
  98. %len:$=dividend length_dividend%
  99. %len:$=divisor length_divisor%
  100. set add=4
  101. for /l %%a in (1,1,!add!) do set dividend=!dividend!0
  102. set/a length_dividend+=!add!
  103. set/a up=!length_dividend!-1
  104. for /l %%a in (0,1,!up!) do (
  105.     set minuend=!minuend!!dividend:~%%a,1!
  106.     call :trim minuend !minuend!
  107.     call :compare !minuend! !divisor!
  108.     if "!flag_com!"=="geq" (
  109.         set quo=&call :subtraction !minuend! !divisor!
  110.         set quotient=!quotient!!quo!
  111.         set minuend=!D_value!
  112.     ) else (
  113.         set quotient=!quotient!0
  114.     )
  115. )
  116. set int=!quotient:~0,-%add%!&set float=!quotient:~-%add%!
  117. call :trim int !int!
  118. set quotient=!int!.!float!
  119. endlocal&set quotient=%quotient%&goto :eof
  120. :subtraction  被减数  减数
  121. setlocal&set a=%1&set b=%2&set/a quo+=1
  122. %len:$=a length_a%
  123. set b=0!b!&set b=!b:~-%length_a%!
  124. set/a p=!length_a!-1
  125. for /l %%a in (0,1,!p!) do (
  126.     set arrA_%%a=!a:~%%a,1!
  127.     set arrB_%%a=!b:~%%a,1!
  128. )
  129. for /l %%a in (!p!,-1,0) do (
  130.     if !arrA_%%a! geq !arrB_%%a! (
  131.         set/a dif=!arrA_%%a!-!arrB_%%a!&set diff=!dif!!diff!
  132.     ) else (
  133.         set/a borrow=%%a-1
  134.         set/a arrA_!borrow!-=1
  135.         set/a dif=!arrA_%%a!+10-!arrB_%%a!
  136.         set diff=!dif!!diff!
  137.     )
  138. )
  139. if "!diff:~0,1!"=="0" call :trim diff !diff!
  140. call :compare !diff! %2
  141. if "!flag_com!"=="geq" (
  142. endlocal&set quo=%quo%&set diffr=%diff%&call :subtraction !diffr! %2
  143. ) else (
  144. endlocal&set quo=%quo%&set D_value=%diff%&goto :eof
  145. )
  146. :compare  被减数  减数(除数)
  147. setlocal&set a=%1&set b=%2
  148. %len:$=a length_a%
  149. %len:$=b length_b%
  150. set/a l=!length_a!-1
  151. if !length_a! gtr !length_b! (endlocal&set flag_com=geq&goto :eof)
  152. if !length_a! lss !length_b! (endlocal&set flag_com=lss&goto :eof)
  153. if !length_a! equ !length_b! (
  154.     if "!a!"=="!b!" (
  155.         endlocal&set flag_com=geq&goto :eof
  156.     ) else (
  157.         for /l %%a in (0,1,!l!) do (
  158.                 if "!a:~%%a,1!" gtr "!b:~%%a,1!" (endlocal&set flag_com=geq&goto :eof)
  159.                 if "!a:~%%a,1!" lss "!b:~%%a,1!" (endlocal&set flag_com=lss&goto :eof)
  160.         )
  161.     )
  162. )
  163. :trim
  164. setlocal
  165. set a=%2
  166. if "!a:~0,1!"=="0" (if not "!a!"=="0" (endlocal&set %1=%a:~1%&call :trim %1 !%1!))
复制代码
5、
  1. @echo off
  2. Rem 增量备份就是加了个/d
  3. if "%date:~-2%"=="周日" (
  4.     xcopy /e/h/r/y/k/o/f/exclude:filter.txt "D:\www.bathome" "D:\bbs.bathome"
  5. ) else (
  6.     xcopy /e/h/r/y/k/o/f/d/exclude:filter.txt "D:\www.bathome" "D:\bbs.bathome"
  7. )
  8. pause>nul
复制代码
6、
  1. @echo off&Rem 因为精确位数很容易修改,所以最后一位没有进行四舍五入
  2. set "len=for /f "tokens=1-2" %%1 in ("$") do (set ##=!%%1!&set %%2=&(for %%a in (4096,2048,1024,512,256,128,64,32,16,8,4,2,1) do (if not "!%%1:~%%a,1!"=="" (set/a %%2+=%%a&set %%1=!%%1:~%%a!)))&(if not "!%%1!"=="" set/a %%2+=1)&set %%1=!##!)"
  3. setlocal enabledelayedexpansion&title 任意位正整数除法 BY wankoilz
  4. set/p x=输入被除数:
  5. set/p y=输入除数:
  6. echo 计算中...
  7. call :division !x! !y!
  8. cls&echo 被除数:!x!&echo 除数:!y!&echo 商:!quotient!
  9. echo;&pause&exit
  10. :division
  11. setlocal
  12. set dividend=%1&set divisor=%2
  13. %len:$=dividend length_dividend%
  14. %len:$=divisor length_divisor%
  15. set add=100
  16. for /l %%a in (1,1,!add!) do set dividend=!dividend!0
  17. set/a length_dividend+=!add!
  18. set/a up=!length_dividend!-1
  19. for /l %%a in (0,1,!up!) do (
  20.     set/p=^><nul
  21.     set minuend=!minuend!!dividend:~%%a,1!
  22.     call :trim minuend !minuend!
  23.     call :compare !minuend! !divisor!
  24.     if "!flag_com!"=="geq" (
  25.         set quo=&call :subtraction !minuend! !divisor!
  26.         set quotient=!quotient!!quo!
  27.         set minuend=!D_value!
  28.     ) else (
  29.         set quotient=!quotient!0
  30.     )
  31. )
  32. set int=!quotient:~0,-%add%!&set float=!quotient:~-%add%!
  33. call :trim int !int!
  34. set quotient=!int!.!float!
  35. endlocal&set quotient=%quotient%&goto :eof
  36. :subtraction  被减数  减数
  37. setlocal&set a=%1&set b=%2&set/a quo+=1
  38. %len:$=a length_a%
  39. set b=0!b!&set b=!b:~-%length_a%!
  40. set/a p=!length_a!-1
  41. for /l %%a in (0,1,!p!) do (
  42.     set arrA_%%a=!a:~%%a,1!
  43.     set arrB_%%a=!b:~%%a,1!
  44. )
  45. for /l %%a in (!p!,-1,0) do (
  46.     if !arrA_%%a! geq !arrB_%%a! (
  47.         set/a dif=!arrA_%%a!-!arrB_%%a!&set diff=!dif!!diff!
  48.     ) else (
  49.         set/a borrow=%%a-1
  50.         set/a arrA_!borrow!-=1
  51.         set/a dif=!arrA_%%a!+10-!arrB_%%a!
  52.         set diff=!dif!!diff!
  53.     )
  54. )
  55. if "!diff:~0,1!"=="0" call :trim diff !diff!
  56. call :compare !diff! %2
  57. if "!flag_com!"=="geq" (
  58. endlocal&set quo=%quo%&set diffr=%diff%&call :subtraction !diffr! %2
  59. ) else (
  60. endlocal&set quo=%quo%&set D_value=%diff%&goto :eof
  61. )
  62. :compare  被减数  减数(除数)
  63. setlocal&set a=%1&set b=%2
  64. %len:$=a length_a%
  65. %len:$=b length_b%
  66. set/a l=!length_a!-1
  67. if !length_a! gtr !length_b! (endlocal&set flag_com=geq&goto :eof)
  68. if !length_a! lss !length_b! (endlocal&set flag_com=lss&goto :eof)
  69. if !length_a! equ !length_b! (
  70.     if "!a!"=="!b!" (
  71.         endlocal&set flag_com=geq&goto :eof
  72.     ) else (
  73.         for /l %%a in (0,1,!l!) do (
  74.                 if "!a:~%%a,1!" gtr "!b:~%%a,1!" (endlocal&set flag_com=geq&goto :eof)
  75.                 if "!a:~%%a,1!" lss "!b:~%%a,1!" (endlocal&set flag_com=lss&goto :eof)
  76.         )
  77.     )
  78. )
  79. :trim
  80. setlocal
  81. set a=%2
  82. if "!a:~0,1!"=="0" (if not "!a!"=="0" (endlocal&set %1=%a:~1%&call :trim %1 !%1!))
复制代码
7、
  1. @echo off&setlocal enabledelayedexpansion
  2. set/p url=请输入系列壁纸的网址:
  3. Rem wget http://www.zhuoku.com/zhuomianbizhi/star-starcn/20130729110511.htm
  4. wget !url!
  5. set Lf=^
  6. for %%a in ("!url!") do (set filename=%%~nxa&set dirname=%%~na)
  7. md !dirname! 2>nul
  8. for /f "delims=" %%a in ('findstr "src=.*http.*img.zhuoku.com.*jpg" !filename!') do (
  9.     set "str=%%a"
  10.     for %%b in ("!Lf!") do set "str=!str:img src=%%b!"
  11.     for /f "delims=" %%c in ('set str^| findstr "http.*img.zhuoku.com.*jpg"') do (
  12.         for /f %%d in (%%c) do (
  13.             set "url=%%d"
  14.             set "url=!url:~2,-1!"
  15.             set "url=!url:img=bizhi!"
  16.             set "url=!url:/thumbs=!"
  17.             set "url=!url:tn_=!"
  18.             wget !url!
  19.             for %%e in ("!url!") do move %%~nxe !dirname!\
  20.         )
  21.     )
  22. )
  23. del "!filename!" /f
  24. cls&echo 下载完毕!
  25. pause>nul
复制代码
8、
  1. @echo off&echo; 2>nul 3>nul
  2. set "cmd1=for /f "delims=" %%x in ("!str:~-1!") do set x_danwei=!%%~x!"
  3. set "cmd2=for /f "delims=" %%x in ("!str:~0,-1!") do set n_danwei=!%%~x!"
  4. setlocal enabledelayedexpansion
  5. set 壹=1&set 贰=2&set 叁=3&set 肆=4&set 伍=5
  6. set 陆=6&set 柒=7&set 捌=8&set 玖=9
  7. set 万=1&set 仟=1000&set 佰=100
  8. set 拾=10&set 圆=1&set 角=10&set 分=1
  9. for /f %%a in (a.txt) do (
  10.     set str=%%a&set str=!str:整=!&set str=!str:零=!
  11.     for %%b in (万_wan 仟_qian 佰_bai 拾_shi 圆_yuan 角_jiao 分_fen) do (
  12.         for /f "delims=_ tokens=1,2" %%c in ("%%b") do call :decompose %%c %%d
  13.     )
  14.     set str=!wan!
  15.     for %%c in (仟_q 佰_b 拾_s 万_g) do (
  16.         for /f "delims=_ tokens=1-2" %%d in ("%%c") do (
  17.              call :decompose %%d %%e
  18.     ))      
  19.     for %%c in (q b s g) do (
  20.         set str=!%%c!
  21.         %cmd1%
  22.         %cmd2%
  23.         set/a sum+=!n_danwei!*!x_danwei!
  24.     )
  25.     set/a sum=!sum!*10000
  26.     for %%c in (qian bai shi yuan) do (
  27.         set str=!%%c!
  28.         %cmd1%
  29.         %cmd2%
  30.         set/a sum+=!n_danwei!*!x_danwei!
  31.     )
  32.     for %%c in (jiao fen) do (
  33.         set str=!%%c!
  34.         %cmd1%
  35.         %cmd2%
  36.         set/a float+=!n_danwei!*!x_danwei!
  37.     )
  38.     set float=00!float!&set float=!float:~-2!
  39.     set sum=0000000000!sum!!float!&set sum=!sum:~-10!
  40.     set sort_!sum!=%%a
  41.     for %%c in (wan qian bai shi yuan jiao fen) do set %%c=
  42.     set sum=&set float=
  43. )
  44. echo 升序:&echo;
  45. for /f "delims== tokens=2" %%a in ('set sort') do echo %%a
  46. pause>nul
  47. :decompose
  48. set t=!str:*%1=!&if "!t!"=="" (set %2=!str!&goto :eof)
  49. set %2=!str:%t%=!& if "!%2!"=="" goto :eof
  50. call set str=%%str:!%2!=%%
复制代码
9、
  1. @echo off&setlocal enabledelayedexpansion&echo; 2>nul 3>nul
  2. Rem 要查F盘改 ".\测试文件夹" 为 "F:"
  3. echo 查找中...
  4. md tmp123
  5. call :loop ".\测试文件夹"
  6. for /f "delims=" %%a in ('dir/b tmp123') do (set/a n+=1&set "arr_!n!=%%a")
  7. set/a u=!n!-1
  8. for /l %%a in (1,1,!u!) do (
  9.     set/a low=%%a+1,up=!n!
  10.     for /l %%b in (!low!,1,!up!) do (
  11.         fc "tmp123\!arr_%%a!" "tmp123\!arr_%%b!" | findstr "找不到差异" >nul&&set "f_!arr_%%a!=!arr_%%b!"
  12.     )
  13. )
  14. cls&echo 存在重复情况的目录:&echo;
  15. for /f "delims=_= tokens=2,3" %%a in ('set f_') do (
  16.     set "s=%%a"&set "i=%%b"
  17.     set "s=!s:$=:!"&set "i=!i:$=:!"
  18.     set "s=!s:#=\!"&set "i=!i:#=\!"
  19.     echo !s! = !i!
  20. )
  21. rd tmp123 /s/q
  22. pause
  23. goto :eof
  24. :loop
  25. set "fn=%~1"&set fn=!fn::=$!&set fn=!fn:\=#!
  26. (for /f "delims=" %%a in ('dir/a-d/b "%~1"') do echo %%a)>"tmp123\!fn!"
  27. for /d %%a in ("%~1\*") do call :loop "%%a"
复制代码
10、
  1. @echo off&setlocal enabledelayedexpansion
  2. Rem 目录
  3. (
  4. echo ^<html^>^<body^>
  5. for /f "delims=" %%a in ('findstr/v "^图[0-9]*\.[0-9]*$" 时间简史.txt') do (
  6.     set "s=%%a"
  7.     if "!s!"=="时间简史 目录" set flag_mulu=true
  8.     if defined flag_mulu (
  9.         for /l %%x in (1,1,4) do set "s=&nbsp!s!"
  10.         for /f "tokens=1 delims=  " %%x in ("!s!") do (
  11.             set "t=%%x"&if "!t:~20,1!!t:~-1!"=="第章" set "s=<br>!s!"
  12.         )
  13.         set "s=!s!<br>"
  14.     echo !s!
  15. ))
  16. echo ^<br^>^<br^>
  17. )>时间简史.htm
  18. set flag_mulu=
  19. Rem 正文
  20. (for /f "delims=" %%a in ('findstr/v "^图[0-9]*\.[0-9]*$" 时间简史.txt') do (
  21.     set "s=%%a"
  22.     if "!s!"=="时间简史 目录" set flag_mulu=true
  23.     if not defined flag_mulu (
  24.         for /l %%x in (1,1,4) do set "s=&nbsp!s!"
  25.         for /f "tokens=1 delims=  " %%x in ("!s!") do (
  26.             set "t=%%x"&if "!t:~20,1!!t:~-1!"=="第章" set "s=<font size="5" color="blue">!s!</font>"
  27.         )
  28.         set "s=!s!<br><br>"
  29.     echo !s!
  30. ))
  31. echo ^</body^>^</html^>
  32. )>>时间简史.htm
  33. echo OK&pause
复制代码






附件: 您需要登录才可以下载或查看附件。没有帐号?注册

TOP

本帖最后由 ShadowFiend 于 2013-7-23 17:46 编辑

现在第六题,看有时间再做其他
  1. @rem 第六题
  2. @echo off
  3. set ucase=ABCDEFGHIJKLMNOPQRSTUVWXYZ
  4. set lcase=abcdefghijklmnopqrstuvwxyz
  5. set num=0123456789
  6. rem 下面是除了space外的特殊字符,共32个
  7. set "spec=!#$%%&'()*+,-./:;<=>?@[\]^_`{|}~""
  8. set "all=%ucase%%lcase%%num%%spec%"
  9. set /a len=26+26+10+32,count=1
  10. setlocal enabledelayedexpansion
  11. :start
  12. set op=&set o=
  13. rem 先分别在四种类型字符中选一个,保证至少存在四类字符
  14. for %%i in (%ucase%_26 %lcase%_26 %num%_10) do (
  15. set "tmp=%%i"
  16. set /a n=!random!%%!tmp:~-2!
  17. for %%j in (!n!) do set "op=!op!!tmp:~%%j,1!"
  18. )
  19. rem 特殊字符特殊处理
  20. set /a n=!random!%%32
  21. for %%i in (!n!) do set "op=!op!!spec:~%%i,1!"
  22. rem 再从所有字符中选出剩余的四个字符
  23. for /l %%i in (1 1 4) do (
  24. set /a n=!random!%%%len%
  25. for %%j in (!n!) do set "op=!op!!all:~%%j,1!"
  26. )
  27. rem 对8个字符做乱序处理,保证随机性
  28. for /l %%i in (8 -1 1) do (
  29. set /a n=!random!%%%%i
  30. for %%j in (!n!) do (
  31. set "o=!o!!op:~%%j,1!"
  32. set /a q=!n!+1
  33. for %%k in (!q!) do (set "op=!op:~0,%%j!!op:~%%k,8!")
  34. )
  35. )
  36. rem 判断是否重复,不重复则输出,出现重复的概率太小,个人认为没必要判断
  37. if %count% leq 100 (
  38. set c=%count%
  39. if %count% lss 100 (
  40. if %count% lss 10 (
  41. set c=00%count%
  42. ) else (
  43. set c=0%count%
  44. )
  45. )
  46. echo username: Bathome!c! password: !o!
  47. set /a count+=1
  48. if %count% lss 100 (goto start)
  49. )
  50. pause
复制代码
nevermore

TOP

本帖最后由 apang 于 2013-8-12 08:28 编辑

1.
  1. @echo off
  2. %1(for /f "delims=" %%a in ('%~s0 :^|sort /r') do del/a/f/q %%a&rd/q %%a)&exit /b
  3. set "folder=D:\BatHome"
  4. echo,"%folder%\"
  5. call :GetSubFolder "%folder%"
  6. exit /b
  7. :GetSubFolder
  8. for /f "delims=" %%a in ('dir /ad/b "%~1\"') do (
  9.     echo,"%~1\%%a\"
  10.     call :GetSubFolder "%~1\%%a"
  11. )
复制代码
2.
  1. @echo off
  2. set "wmic=wmic useraccount get name /value"
  3. (for /f "tokens=2delims==" %%a in ('%wmic%^|find "="') do (
  4.     for /f "tokens=*" %%b in ("%%a") do echo,%%b
  5. ))>$
  6. for /f "delims=" %%a in ('findstr/ixvg:"D:\BatHome_User_List.txt" $') do (
  7.     echo,增加的用户:%%a
  8. )
  9. for /f "delims=" %%a in ('findstr/ixvg:$ "D:\BatHome_User_List.txt"') do (
  10.     echo,减少的用户:%%a
  11. )
  12. del $
  13. pause
复制代码
3.
  1. :~!@#$%^&*()+-=<>?/\|
  2. @echo off&setlocal enabledelayedexpansion
  3. set "str=QWERTYUIOPASDFGHJKLZXCVBNM qwertyuiopasdfghjklzxcvbnm"
  4. set "str1=1234567890"
  5. set /p str2=<%0
  6. for /l %%a in (1001 1 1100) do (
  7.     set "pwd=" & set "Name=%%a"
  8.     for %%b in (%str%) do (
  9.         set "a=%%b"
  10.         set /a r=!random! %% 26
  11.         for %%c in (!r!) do set "pwd=!pwd!!a:~%%c,1!"
  12.     )
  13.     set /a r1=!random! %% 10,r2=!random! %% 21
  14.     for %%a in (!r1!) do set "pwd=!pwd!!str1:~%%a,1!"
  15.     for %%a in (!r2!) do set "pwd=!pwd!!str2:~%%a,1!"
  16.     Net User "BatHome!Name:~1!" "!pwd!%%a" /add
  17.     echo,用户名:BatHome!Name:~1! 密码:!pwd!%%a>>List.txt
  18. )
  19. pause
复制代码
4.
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=1,2*" %%a in ('systeminfo') do (
  3.     if "%%a"=="物理内存总量:" set "mem=%%b"
  4.     if "%%a"=="可用的物理内存:" set "freemem=%%b"
  5.     if "%%a"=="系统启动时间:" (
  6.         for /f "tokens=1-8" %%i in ("%%b %%c") do (
  7.             set /a "st=%%i*24*3600+%%k*3600+%%m*60+%%o"
  8.         )
  9.     )
  10. )
  11. set /a "mem1=%mem:,=%0/1024,mem2=100*(%mem:,=%-%freemem:,=%)/%mem:,=%"
  12. >D:\BatHome_OS.txt echo,已运行时间:%st%秒
  13. >>D:\BatHome_OS.txt echo,物理内存总量:%mem1:~,-1%.%mem1:~-1%GB
  14. >>D:\BatHome_OS.txt echo,内存使用率:%mem2%%%
  15. set wmic=wmic logicaldisk where "DriveType='3'"
  16. (for /f "tokens=1-3" %%i in ('%wmic% get freespace^,name^,size^|find ":"') do (
  17.     call :GetSize %%k00
  18.     set /a a=Size/1024
  19.     set /p=%%j容量:!a:~,-1!.!a:~-1!GB <nul
  20.     call :GetSize %%i00
  21.     set /a "b=100*(a-Size/1024)/a"
  22.     set /p=使用率:!b!%%<nul
  23.     echo,
  24. ))>>D:\BatHome_OS.txt
  25. pause&goto :eof
  26. :GetSize
  27. set "num=%1"&set "Size="&set "n="
  28. for %%a in (16 8 4 2 1) do (
  29.     if not "!num:~%%a!"=="" set /a n+=%%a&set "num=!num:~%%a!"
  30. )
  31. set "num=%1"
  32. set /a n-=6
  33. for /l %%a in (!n! -1 1) do (
  34.     set "s=!num:~,-%%a!"
  35.     if "!s:~,1!"=="0" set "s=!s:~1!"
  36.     set /a m=s%%1048576,s/=1048576
  37.     set "Size=!Size!!s!"
  38.     set "num=!m!!num:~-%%a!"
  39. )
  40. if "!Size:~,1!"=="0" set "Size=!Size:~1!"
复制代码
5.
  1. @echo off
  2. ::需要更改 Name 和 PWD 为实际的登录用户名和密码
  3. more +6 "%~0">"%~dp0Back.bat"
  4. schtasks /create /tn FulBack /tr "cmd /cxcopy /shcy \"D:\www.bathome\*\" \"D:\bbs.bathome\\"" /sc weekly /d sun /ru Name /rp PWD
  5. schtasks /create /tn IncBack /tr \""%~dp0Back.bat"\" /sc weekly /d mon,tue,wed,thu,fri,sat /ru Name /rp PWD
  6. pause&goto :eof
  7. pushd "%~dp0"
  8. (for %%a in ("\logs\" "\img\" ".zip" ".bak") do echo,%%~a)>$
  9. xcopy /shcdy /exclude:$ "D:\www.bathome\*" "D:\bbs.bathome\"
  10. del $
复制代码
6.
  1. @echo off&setlocal enabledelayedexpansion
  2. set /p Num1=输入被除数(正整数):
  3. set /p Num2=输入除数(小于9位数的正整数):
  4. set "x=100"
  5. set /a Len1=Len2=0&set "tmp=%Num1%"
  6. for %%a in (64 32 16 8 4 2 1) do (
  7.     if "!tmp:~%%a!" neq "" set /a Len1+=%%a&set "tmp=!tmp:~%%a!"
  8. )
  9. set "tmp=%Num2%fedcba9876543210"
  10. set /a Len2=0x!tmp:~15,1!
  11. for /l %%a in (1 10 %x%) do set "str=!str!0000000000"
  12. set "str=%Num1%%str%0"
  13. set /a n = Len1 - Len2 + x + 1
  14. for /l %%a in (!n! -1 1) do (
  15.     set "s=!str:~,-%%a!"
  16.     if "!s:~,1!"=="0" set "s=!s:~1!"
  17.     set /a m=s %% Num2,s/=Num2
  18.     set "Size=!Size!!s!"
  19.     set "str=!m!!str:~-%%a!"
  20. )
  21. if %Len1% lss %Len2% (
  22.     set "Size=0000000000!Size!" & set "Size=!Size:~-%x%!"
  23. ) else if "!Size:~,1!"=="0" set "Size=!Size:~1!"
  24. echo,%Num1% ÷ %Num2% = !Size:~,-%x%!.!Size:~-%x%!
  25. pause
复制代码
7.
  1. @echo off&setlocal enabledelayedexpansion
  2. ::需要第三方工具 wget 和 geturls
  3. set "url=http://www.zhuoku.com/zhuomianbizhi/show/index-1.htm"
  4. set "str=tool.zhuoku.com/tool/dyw_size.php?width=1440&height=900&corp_x=center&corp_y=top&picurl=.."
  5. wget -O "%tmp%\$1" "%url%" 2>nul
  6. md show 2>nul
  7. for /f "delims=" %%a in ('geturls /s:"zhuomianbizhi"^<"%tmp%\$1"^|findstr /i "\/[0-9]*\.htm$"') do (
  8.     if not defined %%~na (
  9.         wget -O "%tmp%\$2" "http://www.zhuoku.com/%%a" 2>nul
  10.         for /f "delims=" %%b in ('geturls^<"%tmp%\$2"^|findstr /i "\.jpg$"')do (
  11.             set "a=%%b"
  12.             set "a=!a:/thumbs/tn_=/!"
  13.             for /f "delims=" %%c in ("!a:img.zhuoku.com=%str%!") do (
  14.                 wget -O "show\%%~nxc" "%%c"
  15.             )
  16.         )
  17.         set "%%~na=1"
  18.     )
  19. )
  20. del "%tmp%\$*"
复制代码
8.
  1. @echo off&setlocal enabledelayedexpansion
  2. %1(for /f "tokens=2delims=." %%a in ('%~s0 :^|sort') do echo,%%a)>b.txt&exit /b
  3. for /f "delims=" %%a in (a.txt) do (
  4.     set "a=%%a" & set /a n=0
  5.     for %%b in (整 角 分) do set "a=!a:%%b=!"
  6.     for %%b in (捌 玖 拾) do set "a=!a:零%%b=%%b!"
  7.     set "a=!a:拾=*10+!"
  8.     set "a=!a:佰=*100+!"
  9.     set "a=!a:仟=*1000+!"
  10.     for %%b in (零 壹 贰 叁 肆 伍 陆 柒 捌 玖) do (
  11.         for %%c in (!n!) do set "a=!a:%%b=%%c!"
  12.         set /a n+=1
  13.     )
  14.     set "a=!a:+万=万!" & set "a=!a:+圆=圆!"
  15.     if "!a:万=!" neq "!a!" set "a=(!a:万=)*10000+!"
  16.     if "!a:圆=!"=="!a!" set "a=0圆!a!"
  17.     set "a=!a:+圆=圆!"
  18.     set "b=!a:*圆=!00"
  19.     set "b=!b:~,2!"
  20.     for /f "delims=圆" %%b in ("!a!") do set /a "a=%%b"
  21.     set "str=..........!a!!b!"
  22.     echo,!str:~-11!.%%a
  23. )
复制代码
9.
  1. @echo off&setlocal enabledelayedexpansion
  2. md "%tmp%\~tmp\" 2>nul
  3. for /d /r "f:\" %%a in (*) do (
  4.     set /a n+=1
  5.     set "_!n!=%%a"
  6.     (for %%b in ("%%a\*") do echo,%%~nxb)>"%tmp%\~tmp\!n!.tx"
  7. )
  8. pushd "%tmp%\~tmp\"
  9. (for /l %%a in (1 1 !n!) do (
  10.     for /f "delims=" %%b in ('findstr/imxg:%%a.tx *.tx 2^>nul')do (
  11.         if /i not "%%a.tx"=="%%b" (
  12.             fc /c %%a.tx %%b|find /i "FC: 找不到">nul && (
  13.                 if not defined #%%a (
  14.                     echo,!_%%a!&type %%a.tx&echo,
  15.                     set #%%a=1
  16.                 )
  17.                 if not defined #%%~nb (
  18.                     echo,!_%%~nb!&type %%b&echo,
  19.                     set #%%~nb=1
  20.                 )
  21.             )
  22.         )
  23.     )
  24. ))>"%~dp0List.txt"
  25. popd&rd "%tmp%\~tmp\" /s /q
  26. pause
复制代码
10.
  1. @echo off
  2. (echo,^<html^>&echo,^<body^>)>a.html
  3. (for /f "delims=" %%a in ('findstr/ivn "^图[0-9]*\.[0-9]*" a.txt') do (
  4.     set "str=%%a"
  5.     setlocal enabledelayedexpansion
  6.     set "str=!str:*:=!"
  7.     if "!str:~,1!"=="第" if "!str:章=!" neq "!str!" set flag=1
  8.     if defined flag (
  9.         echo,^</br^>
  10.         set "str=<p>!str!</p>"
  11.     ) else (
  12.         if "!str!" neq "" (
  13.             set "str=&nbsp;&nbsp;&nbsp;&nbsp!str!</br>"
  14.         ) else set "str=!str!</br>"
  15.     )
  16.     echo,!str!
  17.     endlocal
  18. ))>>a.html
  19. (echo,^</body^>&echo,^</html^>)>>a.html
  20. pause
复制代码

TOP

本帖最后由 xxpinqz 于 2013-7-22 18:36 编辑

vbs用来计算惯了,表示计算类都没写过。第三方更少接触,附上几个。表示纯支持,纯参与。
第一题:
  1. @echo off
  2. rem 能del /a/s/f/q *.*吗
  3. set "dst=d:\bathome"
  4. (for /f "delims=" %%a in ('dir /b/s/a "%dst%"^|sort /r') do (
  5.       del /a/q/f "%%a"
  6.       rd "%%a"
  7. ))>nul 2>nul
  8. rd "%dst%"
复制代码
第二题:郁闷的这题,有人考虑含空格的用户名吗,表示没招,查找目录也不一定准确。或者多虑了
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "skip=4 delims=" %%a in ('net user^|findstr /iv "命令成功完成"') do (
  3.      for %%i in (%%a) do set "#%%i=1"
  4. )
  5. for /f "delims=" %%a in (d:\BatHome_User_List.txt) do set "#%%a=!#%%a!2"
  6. for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
  7.     if %%b equ 1 (
  8.         set "strz=!strz!%%a "
  9.     ) else (
  10.         if %%b equ 2 set "strj=!strj!%%a "
  11.     )
  12. )
  13. echo 非法增加用户:!strz!
  14. echo 非法减少用户:!strj!
  15. pause
复制代码
第三题:
  1. @echo off
  2. set "dx=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  3. set "xx=abcdefghijklmnopqrstuvwxyz"
  4. set "sz=0123456789"
  5. set "zf=$#@~_+{}|;'<>?,./\][=-)(*:"
  6. set l=1000
  7. setlocal enabledelayedexpansion
  8. :loop
  9. set/a r1=!random!%%26,r2=!random!%%10,r3=!random!%%26,r4=!random!%%10
  10. if not defined #!r1!-!r2!-!r3!-!r4! (
  11.      set /a l+=1
  12.      set #!r1!-!r2!-!r3!-!r4!=!l!
  13. )
  14. if %l% lss 1100 goto :loop
  15. for /f "tokens=1,2 delims=#=" %%a in ('set #') do (
  16.     set "id=%%b"
  17.     for /f "tokens=1-4 delims=-" %%i in ("%%a") do (
  18.          set "pw=!dx:~%%i,1!!xx:~%%i,1!!zf:~%%i,1!!sz:~%%j,1!!dx:~%%k,1!!xx:~%%k,1!!zf:~%%k,1!!sz:~%%l,1!"
  19.          echo net user Bathome!id:~-3! !pw! /add
  20.     )
  21. )
  22. pause
复制代码
第四题:
浮点啊,不会。。。。
第五题:
算日期,也不会,只好走个捷径
  1. @echo off
  2. rem 不会计算日期,只好这么写了,win7测试,XP默认是周还是星期?
  3. more +6 <%~fs0>autobak.bat
  4. schtasks /create /tn autobak /sc daily /ru system /tr "%~dp0autobak.bat" /f
  5. goto :eof
  6. @echo off
  7. cd /d "%~dp0"
  8. for /f "tokens=1-4 delims=-/ " %%a in ("%date%") do (
  9.     set "today=%%b-%%c-%%a"
  10.     set "week=%%d"
  11. )
  12. if not exist "*.lst" (
  13.      (echo logs\
  14.       echo img\
  15.       echo .zip
  16.       echo .bak
  17.     )>"%today%.lst"
  18. )
  19. for %%a in (*.lst) do set "day=%%~na"
  20. ren "%day%.lst" "%today%.lst"
  21. if "%week:周=星期%"=="星期日" (
  22.     xcopy "d:\www.bathome\*.*" "d:\bbs.bathome\%week%\" /yshr
  23. ) else (
  24.     xcopy "d:\www.bathome\*.*" "d:\bbs.bathome\%week%\" /yshr /d:%day% /exclude:%today%.lst
  25. )
复制代码
第六题:
计算的略过
第七题:
curl,纯欣赏。
第八题:
  1. @echo off&setlocal enabledelayedexpansion
  2. more +20<%~s0>tmp
  3. set "c=零-+0# 壹-+1# 贰-+2# 叁-+3# 肆-+4# 伍-+5# 陆-+6# 柒-+7# 捌-+8# 玖-+9# 拾-0 佰-00 仟-000 万-^)#0000+^(0 角-0 分- "
  4. for /f "delims=" %%z in (tmp)do (
  5.   for /f "tokens=1,2 delims=圆整" %%i in ("%%z")do (
  6.     set "str=(%%i).%%j"
  7.     for %%a in (%c%) do (
  8.       for /f "tokens=1-2 delims=-" %%b in ("%%a")do set "str=!str:%%b=%%c!"
  9.     )
  10.     for /f "tokens=1-2 delims=." %%a in ("!str:#=*1!")do (
  11.       set/a xs=100%%b,num=%%a
  12.       set "num=0000000000!num!.!xs:~-2!"
  13.       set "#!num:~-12!=%%z"
  14.     )
  15.   )
  16. )
  17. for /f "tokens=2 delims==" %%a in ('set #')do echo,%%a
  18. del /f tmp&pause>nul&goto :eof
  19. 陆拾叁万贰仟伍佰肆拾叁圆叁角整
  20. 伍万圆整
  21. 捌万零肆佰圆伍角叁分
  22. 壹佰圆整
  23. 贰佰万叁仟贰佰陆拾陆圆零叁分
  24. 柒佰柒拾万零捌拾叁圆整
  25. 叁万圆肆角肆分
  26. 壹圆整
  27. 玖仟万零玖圆玖角
复制代码
第玖题:
  1. @echo off&setlocal enabledelayedexpansion
  2. set "src=%~dp0"
  3. (for /d /r f: %%a in (*) do (
  4.     pushd "%%a"
  5.     set str=
  6.     for %%b in (*) do set "str=!str!%%b"
  7.     if defined str (
  8.           echo,!str!*%%a
  9.     ) else (
  10.           echo,%%a >>%src%$t
  11.     )   
  12. ))>%src%$f
  13. cd /d "%src%"
  14. echo                    相同的不含文件的文件夹:
  15. type $t
  16. echo                      含相同文件的文件夹:
  17. for /f "tokens=1* delims=*" %%a in ('sort $f') do (
  18.     set "str1=!str2!"
  19.     set "path1=!path2!"
  20.     set "str2=%%a"
  21.     set "path2=%%b"
  22.     if "!str1!"=="!str2!" (
  23.     echo !path1!
  24.     set .=.
  25.     ) else (
  26.         if defined . (
  27.               set.=
  28.               echo,!path1!
  29.               echo ---------------传说的分割线---------------
  30.         )
  31.     )
  32. )
  33. if defined . echo,!path2!
  34. del $?&pause
复制代码
第十题:
没整过html,也不知道对不对.
  1. @echo off
  2. set "char=一 二 三 四 五 六 七 八 九 十 零"
  3. (echo ^<HTML^>^<BODY^>
  4. for /f "delims=" %%a in ('findstr /bivx "图[0-9]*\.[0-9]*$" 时间简史.txt') do (
  5.     for /f "delims=章" %%b in ("%%a") do (
  6.         set "str=%%b"
  7.         setlocal enabledelayedexpansion
  8.         for %%c in (%char%) do set "str=!str:%%c=!"
  9.         if "!str!"=="第" echo ^<br^>
  10.         endlocal
  11.     )
  12.     echo ^<br^>
  13.     echo ^&nbsp^&nbsp^&nbsp^&nbsp%%a^<br^>
  14. )
  15. echo ^</BODY^>^</HTML^>
  16. )>时间简史.html
复制代码
初学BAT,非专业。代码不适当之处还望前辈们多多指点。在此表示感谢!

TOP

好多题,也好诱惑!
如无特别说明,代码测试环境均为 XP SP3

TOP

本帖最后由 523066680 于 2013-8-19 19:30 编辑

第一题:
(没有考虑特殊文件夹或者权限限制的情况)

修改时间:2013-08-08
改善问题:1、遇到含有%、! 的符号出错
             2、for /f + 'dir'效率低,改为了for /D
  1. @echo off
  2. set target=x:\bathome
  3. if exist %target% (
  4.     cd /d %target%\
  5.     call :subfolder
  6.     cd ..
  7.     rd %target%
  8. ) else (
  9.     echo %target% 目录不存在
  10. )
  11. pause
  12. exit
  13. :subfolder
  14.   echo %cd%
  15.   del *.* /a /f /q
  16.   for /D %%i in (*.*) do (
  17.       cd "%%~fi"
  18.       call :subfolder
  19.       cd ..
  20.       rd "%%~fi"
  21.   )
  22.   goto :eof
  23.   
复制代码
第三题,
修改时间:2013-07-27
改善问题:1、支持%、! 等字符
             2、减少call,提速
             3、net user 在遇到 " 时需要用\"转义。

每一组出现的次数是随机的(在至少出现一次的基础上),每一组出现的位置是随机的。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set t=%time%
  4. set S1=ABCDEFGHIJKLMNOPQRSTUVWXYZ
  5. set S2=abcdefghijklmnopqrstuvwxyz
  6. set S3=0123456789
  7. rem       V    V V V              V    V V
  8. set "S4=~`^!@#$%%^^^&*()_+-={}[]|\:^";'^<^>,.?/"
  9. set /a N1=26,N2=26,N3=10,N4=32
  10. set /a i=0
  11. :LP
  12.     set "Series="
  13.     set "Code="
  14.     rem [1-4]是4个必须的组元素,4以上随机选组
  15.     for /l %%i in (1,1,8) do (
  16.         if %%i leq 4 (
  17.             set /a Who=%%i, R=!random! %% N%%i
  18.         ) else (
  19.             set /a Who=!random! %% 4 + 1
  20.             set /a R=!random! %% N!Who!
  21.         )
  22.         set Code[%%i]=!Who!_!R!
  23.     )
  24.     rem 从数组中随机提取元素,装载到SRand
  25.     rem 根据提取的个数,逐步缩小提取范围
  26.     set "SRand="
  27.     for /l %%a in (8,-1,1) do (
  28.         set /a R=!random! %% %%a + 1
  29.         for %%b in (!R!) do set SRand=!SRand! !Code[%%b]!
  30.         set Code[!R!]=!Code[%%a]!
  31.     )
  32.     if not defined EVER_%SRand: =% (
  33.         set EVER_%SRand: =%=exist
  34.         set /a i+=1
  35.         rem 编号转字符
  36.         for %%n in (%SRand%) do (
  37.             for /f "tokens=1,2 delims=_" %%a in ("%%n") do (
  38.                 set Series=!Series!!S%%a:~%%b,1!
  39.             )
  40.         )
  41.         set nnn=00!i!
  42.         set nnn=!nnn:~-3!
  43.         rem 设置密码如果出现双引号, 需要用\进行转义
  44.         rem net user Bathome!nnn! "!Series:"=\"!" /add 1>nul
  45.         echo 编号和密码:!nnn! !Series!
  46.     ) else (
  47.         rem  其实很少重复,走个过场。
  48.         echo 出现重复密码
  49.         goto :LP
  50.     )
  51.     if %i% lss 100 (
  52.         goto :LP
  53.     )
  54. echo %t% %time%
  55. pause
  56. exit
复制代码
编号和密码:090 Mb1BN49}
编号和密码:091 i9A8^2DU
编号和密码:092 bm6=:I1V
编号和密码:093 1G+iY2i3
编号和密码:094 nq"s|8rI
编号和密码:095 2[|8SmL1
编号和密码:096 kZf:6ST:
编号和密码:097 W&6?*b8f
编号和密码:098 6&4t_1P2
编号和密码:099 9e^7K>EV
编号和密码:100 C>%Qz1Q4



第八题   很长……
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /a n=1, m=10
  4. for %%i in (壹 贰 叁 肆 伍 陆 柒 捌 玖 分 角 圆 拾 佰 仟 万) do (
  5.     if !n! leq 9 (
  6.         set /a C_%%i=n,n+=1
  7.     ) else (
  8.         set /a P_%%i=m,m-=1
  9.     )
  10. )
  11. set /a Code=0, Begin=0
  12. rem 读取文本/调用Deal转换数字
  13. for /f "tokens=*" %%a in ('type %0') do (
  14.     if !Begin! equ 1 (call :Deal %%a)
  15.     if "%%a"=="::Data" (set /a Begin=1)
  16. )
  17. rem 选择排序
  18. for /l %%a in (%Code%,-1,1) do (
  19.     set Ever=0000000000_000
  20.     for /l %%b in (1,1,%%a) do (
  21.        if !Ser[%%b]! gtr !Ever! (
  22.            set Ever=!Ser[%%b]!
  23.            set Num=%%b
  24.        )
  25.     )
  26.     set Ser[!Num!]=!Ser[%%a]!
  27.     set Ser[%%a]=!Ever!
  28. )
  29. for /l %%a in (1,1,%Code%) do (
  30.     for %%b in (!Ser[%%a]!) do echo !SRC_%%b!
  31. )
  32. pause
  33. exit
  34. :Deal
  35.     set Say=%1
  36.     set Ser=0000000000
  37.     set Say=%Say:整=%
  38.     set Say=%Say:零=%
  39.     set /a i=0,Cut=0,Code+=1
  40.     for /l %%a in (0,1,25) do (
  41.         set z=!Say:~-1,1!
  42.         set Say=!Say:~,-1!
  43.         if defined P_!z! (
  44.             set /a Site=P_!z!-Cut
  45.             if "!z!"=="万" (set Cut=4)
  46.         ) else (
  47.             set /a Num=C_!z!,SiteA=Site-1
  48.             for /f "tokens=1,2" %%i in ("!Site! !SiteA!") do (
  49.                 set Ser=!Ser:~0,%%j!!Num!!Ser:~%%i!
  50.             )
  51.         )
  52.         if "!Say!"=="" (goto :next)
  53.     )
  54.     :next
  55.     set NNN=000%Code%
  56.     rem NNN后缀用来确保相同的数额能够在排序中保留
  57.     set Ser[%Code%]=%Ser%_!NNN:~-3!
  58.     set SRC_%Ser%_!NNN:~-3!=%1
  59.     goto :eof
  60. ::Data
  61. 陆拾叁万贰仟伍佰肆拾叁圆叁角整
  62. 伍万圆整
  63. 捌万零肆佰圆伍角叁分
  64. 壹佰圆整
  65. 贰佰万叁仟贰佰陆拾陆圆零叁分
  66. 柒佰柒拾万零捌拾叁圆整
  67. 叁万圆肆角肆分
  68. 壹圆整
复制代码
第九题
最后修改: 8月19日
基本上在遇到含有%%!! () & 符号的文件、文件夹时不会出错。不过效率很低。
如果多个目录相同,比如,A=B=C  可以并排列出。而不是A=B,B=C,A=C分别列出。
  1. ::Find the same directory
  2. ::     by 523066680
  3. ::2013-08-19
  4. @echo off
  5. :init
  6.     set MainDir=%cd%\
  7.     set MainDir=%MainDir:\\=\%
  8.     set text="%MainDir%F_Record.txt"
  9.     set Ra="%MainDir%FileRa.txt"
  10.     set Rb="%MainDir%FileRb.txt"
  11.     type nul>%text%
  12.     set t=%time%
  13.     set /a rept=1
  14.     rem list = 0 输出结果不列出文件列表
  15.     rem list = 1 列出文件数量、大小
  16.     rem list = 2 列出文件列表和数量、大小
  17.     set list=1
  18.     rem confrim = 1 or 0 : 决定是否核对文件大小
  19.     set confirm=1
  20.     echo Current Directory: "%MainDir%"
  21.     if %confirm% equ 0 (
  22.         set "ext="
  23.         set "arg=/b"
  24.         set "filter="
  25.         set filt2="字节"
  26.     ) else (
  27.         set "ext=%%~za"
  28.         set "arg=/w"
  29.         set "filter=|more +5 |find /v " 可用字节""
  30.         set filt2=" 可用字节"
  31.     )
  32. :deal
  33.     for /r "%MainDir%" %%a in (.) do (
  34.         if "%%a"=="%MainDir%%%~nxa\." echo "%%a"
  35.         cd "%%a"
  36.         call :next
  37.     )
  38.     echo 处理结果:
  39.     rem 对搜集的数据汇总输出
  40.     set /a rept-=1
  41.     if %rept% equ 0 (
  42.         set /p inp=没有找到重复文件夹<nul
  43.         pause >nul
  44.         exit
  45.     )
  46.     for /l %%a in (1,1,%rept%) do (
  47.         for /f "tokens=2 delims=?" %%b in ('set SAME_[%%a]') do (
  48.             echo %%b
  49.             echo %%b >>%text%
  50.             set "last=%%b"
  51.             if "%confirm%%list%"=="01" (dir /w %%b |find " 个文件" >>%text%)
  52.             if "%confirm%%list%"=="02" (
  53.                 dir /w %%b |more +5 |find /v " 个目录" >>%text%
  54.             )
  55.         )
  56.         if "%confirm%%list%"=="11" (
  57.             call dir /w %%last%% |find " 个文件" >>%text%
  58.         )
  59.         if "%confirm%%list%"=="12" (
  60.             call dir /w %%last%% |more +5 |find /v " 个目录" >>%text%
  61.         )
  62.         echo,>>%text%
  63.     )
  64.     cd %MainDir%
  65.     echo 清理临时文件FileRa.txt FileRb.txt
  66.     if exist %Ra% del %Ra% %Rb%
  67.     echo 前后时间: %t% %time%
  68.     pause
  69.     exit
  70. :next
  71.     set str=_
  72.     rem 文件名单
  73.     rem 有短名则使用短名,避免"="造成的变量赋值、判断问题
  74.     for /f "tokens=4" %%a in ('dir /a /x ^|more +7 ^|find "-"') do (
  75.         (
  76.           call set "str=%%str%%_%%a_%ext%" 2>nul
  77.         ) || (
  78.           goto :out
  79.         )
  80.     )
  81.     rem 空目录跳过
  82.     if "%str%"=="_" (goto :eof)
  83.     :out
  84.     rem 不管字符串是否超载,都进行截取。
  85.     set "str=%str:~0,3000%"
  86.     set "str=%str: =#%"
  87.     set "str=%str:,=#%"
  88.     set "str=%str:&=#%"
  89.     if not defined Def_%str% (
  90.         call set "Def_%%str%%=%%cd%%"
  91.     ) else (
  92.         rem 做进一步确认:
  93.         rem 原因1、变量记录的有限数据,作为初步判断
  94.         rem 原因2、相似的文件名可以产生相同的短名
  95.         call dir %arg% "%%Def_%str%%%" %filter% >%Ra%
  96.         dir %arg% "%cd%" %filter% >%Rb%
  97.         (
  98.           fc %Ra% %Rb% >nul && (
  99.               rem Same Folder
  100.               call :record
  101.           )
  102.         ) || (
  103.           rem echo 在疑似重复的目录中找到异同
  104.         )
  105.     )
  106.     goto :eof
  107. :record
  108.     if not defined REPT_%str% (
  109.         set "REPT_%str%=%rept%"
  110.         set "COF_%str%=2"
  111.         call set SAME_[%rept%][1]=?"%%Def_%str%%%"
  112.         set SAME_[%rept%][2]=?"%cd%"
  113.         set /a rept+=1
  114.     ) else (
  115.         call set /a "COF=%%COF_%str%%%+1"
  116.         call set "COF_%str%=%%COF%%"
  117.         call set "SAME_[%%REPT_%str%%%][%%COF_%str%%%]=?"%%cd%%""
  118.     )
  119.     goto :eof
复制代码
检查D盘耗时差不多1小时。

confirm=1  list=1  详细对比并列出文件数量大小

"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\复件 (2) a"
"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\复件 a"
               2 个文件          1,404 字节

"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\ftest"
"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\复件 ftest"
               1 个文件              0 字节



confirm=0  list=1   仅对比文件名、列出文件大小
(这个时候是分开列出文件大小的,增加了一个文件夹做测试)

"D:\me\IF_Batch\目录、文件操作\FindSameFolder\大小不同的例子\CA"
               1 个文件              6 字节
"D:\me\IF_Batch\目录、文件操作\FindSameFolder\大小不同的例子\CB"
               1 个文件              3 字节

"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\复件 (2) a"
               2 个文件          1,404 字节
"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\复件 a"
               2 个文件          1,404 字节

"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\ftest"
               1 个文件              0 字节
"D:\me\IF_Batch\目录、文件操作\FindSameFolder\错误实例\复件 ftest"
               1 个文件              0 字节


虽然不想用批处理做第九题。
没有以前那么能折腾了,做出来还是效率又低问题依旧。。。
附件,内有备注明一些错误情况。
附件: 您需要登录才可以下载或查看附件。没有帐号?注册

TOP

本帖最后由 terse 于 2013-9-18 19:03 编辑

第一题 不知道符合不
  1. @echo off
  2. del /f /s /q "D:\BatHome" >nul
  3. for /f "delims=" %%i in ('dir /ad /b /s "D:\BatHome" ^|sort /r') do rd "%%i"
  4. rd "D:\BatHome"
  5. pause
复制代码
第二题 比较一下 wmic   wmic 虽然取名方便 对比效率还是不行  所以还是这样处理了
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=*" %%a in (D:\BatHome_User_List.txt) do set "$%%a=a"
  3. (for /f "skip=4 delims=" %%i in ('net user') do (
  4.     if defined str (
  5.        for /l %%j in (0 25 50) do (
  6.            for /f "tokens=*" %%a in ("!str:~%%j,25!") do (  
  7.                if not defined $%%~nxa echo %%~nxa
  8.                set #%%~nxa=a
  9.            )
  10.        )
  11.    )
  12.    set "str=%%i"
  13. ))>增加的用户.txt
  14. (for /f "tokens=*" %%a in (D:\BatHome_User_List.txt) do if not defined #%%~nxa echo %%~nxa)>减少的用户.txt
  15. pause
复制代码
上面账户名不含空格和特殊字符的 可以简化下
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "tokens=*" %%a in (D:\BatHome_User_List.txt) do set "$%%a=a"
  3. (for /f "skip=4 delims=" %%i in ('net user') do (
  4.      for %%j in (!str!) do (  
  5.            if not defined $%%~nxj echo %%~nxj
  6.            set #%%~nxj=a
  7.      )
  8.      set "str=%%i"
  9. ))>增加的用户.txt
  10. (for /f "tokens=*" %%a in (D:\BatHome_User_List.txt) do if not defined #%%~nxa echo %%~nxa)>减少的用户.txt
  11. pause
复制代码
第三题 偷点懒了 写着写着 代码怎么这么长 总想着在一个FOR里完成  不过效率还好 还应该算随机了
发现还是有重复 现在还没时间修改
换个随机性强点
  1. @echo off
  2. set "code1=0123456789"
  3. set "code2=abcdefghijklmnopqrstuvwxyz"
  4. set "code3=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  5. set "code4=`~!@$^&*()-\+|{}[];'<>,.?/"
  6. set /a num=1000
  7. setlocal enabledelayedexpansion
  8. for /L %%i in (0,1,9) do set #%%i=!code1:~%%i,1!&set #####%%i#=1&set "arr=!arr!#####%%i#"
  9. for /L %%i in (2,1,5) do (
  10. set s%%i=
  11. for /L %%j in (1,1,%%i) do set s%%i=1!s%%i!
  12. for /L %%j in (%%i,1,9) do set s%%i=!s%%i!0
  13. set s%%i=!s%%i!
  14. set /a n=10,i=j=1
  15. for /L %%j in (1,1,%%i) do set /a i*=n,j*=%%j,n-=1
  16. set /a lena_%%i=i/j,lenb_%%i=lena_%%i-22
  17. )
  18. for /L %%i in (2,1,5) do (
  19. set var=
  20. for /L %%a in (1,1,!lena_%%i!) do (
  21.          set "s=#####"
  22.          for /l %%b in (0 1 9) do if "!s%%i:~%%b,1!" == "1" set "s=!s!!#%%b!"
  23.          for /f %%b in ("0!s%%i:10= !") do set "t=%%b"
  24.        set "s%%i=!t:0=!!t:1=!1!s%%i:*10=!"
  25. set var=!var!!s:~-6!#
  26. )
  27. for /L %%a in (!lena_%%i!,-1,!lenb_%%i!) do (
  28. set /a "n=!random!%%%%a*7"
  29. for %%b in (!n!) do set "s=!var:~%%b,7!"
  30. for %%b in (!s!) do set var=!var:%%b=!
  31. set "arr=!arr!!s!"
  32. set "!s!=%%i"
  33. )
  34. )
  35. for /L %%i in (102,-1,3) do (
  36. set /a "n=!random!%%%%i*7,num+=1"
  37. for %%j in (!n!) do set "s=!arr:~%%j,7!"
  38. for %%j in (!s!) do set arr=!arr:%%j=!&set n1=!%%j!
  39.         set /a "n2=!random!%%(6-n1)+1,n3=!random!%%(7-n1-n2)+1,n4=8-n1-n2-n3"
  40. set s=!s:#=!
  41. for /L %%a in (2,1,4) do (
  42. for /L %%b in (1,1,!n%%a!) do (
  43. set /a m=!random!%%26,r=!random!%%n1,n1+=1
  44. for /f "tokens=1,2" %%c in ("!m! !r!") do set s=!s:~%%d!!code%%a:~%%c,1!!s:~,%%d!
  45. )
  46.     )
  47.         net user BatHome!num:~1! !s! /add 2>nul
  48. )
  49. pause
复制代码
第四题 这里处理成百分比吗
  1. @echo off&setlocal enabledelayedexpansion
  2. set "wmi=LastBootUpTime,LocalDateTime,TotalVisibleMemorySize,FreePhysicalMemory"
  3. for /f "delims=U" %%a in ('cmd /u /c echo 唉') do set "tab=%%a"
  4. (
  5. for /f "skip=1 tokens=1-4" %%i in ('"wmic os get %wmi%"') do for %%j in (%%j) do (
  6. set "ymd=%%j"
  7. set /a "m=(1!ymd:~4,2!+5)%%12+4,y=!ymd:~,4!-m/14,d=1!ymd:~6,2!-100,h=1!ymd:~8,2!-100"
  8. set /a "s=1461*y/4+153*m/5+d"
  9. set /a "s1=h*3600+(1!ymd:~10,2!-100)*60+1!ymd:~12,2!-100"
  10. set "ymd=%%k"
  11. set /a "m=(1!ymd:~4,2!+5)%%12+4,y=!ymd:~,4!-m/14,d=1!ymd:~6,2!-100,h=1!ymd:~8,2!-100"
  12. set /a "s=(1461*y/4+153*m/5+d-s)*86400-s1,s+=h*3600+(1!ymd:~10,2!-100)*60+1!ymd:~12,2!-100"
  13. set size=%%l
  14. set /a "useb=%%l-%%i,usebp=useb*100/%%l"
  15. for %%a in (size useb) do (
  16. set mod=!%%a!
  17. for /l %%b in (1 1 3) do (
  18. set/a "Qent=mod/1048576,mod=mod%%1048576*10"
  19. set "%%a_g=!%%a_g!!Qent!"
  20. )
  21. )
  22. echo 物理内存: !size_g:~,-2!.!size_g:~-2! [GB]
  23. echo 已使用  : !useb_g:~,-2!.!useb_g:~-2! [GB]
  24. echo 使用率  : !usebp!%%
  25. echo 系统运行时间 : !s! 秒
  26. )
  27. echo 盘符%tab%总容量    %tab%已用空间%tab%可用空间%tab%使用率
  28. set "get=get DeviceID^,FreeSpace^,Size"
  29. for /f "skip=1 tokens=1-3" %%a in ('wmic logicaldisk where "drivetype='3'" !get!') do (
  30. for %%b in (%%b) do (
  31. set $Free=%%b&set "$size=%%c"
  32. for %%i in (size Free) do (
  33. set Qent=&set/a "mod=!$%%i:~,9!%%1048576,%%i=!$%%i:~,9!/1048576"
  34. if "!$%%i:~9!" neq "" (
  35. set "len=!$%%i:~9!876543210"
  36. set /a "len=!len:~8,1!,Qent=!mod!!$%%i:~9!/1048576"
  37. set Qent=00000000!Qent!
  38. for %%j in (!len!) do set Qent=!Qent:~-%%j!
  39. )
  40. set "%%i=!%%i!!Qent!"
  41. )
  42. set /a "Tree=Size-Free,Treep=Tree*100/Size"
  43. for %%i in (Size Tree Free) do (
  44. set $%%i=&set mod=!%%i!
  45. for /l %%j in (1 1 3) do (
  46. set/a "Qent=mod/1024,mod%%=1024"
  47. set mod=!mod!0
  48. set "$%%i=!$%%i!!Qent!"
  49. )
  50. )
  51. echo %%a%tab%!$size:~,-2!.!$size:~-2! [GB]%tab%!$Tree:~,-2!.!$Tree:~-2! [GB]%tab%!$Free:~,-2!.!$Free:~-2! [GB]%tab%!Treep! %%
  52. )
  53. )
  54. )>D:\BatHome_OS.txt
  55. pause
复制代码
第五题 : 此类问题不熟悉 胡凑一个了
今天网友一问题 提醒了我 可能这题遗漏了一个计划任务
  1. @echo off
  2. for /f "skip=1" %%i in ('"wmic path Win32_LocalTime get DayOfWeek"') do for %%j in (%%i) do set "w=%%i"
  3. set /a "m=(w+6)%%7"
  4. set "bak1=D:\bbs.bathome\Week-Bak_%w%"
  5. for /L %%i in (0,1,%m%) do if exist "D:\bbs.bathome\Week-Bak_%%i\" set "bak2=D:\bbs.bathome\Week-Bak_%%i"
  6. if not defined bak2 set w=0
  7. (for %%i in (.zip .bak \logs\ \img\) do echo %%i)>EXCLUDE
  8. setlocal enabledelayedexpansion
  9. if %w% equ 0 (
  10.    rd /s /q "!bak1!" 2>NUL
  11.    xcopy "D:\www.bathome" "!bak1!" /E /H /R /I /Y /EXCLUDE:EXCLUDE >nul
  12. ) else (
  13. RD /S /Q "!bak1!" 2>NUL
  14. xcopy "!bak2!" "!bak1!" /E /H /R /I /Y >NUL
  15. (xcopy "D:\www.bathome" "!bak1!" /E /H /R /Y /I /D /EXCLUDE:EXCLUDE)>"!bak1!.log"
  16. )
  17. del EXCLUDE 2>NUL
  18. pause
复制代码
第六题
正好原来有个处理大数的 但不处理小数和负数
修改一下 一般测试过
第7行的 set "d=" 应该为 set "d=0" 唉 初始代码就这样 不知怎的改成空值了
修正
  1. @echo off&setlocal enabledelayedexpansion
  2. REM 被除数 和 除数
  3. set str1=123.01
  4. set str2=-33.47
  5. rem 小数点后位数
  6. set u=100
  7. set "d=0"
  8. for %%i in (str1 str2) do if "!%%i:~,1!" == "-" set /a d+=1
  9. if %d% equ 1 (set "d=-") else set "d="
  10. set l=00000000&for /l %%i in (1 1 7) do set "l=!l!!l!"
  11. set "var=4096 2048 1024 512 256 128 64 32 16 8 4 2 1"
  12. for /l %%i in (1 1 2) do (
  13. set "s%%i=!str%%i:-=!"
  14. set "len%%i=0"
  15. for /F "tokens=2 delims=." %%a in ("!s%%i!") do (
  16. set s=%%a&set len%%i=1
  17. for %%b in (%var%) do if "!S:~%%b!" neq "" set/a len%%i+=%%b&set "S=!S:~%%b!"
  18. )
  19. set s%%i=!s%%i:.=!
  20. )
  21. set /a len=len1-len2
  22. if !len1! gtr !len2! (set "s2=!s2!!l:~,%len:-=%!") else set "s1=!s1!!l:~,%len:-=%!"
  23. for /l %%a in (1 1 2) do (
  24. for /f "tokens=* delims=0" %%i in ("!s%%a!") do set s=%%i&set "s%%a=0%%i"
  25. set len%%a=1
  26. for %%j in (%var%) do if "!S:~%%j!" neq "" set/a len%%a+=%%j&set "S=!S:~%%j!"
  27. )
  28. set sun=!l:~-%len2%,-%len1%!
  29. set /a len=len2+1
  30. if "!s1!" Lss "!s2!" (
  31. set s1=!s1:~1!
  32. if !len1! leq !len2! set sun=!sun!0
  33. set /a Len1-=1
  34. )
  35. set "s1=!s1!!l:~,%u%!"
  36. set "s=!s1:~,%len2%!"
  37. set "i=!l:~,7!!s2!"
  38. set /a Len_i=Len2+7,len1+=u
  39. if %u% leq 0 set "u="
  40. if !len1! geq !len2! (
  41. for /l %%i in (1 1 9) do (
  42. set "T=0"
  43. for /l %%j in (8 8 !Len_i!) do (
  44. set /a "T=1!i:~-%%j,8!*%%i+T"
  45. set Num%%i=!T:~-8!!Num%%i!&set /a "T=!T:~,-8!-%%i"
  46. )
  47. set "Num%%i=!T!!Num%%i!"
  48. set "Num%%i=!l:~,7!!Num%%i:~-%Len%!"
  49. )
  50. for /L %%a in (!len2! 1 !Len1!) do (
  51. set "s=!L!!s!!s1:~%%a,1!"
  52. set "s=!s:~-%Len%!"
  53. if "!s!" geq "!s2!" (
  54. set M=5&set "i=!l:~,7!!s!"
  55. if "!i!" geq "!Num5!"  (
  56. for /l %%i in (6 1 9) do if "!i!" geq "!Num%%i!" set "M=%%i"
  57. ) else for /l %%i in (1 1 4) do if "!i!" geq "!Num%%i!" set "M=%%i"
  58. set sun=!sun!!M!&set s=&set T=0
  59. for %%i in (!M!) do (
  60. for /l %%j in (8 8 !Len_i!) do (
  61. set /a "T=3!i:~-%%j,8!-1!Num%%i:~-%%j,8!-!T:~,1!%%2"
  62. set "s=!T:~1!!s!"
  63. )
  64. )
  65. ) else set sun=!sun!0
  66. )
  67. if defined u (set "sun=%d%!sun:~,-%u%!.!sun:~-%u%!")else set sun=%d%!sun!
  68. ) else if defined u (set "sun=%d%0.!l:~,%u%!") else set sun=%d%0
  69. echo !str1! / !str2!  =  !sun!
  70. pause>nul
复制代码
第七题
不确定总系列 还是分类系列 增加个判断吧
  1. @echo off
  2. Rem set /p "url=请输入地址:"
  3. Rem 测试地址1 风景壁纸
  4. Rem set "url=http://www.zhuoku.com/zhuomianbizhi/show/index-1.htm"
  5. Rem 测试地址2 风景壁纸之一  托斯卡纳草原 风光风景宽屏壁纸
  6. set "url=http://www.zhuoku.com/zhuomianbizhi/show-fengjingou/20130731145032.htm"
  7. set "seda=/value=index-/s/.* value=index-([0-9]+).*/\1/p"
  8. set "sedb=s#href=\x22| *class=#\nhttp://www.zhuoku.com#gp"
  9. set "sedc=/\"yema\"/s/.*>\[*([0-9]+)\]*<.*/\1/gp"
  10. set "sedd=/thunder_url/s#.*= *\"(http://.*\.jpg)\";.*#\1#p"
  11. set "wu=http://www.zhuoku.com"
  12. setlocal enabledelayedexpansion
  13. if "!url:/index-=!" neq "!url!" (
  14. for %%i in ("!url!") do set "wurl=!url:%%~nxi=!"
  15. for /f %%j in ('wget -q -O- !url! ^| sed -nr  "!seda!"') do (
  16. (for /l %%j in (1 1 %%j) do echo !wurl:\=/!index-%%j.htm)>url.x
  17. wget -q -O- -i url.x | sed -nr "!sedb!" |sed -nr "s/\.htm\x22$//p" >url.y
  18. )
  19. ) else echo !url!|sed -nr "s/\.htm//p" >url.y
  20. (for /f "tokens=*" %%i in (url.y) do (
  21. for /f %%j in ('wget -q -O- "%%i(1).htm" ^| sed -nr "!sedc!"') do (
  22. for /l %%k in (1 1 %%j) do echo %%i(%%k^).htm
  23. )
  24. ))>url.x
  25. wget -q -O- -i url.x |sed -nr "!sedd!" >url.y
  26. wget -m -q -nH -np --referer="http://www.zhuoku.com" -i url.y
  27. del url.x url.y
  28. pause
复制代码
第八题 修改下  还是去掉临时文件了 效率相差不大
  1. @echo off&setlocal enabledelayedexpansion
  2. %1(for /f "tokens=2" %%i in ('%~s0 Rem ^|sort') do echo %%i)&pause&exit
  3. set "t=零壹贰叁肆伍陆柒捌玖"
  4. set 仟=10&for %%i in (拾 佰) do set %%i=!仟!&set "仟=!仟!0"
  5. set 分=1&set "角=10"
  6. for /f %%a in (a.txt) do (
  7.         set str=%%a
  8.         for /l %%i in (1 1 9) do (
  9.                 for %%j in (!t:~%%i^,1!) do set "str=!str:%%j=%%i!"
  10.         )
  11.         for /f "tokens=1* delims=圆" %%i in ("!str:零=!") do (
  12. if "%%j" neq "" (
  13. set $a=%%i&set "$b=%%j"
  14. ) else set $a=0&set "$b=%%i"
  15. )
  16.         for %%j in (角 分) do set "$b=!$b:%%j=*%%j+!"
  17.         set /a $b=!$b!+100,$=0
  18.         if "!$a!" == "!$a:万=!" (set i=1) else set i=10000
  19. for %%i in (!$a:万^= !) do (
  20.                set "$a=%%i"
  21. for %%j in (拾 佰 仟) do set "$a=!$a:%%j=*%%j+!+0"
  22. set /a "$+=(!$a!)*i"
  23. set i=1
  24.         )
  25.         set $=00000000!$!.!$b:~-2!
  26.         echo !$:~-11! %%a
  27. )
复制代码
第九题
最终用了 FC 比较 方案
这里取一个文件比较    多几个文件做比较 也许效率看提升点
  1. @echo off
  2. SET PH=f:
  3. dir /s /a-d /o:n %PH% >dir_a.t
  4. (for /f "skip=2 tokens=*" %%i in (dir_a.t) do (
  5.         set "str=%%i"
  6.         setlocal enabledelayedexpansion
  7.         if exist "!str:~,-4!\" (
  8.                 for /f "tokens=*" %%j in ("!str:~,-4!") do endlocal&set "files=%%j"
  9.         ) else  endlocal&for /f "tokens=1-3*" %%a in ("%%i") do if "%%b%%d" == "个文件字节" (
  10.                 setlocal enabledelayedexpansion
  11.                 for /f "tokens=1-3*" %%a in ("%%i") do echo !file!:%%a:!files!
  12.                 endlocal
  13.         ) else set "file=%%d"
  14. ))>dir_b.t
  15. findstr /ibv ":" dir_b.t|sort >dir_a.t
  16. set /a i=n=1
  17. for /f "tokens=1-2* delims=:" %%a in (dir_a.t) do (
  18.         set "str=%%a:%%b"
  19.         setlocal enabledelayedexpansion
  20.         if /i "!s!" neq "!str!" (
  21.                 if !i! gtr 1 set /a n+=1
  22.                 set i=1
  23.         ) else set /a "i+=1"
  24.         for /f "tokens=1,2" %%i in ("!n! !i!") do endlocal&set n=%%i&set i=%%j&set _%%i=%%j&set "%%i_%%j=%%c"
  25.         set "s=%%a:%%b"
  26. )
  27. setlocal enabledelayedexpansion
  28. (for /l %%i in (1 1 %N%) do (
  29.         for /l %%a in (1 1 !_%%i!) do (
  30.                 for /f "tokens=*" %%m in ("!%%i_%%a!") do (
  31.                         set /a i=%%a+1
  32.                         dir /b /a-d /on "!%%i_%%a!" >dir_a.t
  33.                         for /l %%b in (!i! 1 !_%%i!) do (
  34.                                 for /f "tokens=*" %%n in ("!%%i_%%b!") do (
  35.                                         dir /b /a-d /on "!%%i_%%b!" >dir_b.t
  36. fc /c dir_a.t dir_b.t>nul&&(
  37. for /f "tokens=*" %%o in ("!%%i_%%a!") do (
  38.                                                         echo !%%i_%%a!&type dir_a.t&echo;
  39.                                                         set %%i_%%a=
  40.                                                 )
  41.                                                 echo !%%i_%%b!&type dir_b.t&echo;
  42.                                                 set %%i_%%b=
  43. )
  44.                                 )
  45.                         )
  46.                 )
  47.         )
  48. ))>dir_list.txt
  49. del dir_a.t dir_b.t
  50. start "" dir_list.txt
  51. pause
复制代码
findstr 搜索 方案
相对FCf方案来说  FINDSTR 搜索字符有限制外  还有FINDSTR还有字符串搜索问题
用FIND 替换 FINDSTR 可以解决 但效率
  1. @echo off
  2. SET PH=f:
  3. dir /s /a-d /o:n %PH% >dir_a.t
  4. (for /f "skip=2 tokens=*" %%i in (dir_a.t) do (
  5. set "str=%%i"
  6. setlocal enabledelayedexpansion
  7. if exist "!str:~,-4!\" (
  8. for /f "tokens=*" %%j in ("!str:~,-4!") do endlocal&set "files=%%j"
  9. ) else  endlocal&for /f "tokens=1-3*" %%a in ("%%i") do if "%%b%%d" == "个文件字节" (
  10. setlocal enabledelayedexpansion
  11. for /f "tokens=1-3*" %%a in ("%%i") do echo !file!:%%a:!files!
  12. endlocal
  13. ) else set "file=%%d"
  14. ))>dir_b.t
  15. findstr /ibv ":" dir_b.t|sort >dir_a.t
  16. set /a i=n=1
  17. for /f "tokens=1-2* delims=:" %%a in (dir_a.t) do (
  18. set "str=%%a:%%b"
  19. setlocal enabledelayedexpansion
  20. if /i "!s!" neq "!str!" (
  21. if !i! gtr 1 set /a n+=1
  22. set i=1
  23. ) else set /a "i+=1"
  24. for /f "tokens=1,2" %%i in ("!n! !i!") do endlocal&set n=%%i&set i=%%j&set _%%i=%%j&set "%%i_%%j=%%c"
  25. set "s=%%a:%%b"
  26. )
  27. setlocal enabledelayedexpansion
  28. (for /l %%i in (1 1 %N%) do (
  29. for /l %%a in (1 1 !_%%i!) do (
  30. for /f "tokens=*" %%m in ("!%%i_%%a!") do (
  31. set /a i=%%a+1
  32. dir /b /a-d "!%%i_%%a!" >dir_a.t
  33. for /l %%b in (!i! 1 !_%%i!) do (
  34. for /f "tokens=*" %%n in ("!%%i_%%b!") do (
  35. dir /b /a-d "!%%i_%%b!" >dir_b.t
  36. findstr /ivxg:dir_a.t dir_b.t>dir_c.t>nul||findstr /ivxg:dir_b.t dir_a.t>nul||(
  37. for /f "tokens=*" %%o in ("!%%i_%%a!") do (
  38.                                                  echo !%%i_%%a!&type dir_a.t&echo;
  39. set %%i_%%a=
  40. )
  41. echo !%%i_%%b!&type dir_b.t&echo;
  42. set %%i_%%b=
  43. )
  44. )
  45. )
  46. )
  47. )
  48. ))>dir_list.txt
  49. del dir_a.t dir_b.t
  50. start "" dir_list.txt
  51. pause
复制代码
第九题 增加一个临时文件方案3 如文件夹数量大的情况 增加个临时文件
  1. @echo off
  2. SET PH=f:
  3. dir /s /a-d /o:n %PH% >dir_a.t
  4. (for /f "skip=2 tokens=*" %%i in (dir_a.t) do (
  5.         set "str=%%i"
  6.         setlocal enabledelayedexpansion
  7.         if exist "!str:~,-4!\" (
  8.                 for /f "tokens=*" %%j in ("!str:~,-4!") do endlocal&set "files=%%j"
  9.         ) else  endlocal&for /f "tokens=1-3*" %%a in ("%%i") do if "%%b%%d" == "个文件字节" (
  10.                 setlocal enabledelayedexpansion
  11.                 for /f "tokens=1-3*" %%a in ("%%i") do echo !file!:%%a:!files!
  12.                 endlocal
  13.         ) else set "file=%%d"
  14. ))>dir_b.t
  15. findstr /inbv ":" dir_b.t >dir_a.t
  16. (for /f "tokens=1-3* delims=:" %%a in (dir_a.t) do (
  17. if not defined $%%a (
  18. dir /b /a-d /on "%%d" >dir_b.t
  19. set flag=a
  20. findstr /ic:":%%b:%%c:" dir_a.t>$
  21. for /f "tokens=1-3* delims=:" %%A in ($) do (
  22. if %%A gtr %%a (
  23. if not defined $%%A (
  24. dir /b /a-d /on "%%D" >dir_c.t
  25. fc /c /L dir_b.t dir_c.t>nul&&(
  26. if defined flag set flag=&echo %%d&type dir_b.t&echo;
  27. echo %%D&type dir_c.t&echo;
  28. set $%%A=a
  29. )
  30. )
  31. )
  32. )
  33. )
  34. ))>dir_list.txt
  35. del $ dir_a.t dir_b.t dir_c.t 2>nul
  36. start "" dir_list.txt
  37. pause
复制代码
第十题
  1. @echo off
  2. findstr /ivx "图[0-9]*\.[0-9]*" a.txt>_temp.t
  3. findstr /ivx "图[0-9]*\.[0-9]*、.*图[0-9]*\.[0-9]*" _temp.t>html.txt
  4. set "sp=&nbsp;&nbsp;&nbsp;&nbsp;"
  5. set "br=<br />"
  6. set "ims=一二三四五六七八九十百  "
  7. (findstr /ib "<[^/]" %~s0
  8. for /f "delims=" %%i in ('findstr /in .* html.txt') do (
  9. set "str=%%i"
  10. setlocal enabledelayedexpansion
  11.         set "str=!str:*:=!"
  12. if defined str (
  13. for /f "tokens=1,2 delims=%ims%" %%a in ("!str!") do (
  14. if "%%a%%b" == "第章"  (
  15. if "!str:~,2!" == "第章" (
  16. echo;!br!!sp!!str!!br!
  17. ) else echo;!sp!!str!!br!
  18. ) else echo;!sp!!str!!br!
  19. )
  20. ) else echo !br!
  21. endlocal
  22. )
  23. findstr /ib "</" %~s0
  24. )>a.html
  25. del html.txt _temp.t
  26. exit
  27. <html>
  28. <body>
  29. </body>
  30. </html>
复制代码

TOP

返回列表