bat读取txt最后一行
我要先进行判断文件是否存在,但似乎有问题,命令实效了,哪里出错了呢ifexistq:\0310-%date:~0,4%%date:~5,2%%date:~8,2%.txt...
我要先进行判断文件是否存在,但似乎有问题,命令实效了,哪里出错了呢
if exist q:\0310-%date:~0,4%%date:~5,2%%date:~8,2%.txt.rpt. for /f "tokens=*" %%i in (q:\0310-%date:~0,4%%date:~5,2%%date:~8,2%.txt.rpt) do set str=%%i do echo 集中开卡结果:%str%>>%date:~0,4%%date:~5,2%%date:~8,2%rpt.txt 展开
if exist q:\0310-%date:~0,4%%date:~5,2%%date:~8,2%.txt.rpt. for /f "tokens=*" %%i in (q:\0310-%date:~0,4%%date:~5,2%%date:~8,2%.txt.rpt) do set str=%%i do echo 集中开卡结果:%str%>>%date:~0,4%%date:~5,2%%date:~8,2%rpt.txt 展开
1个回答
展开全部
要引用在循环中动态赋值的变量,需要开启“延迟扩展”,然后用两个感叹号代替百分号来访问变量,修改并优化如下:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET SourceFile=q:\0310-%date:~0,4%%date:~5,2%%date:~8,2%.txt.rpt
SET TargetFile=%date:~0,4%%date:~5,2%%date:~8,2%rpt.txt
if exist %SourceFile% (
for /f "tokens=*" %%i in (%SourceFile%) do (
set str=%%i
echo 集中开卡结果:!str!>>%TargetFile%
)
)
ENDLOCAL
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET SourceFile=q:\0310-%date:~0,4%%date:~5,2%%date:~8,2%.txt.rpt
SET TargetFile=%date:~0,4%%date:~5,2%%date:~8,2%rpt.txt
if exist %SourceFile% (
for /f "tokens=*" %%i in (%SourceFile%) do (
set str=%%i
echo 集中开卡结果:!str!>>%TargetFile%
)
)
ENDLOCAL
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询