如何使用 Windows 批处理文件从文本文件中读取第一行?由于文件很大,我只想处理第一行。
16 回答
呃?imo这要简单得多
set /p texte=< file.txt
echo %texte%
这是一个通用批处理文件,用于打印n
GNU 实用程序等文件的顶部行head
,而不仅仅是一行。
@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 的批处理文件行长度限制。
呃你们...
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.
一个衬里,对使用“>”的标准输出重定向很有用:
@for /f %%i in ('type yourfile.txt') do @echo %%i & exit
powershell Get-Content file.txt -Head 1
这个比上面读取完整文件的其他 powershell 示例快得多。
试试这个
@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
要循环文件(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%
它正在运行。
解决方案的问题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 行、第一行和最后一行?” 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
))
在 Windows PowerShell 下面 cmd 可用于获取第一行并将其替换为静态值
powershell -Command "(gc txt1.txt) -replace (gc txt1.txt)[0], 'This is the first line' | Out-File -encoding ASCII txt1.txt"
参考
仅打印第一行(无需读取整个文件):
set /p a=< file.txt & echo !a!
要打印第一行,然后等待用户按下下一行的键:(
打印所需行后,按 Ctrl+C 停止。)
for /f "delims=" %a in (downing.txt) do echo %a & pause>nul
要打印第一 n 行(无需用户按键):
type nul > tmp & fc tmp "%file%" /lb %n% /t | find /v "*****" | more +2
在 Win 10 CMD 上测试。