Board logo

标题: [文件操作] 批处理读取TXT内容批量执行python的问题 [打印本页]

作者: zcwslj    时间: 2020-5-21 02:25     标题: 批处理读取TXT内容批量执行python的问题

本帖最后由 zcwslj 于 2020-5-21 02:28 编辑

新手一枚,请大虾帮忙!!!

a.txt
hello1,hello2

@echo off
setlocal enabledelayedexpansion
set py_exe="D:\Python37\pythonw.exe"
set cmd1="D:\test\10.py"
for /f "tokens=1,2 delims=," %%i in (D:\test\a.txt) do set hello1=%%i & set hello2=%%j
start /wait %py_exe% %cmd1% %hello1%
start /wait %py_exe% %cmd1% %hello2%
pause>nul

以上a.txt只有一行内容,假如有未知的多行内容呢,该如何去循环?
作者: zcwslj    时间: 2020-5-21 10:34

大佬指点一下呀
作者: smss    时间: 2020-5-21 12:54

本帖最后由 smss 于 2020-5-21 13:03 编辑
  1. for /f "delims=" %%i in (D:\test\a.txt) do (set "hello=%%i"
  2. start /wait %py_exe% %cmd1% %hello%
  3. )
复制代码
同理把%cmd1%也写成这种就可以套嵌 节约大量类似重复代码
例如 *.py和a.txt需保持顺序一致
  1. @echo off
  2. for /r "D:\test" %%a in (*.py) do (for /f "delims=" %%b in (D:\test\a.txt) do (
  3. start /wait "D:\Python37\pythonw.exe" "%%a" "%%b"
  4. ))
复制代码

作者: zcwslj    时间: 2020-5-22 23:58

回复 3# smss


    感谢,我测下,有不懂的地方再请教
作者: zcwslj    时间: 2020-5-24 01:00

本帖最后由 zcwslj 于 2020-5-24 01:15 编辑

回复 3# smss


十分感谢,用了你的方法,经测试,成功运行了!因为只需一个PYTHON文件,所以没用/r
a.txt:
hello,zcw
888,666

批处理,跟据a.txt内容生成hello,zcw,888,666名称的TXT文件:
@echo off
setlocal enabledelayedexpansion
set py_exe="D:\Python37\pythonw.exe"
set cmd1="D:\test\10.py"
for /f "tokens=1,2 delims=," %%i in (D:\test\a.txt) do (
start /wait %py_exe% %cmd1% "%%i"
start /wait %py_exe% %cmd1% "%%j"
)
pause>nul




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