如何使其仅从 .txt 文件中的第一行获取令牌,而不是循环遍历每一行。我希望将 %%m 分配给第一行的第三个标记,然后停止。
@echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
pause
)
)
pause
set /p
可以用来读取第一行,然后你可以使用FOR /F循环来获取第三个token
setlocal EnableDelayedExpansion
FOR %%A IN (%1) DO (
<%%A set /p firstline=
FOR /F "tokens=3 delims=," %%m IN ("!firstline!") DO (
echo %%m
)
)
如果没有看到eg文件并且确切地知道你想要做什么,我无法测试这个,但是这里列出了
firstline.bat
,它应该可以满足你的要求:)起初我认为这需要是比它更复杂......在你第一次调用之后,只需使用 goto 退出 for 结构 - 问题解决了吗?
@echo off
::: firstline.bat - Retrieve the first line from a series of files
::: usage: firstline $filespec
::: filespace - files to process (eg .\xdrive\*.txt)
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
FOR %%A IN (%1) DO (
call :testfirst "%%A"
)
goto :eof
:testfirst
FOR /F "usebackq tokens=3 delims=," %%m IN (%1) DO (
IF "%%m" == "F01" (xcopy /Y %1 .\Outbound)
goto:eof
)
未经测试
读取第一行标记3
for /f "tokens=3 delims=," %%a in ('"findstr /n . %1|findstr /b 1:"') do set fltok3=%%a
echo(%fltok3%
黑客=
@echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
GOTO:EOF
)
)
所以你所做的就是在第一遍之后逃避循环,而不是继续下一行。
@set STDERRZ=fileread.txt
@setlocal EnableDelayedExpansion
@set lineforread=2
@echo off
echo Line %lineforread% for %STDERRZ%
@FOR /F "tokens=*" %%F IN ('more +%lineforread% %STDERRZ%') do (
@SET conteudo=%%F
goto end)
:end
echo %conteudo%
PAUSE