Board logo

标题: [其他] 【已解决】批处理type中,循环语句如何表示? [打印本页]

作者: 思想之翼    时间: 2024-9-11 09:33     标题: 【已解决】批处理type中,循环语句如何表示?

D:\DATA\文件夹中有500个文本,分别命名为1.txt 2.txt...500.txt

欲1.txt 与 2.txt合并,3.txt 与 4.txt合并......499.txt 与 500.txt合并

type D:\DATA\1.txt > D:\DATA2\1.txt
type D:\DATA\2.txt >> D:\DATA2\1.txt

type D:\DATA\3.txt > D:\DATA2\2.txt
type D:\DATA\4.txt >> D:\DATA2\2.txt
......
type D:\DATA\499.txt > D:\DATA2\250.txt
type D:\DATA\500.txt >> D:\DATA2\250.txt

@echo off
setlocal enabledelayedexpansion
for /l %%f in (1,1,250) do (
    set q=%%f
    type D:\DATA\!q!*2-1.txt > D:\DATA2\!q!.txt
    type D:\DATA\!q!*2.txt >> D:\DATA2\!q!.txt
)
endlocal
exit

上述代码标红处,如何正确表示?
作者: qixiaobin0715    时间: 2024-9-11 09:58

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. md "D:\DATA2" 2>nul
  4. cd /d "D:\DATA"
  5. for /l %%i in (1,2,499) do (
  6.     set /a m+=1
  7.     set /a n=%%i+1
  8.     type "%%i.txt" "!n!.txt" 2>nul>"D:\DATA2\!m!.txt"
  9. )
  10. pause
复制代码

作者: aloha20200628    时间: 2024-9-11 10:53

本帖最后由 aloha20200628 于 2024-9-11 10:59 编辑

回复 1# 思想之翼
  1. @echo off &setlocal enabledelayedexpansion
  2. for /l %%n in (1,1,250) do (
  3. set/a "n2=2*%%n,n1=n2-1"
  4. type "d:\data\!n1!.txt" "d:\data\!n2!.txt">"d:\data2\%%n.txt"
  5. ) 2>nul
  6. endlocal&pause&exit/b
复制代码

作者: 77七    时间: 2024-9-11 12:23

  1. @echo off
  2. cd /d "d:\data"
  3. setlocal enabledelayedexpansion
  4. for /l %%l in (1,2,500) do (
  5. set /a n=%%l+1,m+=1
  6. > "d:\data2\!m!.txt" (
  7. find /v "" < "%%l.txt"
  8. find /v "" < "!n!.txt"
  9. )
  10. )
  11. endlocal
  12. pause
复制代码





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