Board logo

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

作者: chreng    时间: 2020-9-19 14:28     标题: 求助批处理随机抽取文件夹中的10个文件名写入到txt文件并执行命令

本帖最后由 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、循环的次数在开始前提示输入次数

请问批处理能实现吗,恳请大神帮忙,感恩。。。
作者: Batcher    时间: 2020-9-20 09:03

回复 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. )
复制代码

作者: 不会飞的鱼    时间: 2020-9-26 13:38

   非常感谢,非常好用!




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2