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