如何使用Windows批处理文件从文本文件中读取第一行?由于文件很大,我只想处理第一行.
呃?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 )
感谢thewalkingwalnut回答Windows批处理命令从文本文件中读取第一行我想出了以下解决方案:
@echo off for /f "delims=" %%a in ('type sample.txt') do ( echo %%a exit /b )