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

[文本处理] [已解决]背景的批处理如何随机抽取文本中的指定行?

标题够醒目了 方便大家查找
第一步  搜索批处理所在目录里的图片文件 暂定为 *.PNG 类型且文件不只一个 即可以多个  然后 输出一个包含有图片完整路径的 BGlist.txt 文件列表 方便第三部的提取
第一步搞定
  1. @echo off&setlocal enabledelayedexpansion
  2. if exist BGList.txt del /f /q /a BGList.txt
  3. for /f "tokens=*" %%i in ('dir /a /b /s "*.PNG"') do echo "%%~fi" >>BGList.txt
  4. exit
复制代码
第二步  搜索全盘 不包含desktop.ini文件的目录 并导出列表FList.txt (注:即使某个文件夹里有 desktop.ini 这个文件 但是这个文件夹的子文件夹里没有 desktop.ini 这个文件 也仍然输出 子文件夹的完整路径 而不输出有 desktop.ini 文件的文件夹路径)
第二步搞定
  1. @echo off&setlocal enabledelayedexpansion
  2. if exist Flist.txt del /f /q /a Flist.txt
  3. for /f "skip=4" %%i in ('mountvol ^| find ":"') do (for /f "delims=" %%j in ('dir /s /b /ad "%%i" 2^>nul') do if not exist %%j\desktop.ini echo "%%j">>Flist.txt)
  4. exit
复制代码
第三步  把第二步导出的文本里 所有文件夹都加上 系统属性 即 (attrib +s "路径") 然后在所有的文件夹里 输出一个 desktop.ini 文件 文件输出完后 加上 attrib +s +h +r 属性    内容为
  1. [{BE098140-A513-11D0-A3A4-00C04FD706EC}]
  2. IconArea_Image=从第一步输出的BGList.txt图片路径中 随机提取一行 写入=号后面
复制代码
第四步  工作到此基本完了   现在的工作是做一个逆向操作的批处理   即  取消第三步设置的文件夹的系统属性  attrib -s     然后 删除第三步输出的 desktop.ini 文件      第四步可以调用第二步输出的FList.txt文本

第四步搞定
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in (FList.txt) do if exist %%i\desktop.ini del /f /a /q %%i\desktop.ini & attrib -s %%i
  3. exit
复制代码
这样全盘文件都有背景了

[ 本帖最后由 hmlstfqtl 于 2010-4-22 23:56 编辑 ]

至此 终于结贴了。。谢谢各位的帮助 特别是寒星

这里发一个成品 谢谢大家 可以测试下     一切操作可逆 不会影响电脑      图片可以自己添加     推荐使用 快速 版本

这里就把成品给大家发出来

下载链接

http://www.uushare.com/user/gkass/file/2909732

下面是附件说明
  1. 运行文件夹里的 !)生成.bat 即可在全盘文件夹里显示图片
  2. 运行过程切勿关闭运行窗口。
  3. 一切操作都是可逆的。
  4. 当不想要显示图片的时候 再运行一次 !)生成.bat 或者 !)删除.bat 即可
  5. 至于 !)快速.bat 是后面添加的  只搜索到1层目录
  6. 速度当然会很快 而且对系统的稳定不会有影响了
复制代码
华丽的分割线。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
===============================================================================================

下面贴上成品的批处理内容
  1. !)快速.bat
  2. @echo off&setlocal enabledelayedexpansion
  3. if exist "勿删.txt" (goto back) else (goto start)
  4. :back
  5. cls
  6. echo 现在开始删除生成的文件,操作期间一定不要关闭本窗口。
  7. echo 如本机文件夹过多的话,操作的时间就会较长,请耐心等待。谢谢!
  8. for /f "delims=" %%a in (勿删.txt) do if exist "%%a"desktop.ini del /f /a /q "%%a"desktop.ini & attrib -s %%a
  9. for /f "delims=" %%i in (勿删.txt) do if exist %%i\desktop.ini del /f /a /q %%i\desktop.ini & attrib -s %%i
  10. del /f /q /a 勿删.txt
  11. goto xing
  12. :start
  13. cls
  14. echo 如本机文件夹过多的话,操作的时间就会较长,请耐心等待。谢谢!
  15. for /f "skip=4" %%a in ('mountvol ^| find ":"') do if not exist %%a\desktop.ini echo %%a>>勿删.txt
  16. for /f "skip=4" %%i in ('mountvol ^| find ":"') do (for /f "delims=" %%j in ('dir /b /ad "%%i" 2^>nul') do if not exist %%i\desktop.ini echo "%%i%%j">>勿删.txt)
  17. for /f "delims=" %%i in ('dir /a /b /s "*.PNG"') do call:han "%%~fi"
  18. for /F "tokens=1,2* delims==:" %%i in ('set han_^|findstr /n .*') do set "BG_%%i=%%k"&set ye_BG=%%i
  19. for /f "delims=" %%i in (勿删.txt) do set "xing_%%~i=="
  20. for /F "tokens=1,2* delims==:_" %%i in ('set xing_^|findstr /n .*') do set a=%%k&set "Fl_%%i=!a:~0,-2!"&set ye_Fl=%%i
  21. for /l %%i in (1,%ye_BG%,%ye_Fl%) do for /l %%j in (1,1,%ye_BG%) do set/a "gu=%%i-1+%%j"&call set "gu=%%Fl_!gu!%%"&if not defined gu (goto :xing) else (attrib +s "!gu!"
  22. >>"!gu!\Desktop.ini" echo.[{BE098140-A513-11D0-A3A4-00C04FD706EC}]
  23. >>"!gu!\Desktop.ini" echo.IconArea_Image=!BG_%%j!
  24. attrib +s +h +r "!gu!\Desktop.ini"
  25. )
  26. :xing
  27. exit
  28. :han
  29. set han=%random%%random%%random%
  30. if "!han_%han%!"=="" (set "han_%han%=%~1") else goto:han
复制代码
  1. !)生成.bat
  2. @echo off&setlocal enabledelayedexpansion
  3. if exist "勿删.txt" (goto back) else (goto start)
  4. :back
  5. cls
  6. echo 现在开始删除生成的文件,操作期间一定不要关闭本窗口。
  7. echo 如本机文件夹过多的话,操作的时间就会较长,请耐心等待。谢谢!
  8. for /f "delims=" %%a in (勿删.txt) do if exist "%%a"desktop.ini del /f /a /q "%%a"desktop.ini & attrib -s %%a
  9. for /f "delims=" %%i in (勿删.txt) do if exist %%i\desktop.ini del /f /a /q %%i\desktop.ini & attrib -s %%i
  10. del /f /q /a 勿删.txt
  11. goto xing
  12. :start
  13. cls
  14. echo 如本机文件夹过多的话,操作的时间就会较长,请耐心等待。谢谢!
  15. for /f "skip=4" %%a in ('mountvol ^| find ":"') do if not exist %%a\desktop.ini echo %%a>>勿删.txt
  16. for /f "skip=4" %%i in ('mountvol ^| find ":"') do (for /f "delims=" %%j in ('dir /s /b /ad "%%i" 2^>nul') do if not exist %%j\desktop.ini echo "%%j">>勿删.txt)
  17. for /f "delims=" %%i in ('dir /a /b /s "*.PNG"') do call:han "%%~fi"
  18. for /F "tokens=1,2* delims==:" %%i in ('set han_^|findstr /n .*') do set "BG_%%i=%%k"&set ye_BG=%%i
  19. for /f "delims=" %%i in (勿删.txt) do set "xing_%%~i=="
  20. for /F "tokens=1,2* delims==:_" %%i in ('set xing_^|findstr /n .*') do set a=%%k&set "Fl_%%i=!a:~0,-2!"&set ye_Fl=%%i
  21. for /l %%i in (1,%ye_BG%,%ye_Fl%) do for /l %%j in (1,1,%ye_BG%) do set/a "gu=%%i-1+%%j"&call set "gu=%%Fl_!gu!%%"&if not defined gu (goto :xing) else (attrib +s "!gu!"
  22. >>"!gu!\Desktop.ini" echo.[{BE098140-A513-11D0-A3A4-00C04FD706EC}]
  23. >>"!gu!\Desktop.ini" echo.IconArea_Image=!BG_%%j!
  24. attrib +s +h +r "!gu!\Desktop.ini"
  25. )
  26. :xing
  27. exit
  28. :han
  29. set han=%random%%random%%random%
  30. if "!han_%han%!"=="" (set "han_%han%=%~1") else goto:han
复制代码
  1. !)删除.bat
  2. @echo off&setlocal enabledelayedexpansion
  3. echo 如本机文件夹过多的话,操作的时间就会较长,请耐心等待。谢谢!
  4. echo 显示的错误也属于正常的,不必担心本操作失误造成系统出现问题。
  5. echo 运行窗口一定不要关闭,切记。
  6. del /f /q /a 勿删.txt >nul 2>nul
  7. for /f "skip=4" %%a in ('mountvol ^| find ":"') do for /f "tokens=*" %%i in ('dir /s /ashr /b "%%a"desktop.ini') do echo %%~dpi>>勿删.txt
  8. for /f "delims=" %%a in (勿删.txt) do if exist "%%a"desktop.ini del /f /a /q "%%a"desktop.ini & attrib -s %%a
  9. for /f "delims=" %%i in (勿删.txt) do if exist %%i\desktop.ini del /f /a /q %%i\desktop.ini & attrib -s %%i
  10. del /f /q /a 勿删.txt
  11. exit
复制代码

[ 本帖最后由 hmlstfqtl 于 2010-4-23 08:44 编辑 ]

TOP

发觉寒夜兄的毛病,   一句批处理, 只要能用复合语连起来, 就会连得老长,  打死也不肯分行,   给别人造成了阅读困难.

  所以十五楼兄台没一下看全哈哈,   
           

              而寒夜兄的毛病我也有,而且一样严重, 所以我看清了○( ̄﹏ ̄)○
    ╭╩═╮
╭╯G O ╠ 雁过留声
╰⊙═⊙╯

TOP

原帖由 qzwqzw 于 2010-4-20 22:16 发表
孤星的代码看的头大
大概明白那句两层嵌套的for/l是什么意思了
就我的看法
这种场合下使用嵌套for/l
不能做到完全随机选取图片
不如使用for/l+set !random!
for /l %%i in (1,,%ye_Fl%)  do (
    set /a i_bg=!random! %% %ye_BG% + 1
    call set BG=%%BG_!i_bg!%%
    ...
    >>"!gu!\Desktop.ini" echo.IconArea_Image=!BG!
)

图片的乱序排列在标签:han中完成的,所以for /l里就可以按顺序读了。
至于用双for /l嵌套是因为他要求,要循环完所有图片一次,才能再开始第二次。显然这里!random!就不合适了。因为他把这个要求写到另要一个帖子里了http://bbs.bathome.net/thread-7691-1-2.html,你没看到,哈哈。
寒夜孤星:在没有说明的情况下,本人所有代码均运行在 XP SP3 下 (有问题请发贴,QQ临时会话已关闭)

TOP

孤星的代码看的头大
大概明白那句两层嵌套的for/l是什么意思了
就我的看法
这种场合下使用嵌套for/l
不能做到完全随机选取图片
不如使用for/l+set !random!
for /l %%i in (1,,%ye_Fl%)  do (
    set /a i_bg=!random! %% %ye_BG% + 1
    call set BG=%%BG_!i_bg!%%
    ...
    >>"!gu!\Desktop.ini" echo.IconArea_Image=!BG!
)
天的白色影子

TOP

for hanyeguxing

兄台  这学了一招:    for  /l  %%i  (1, num1, num2)  do  for /l  %%j  (1,1,num1)  do set /a *=%%i-1+%%j  &if *


    好聪明 的方法呀,  …(⊙_⊙;)…○圭~○列

   劳记在心, 感激涕零,  谢谢  "寒夜孤星" 的智慧   ○( ̄﹏ ̄)○
    ╭╩═╮
╭╯G O ╠ 雁过留声
╰⊙═⊙╯

TOP

原帖由 hmlstfqtl 于 2010-4-19 22:25 发表
你的回复 我运行后 输出的文件是 下面的样子


[{be098140-a513-11d0-a3a4-00c04fd706ec}]
iconarea_image=

如果你觉得加QQ弄比较好交流也方便的话 可以随时加我 QQ 394441501


     怪了,  我就按你发的加我写的第三步运行, 没一点问题呀,       我知道 你是哪里出错了, 应当是,  goto :eof  这里出了错,  我第三步没有子程序 , 所以用了的是GOTO :eof      而你第一二步有 冒号开头的子程序,  所以出错吧
    ╭╩═╮
╭╯G O ╠ 雁过留声
╰⊙═⊙╯

TOP

我 编的 东西 目前跟你的没法比-_-!

TOP

  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%i in ('dir /a-d /b /s "*.png"') do call:han "%%~fi"
  3. for /F "tokens=1,2* delims==:" %%i in ('set han_^|findstr /n .*') do set "BG_%%i=%%k"&set ye_BG=%%i
  4. for /f "skip=4" %%i in ('mountvol^|find ":"') do for /f "delims=" %%j in ('dir /s /b /ad "%%i" 2^>nul') do if not exist "%%j\desktop.ini" set "xing_%%j=="
  5. for /F "tokens=1,2* delims==:_" %%i in ('set xing_^|findstr /n .*') do set a=%%k&set "Fl_%%i=!a:~0,-2!"&set ye_Fl=%%i
  6. for /l %%i in (1,%ye_BG%,%ye_Fl%) do for /l %%j in (1,1,%ye_BG%) do set/a "gu=%%i-1+%%j"&call set "gu=%%Fl_!gu!%%"&if not defined gu (goto :xing) else (attrib +s "!gu!"
  7. >>"!gu!\Desktop.ini" echo.[{BE098140-A513-11D0-A3A4-00C04FD706EC}]
  8. >>"!gu!\Desktop.ini" echo.IconArea_Image=!BG_%%j!
  9. attrib -s "!gu!")
  10. :xing
  11. exit
  12. :han
  13. set han=%random%%random%%random%
  14. if "!han_%han%!"=="" (set "han_%han%=%~1") else goto:han
复制代码
1,本批处理不产生临时文件,所需内容全部定义为变量
2,由于大量操作,所以批处理中尽量少使用set/a以加快运行速度

[ 本帖最后由 hanyeguxing 于 2010-4-20 06:37 编辑 ]
1

评分人数

寒夜孤星:在没有说明的情况下,本人所有代码均运行在 XP SP3 下 (有问题请发贴,QQ临时会话已关闭)

TOP

你的回复 我运行后 输出的文件是 下面的样子


[{be098140-a513-11d0-a3a4-00c04fd706ec}]
iconarea_image=

如果你觉得加QQ弄比较好交流也方便的话 可以随时加我 QQ 394441501

TOP

上面是目录

D:\bg\BGList.bat
D:\bg\FList.bat
D:\bg\Photos
D:\bg\back.bat
D:\bg\FList.txt
D:\bg\BGList.txt
D:\bg\有问题的那个批处理.bat
        D:\bg\Photos\1 (1).PNG
        D:\bg\Photos\1 (2).png
        D:\bg\Photos\1 (3).png
        D:\bg\Photos\1 (4).png
        D:\bg\Photos\1 (5).png
        D:\bg\Photos\1 (6).PNG
        D:\bg\Photos\1 (7).PNG
        D:\bg\Photos\1 (8).PNG
        D:\bg\Photos\1 (9).png
        D:\bg\Photos\1 (10).png
        D:\bg\Photos\1 (11).png
        D:\bg\Photos\1 (12).png
        D:\bg\Photos\1 (13).png
        D:\bg\Photos\1 (14).png
        D:\bg\Photos\1 (15).png
        D:\bg\Photos\1 (16).PNG
        D:\bg\Photos\1 (17).PNG
        D:\bg\Photos\1 (18).png
        D:\bg\Photos\1 (19).png
        D:\bg\Photos\1 (20).png
        D:\bg\Photos\1 (21).png
        D:\bg\Photos\1 (22).png
        D:\bg\Photos\1 (23).png
        D:\bg\Photos\1 (24).png
        D:\bg\Photos\1 (25).PNG

TOP

真的很感谢   哈哈 是我发的

TOP

我不忍心给硬盘做这样的处理,文件夹太多了,还不如去下载一个文件夹背景设置软件,不过楼主积极探索的精神是我等要学习的。我顶!!

TOP

原帖由 hmlstfqtl 于 2010-4-19 17:33 发表
if !n! gtr 1 goto lp      同     if %n% 大于1 goto lp



最好带上引号   有时后  文件编码 不同, 也出错

  if  "*"  gtr "*"  ****
    ╭╩═╮
╭╯G O ╠ 雁过留声
╰⊙═⊙╯

TOP

楼主你好像在别的论坛发了这贴我回复了呀

  1. @echo off
  2. ::判定 图片文本 的行数
  3. for /f  "delims=" %%i in ('find /c /v /n "" BGList.txt) do for %%j in (%%i) do set v=%%j
  4. ::提供小于 图片文本行数的NUM值
  5. set  num=%random%%%%v%
  6. ::为 for 命令, SKIP 设定值, 为文本随机跳跃提供支持,  并修复 for 命令  SKIP=0  时报错并退出CMD情况的出现
  7. set "skip=skip=%num% "&&echo.%num%|find "0" >nul&&set skip=
  8. :: 随机设定图片值
  9. for  /f  "%skip%delims="  %%i  in  (BGList.txt) do set "jpg=%%i"&& goto :eof
  10. ::生成DESKTOP.INI
  11. for /f "delims=" %%j in (FList.txt) do (echo [{be098140-a513-11d0-a3a4-00c04fd706ec}]>>"%%j\desktop.ini") & (echo iconarea_image=%jpg%>>"%%j\desktop.ini") & (attrib +s %%j)
  12.    )
复制代码
这个是不是你发的         另记得加点分喔( ̄Q ̄)╯

[ 本帖最后由 x9tiancmd 于 2010-4-19 21:12 编辑 ]
    ╭╩═╮
╭╯G O ╠ 雁过留声
╰⊙═⊙╯

TOP

返回列表