Windowsバッチのコマンドを最初にお読みくださいテキストファイルから

StackOverflow https://stackoverflow.com/questions/130116

  •  02-07-2019
  •  | 
  •  

質問

どのように読んだ最初の行からのテキストファイル用のWindowsバッチファイルとは何ですか?以降、ファイルが大きいだけ欲しいんだけどへの対応があります。)

役に立ちましたか?

解決

この汎用的なバッチファイルを印刷トップ 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

ない現在のカウントは空白です。また、バッチファイルラインの長さの制限8KBです。

他のヒント

え?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.バット)

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

してみてください

@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番目、最後の行のテキストファイルとは何ですか?" http://www.netikka.net/tsneti/info/tscmd023.htm

【追加28-月-2012年] できるもの:

@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
))

にcicleファイル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%

で走っています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top