题
如何使用Windows批处理文件从文本文件中读取第一行?由于文件很大,我只想处理第一行。
解决方案
这是一个通用批处理文件,用于打印GNU head
实用程序等文件中的顶部 n
行,而不只是一行。
@echo off
if [%1] == [] goto usage
if [%2] == [] goto usage
call :print_head %1 %2
goto :eof
REM
REM print_head
REM Prints the first non-blank %1 lines in the file %2.
REM
:print_head
setlocal EnableDelayedExpansion
set /a counter=0
for /f ^"usebackq^ eol^=^
^ delims^=^" %%a in (%2) do (
if "!counter!"=="%1" goto :eof
echo %%a
set /a counter+=1
)
goto :eof
:usage
echo Usage: head.bat COUNT FILENAME
例如:
Z:\>head 1 "test file.c"
; this is line 1
Z:\>head 3 "test file.c"
; this is line 1
this is line 2
line 3 right here
它目前不计算空行。它还受批量文件行长度限制8 KB的限制。
其他提示
呃? imo这个更简单
set /p texte=< file.txt
echo %texte%
呃,你们......
C:\>findstr /n . c:\boot.ini | findstr ^1:
1:[boot loader]
C:\>findstr /n . c:\boot.ini | findstr ^3:
3:default=multi(0)disk(0)rdisk(0)partition(1)\WINNT
C:\>
你可以尝试一下:
@echo off
for /f %%a in (sample.txt) do (
echo %%a
exit /b
)
修改强> 或者,假设您有四列数据,并希望从第5行到底部,请尝试:
@echo off
for /f "skip=4 tokens=1-4" %%a in (junkl.txt) do (
echo %%a %%b %%c %%d
)
感谢thetalkingwalnut及答案 Windows批处理从文本文件中读取第一行的命令我提出了以下解决方案:
@echo off
for /f "delims=" %%a in ('type sample.txt') do (
echo %%a
exit /b
)
略微建立在其他人的答案上。现在允许您指定要读取的文件以及要将结果放入的变量:
@echo off
for /f "delims=" %%x in (%2) do (
set %1=%%x
exit /b
)
这意味着你可以使用上面这样的(假设你称之为getline.bat)
c:\> dir > test-file
c:\> getline variable test-file
c:\> set variable
variable= Volume in drive C has no label.
一个衬垫,对于带有“&gt;”的标准重定向非常有用:
@for /f %%i in ('type yourfile.txt') do @echo %%i & exit
试试这个
@echo off
setlocal enableextensions enabledelayedexpansion
set firstLine=1
for /f "delims=" %%i in (yourfilename.txt) do (
if !firstLine!==1 echo %%i
set firstLine=0
)
endlocal
EXIT / B
解决方案的问题,当批处理文件中更真实地作为其中的一部分时,如下所示。在 EXIT / B
之后,所述批处理文件中没有后续处理。通常,批次不仅仅是一项有限的任务。
要解决这个问题:
@echo off & setlocal enableextensions enabledelayedexpansion
set myfile_=C:\_D\TEST\My test file.txt
set FirstLine=
for /f "delims=" %%i in ('type "%myfile_%"') do (
if not defined FirstLine set FirstLine=%%i)
echo FirstLine=%FirstLine%
endlocal & goto :EOF
(但是,所谓的毒药字符仍然是个问题。)
有关使用批处理命令获取特定行的主题的更多信息:
如何获取文本文件的第n行,第1行和最后一行?“ http://www.netikka.net/tsneti/info/tscmd023.htm结果
[2012年8月28日添加] 还可以:
@echo off & setlocal enableextensions
set myfile_=C:\_D\TEST\My test file.txt
for /f "tokens=* delims=" %%a in (
'type "%myfile_%"') do (
set FirstLine=%%a& goto _ExitForLoop)
:_ExitForLoop
echo FirstLine=%FirstLine%
endlocal & goto :EOF
以下是使用 powershell
的解决方法:
powershell (Get-Content file.txt)[0]
(您可以使用 powershell(Get-Content file.txt)[0..3]
轻松阅读一系列行
如果您需要在批处理脚本中设置变量作为 file.txt
的第一行,您可以使用:
for /f "usebackq delims=" %%a in (`powershell ^(Get-Content file.txt^)[0]`) do (set "head=%%a")
注意,批处理文件方法将限制为DOS命令处理器的行限制 - 请参阅什么是命令行长度限制?。
因此,如果尝试处理的文件具有多于 8192 字符的行,则脚本将跳过它们,因为无法保存该值。
另一种方式
setlocal enabledelayedexpansion
@echo off
for /f "delims=" %%i in (filename.txt) do (
if 1==1 (
set first_line=%%i
echo !first_line!
goto :eof
))
表示文件( file1.txt
, file1 [1] .txt
, file1 [2] .txt
等) :
START/WAIT C:\LAERCIO\DELPHI\CICLADOR\dprCiclador.exe C:\LAERCIUM\Ciclavel.txt
rem set/p ciclo=< C:\LAERCIUM\Ciclavel.txt:
set/p ciclo=< C:\LAERCIUM\Ciclavel.txt
rem echo %ciclo%:
echo %ciclo%
它正在运行。