找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 26980|回复: 2

[文本处理] 求助批处理随机抽取文件夹中的10个文件名写入到txt文件并执行命令

[复制链接]
发表于 2020-9-19 14:28:32 | 显示全部楼层 |阅读模式
本帖最后由 chreng 于 2020-9-19 14:35 编辑

out.txt文件内容格式:
file './03.txt'
file './25.txt'
file './29.txt'
file './57.txt'

1、要求在当前目录的所有文件当中,随机把10个文件名按照上面的格式写入out.txt文档中;
2、执行命令块 copy /b out.txt d:\a.txt end.txt;
3、返回第一步,清空out.txt,又重新随机抽取10个文件名写入out.txt文档中,执行第二步,反复循环;
4、循环的次数在开始前提示输入次数

请问批处理能实现吗,恳请大神帮忙,感恩。。。
发表于 2020-9-20 09:03:46 | 显示全部楼层
回复 1# chreng


请参考Q-04把bat文件保存为ANSI编码:
https://mp.weixin.qq.com/s/6lbb97qUOs1sTyKJfN0ZEQ
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "FileNum=10"
  4. set "LoopNum=3"
  5. set "LoopCount=1"

  6. set /p LoopNum=输入循环次数:

  7. :BeginLoop
  8. for /f "delims=" %%i in ('dir /b /a-d *.txt ^| findstr /v /x /c:"out.txt" /c:"end.txt"') do (
  9.     set "_!random!=%%i"
  10. )

  11. set "FileCount=0"
  12. (for /f "tokens=2 delims==" %%i in ('set _') do (
  13.     set /a FileCount+=1
  14.     if !FileCount! leq %FileNum% (
  15.         echo file './%%i'
  16.     )
  17. ))>"out.txt"
  18. copy /b "out.txt"+"D:\a.txt" end.txt

  19. echo 已完成第 !LoopCount! 次随机抽取及合并,请检查文件 end.txt
  20. pause
  21. set /a LoopCount+=1
  22. if !LoopCount! leq %LoopNum% (
  23.     goto :BeginLoop
  24. )
复制代码

评分

参与人数 1技术 +1 收起 理由
chreng + 1 感谢

查看全部评分

发表于 2020-9-26 13:38:25 | 显示全部楼层
   非常感谢,非常好用!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 07:18 , Processed in 0.019874 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表