1

我尝试了十几种不同的方法,但都无法做到。

我在不同的文件夹中有很多文本文件,所以我想将输入文件作为变量引用,例如:*.txt(只需使用与 bat 文件位于同一文件夹中的任何 txt 文件)。

我需要复制前 6 行并将它们粘贴到一个新的 txt 文件中。
我想命名它(但不是必需的) SAMPLE_original_txt_file_name

例如:

Input = text01.txt
Output = SAMPLE_text01.txt (this would contain the first 6 complete lines from text01.txt)

我会很感激任何帮助,因为我的头现在需要缝合,以免将它撞到墙上太多......

4

2 回答 2

2

以下对我有用:

@ECHO OFF
IF "%~1" == "" (ECHO Usage: %~nx0 filemask& GOTO :EOF)
FOR /F "delims=" %%I IN ('DIR /B %1') DO (
  <"%%I" (
    FOR /L %%I IN (1,1,6) DO (
      SET line=
      SET /P line=
      SETLOCAL EnableDelayedExpansion
      ECHO(!line!
      ENDLOCAL
    )
  ) >"%%~dpISAMPLE_%%~nxI"
)

上面的脚本需要一个参数,它是一个文件掩码,例如*.txt. 它还支持使用(现有)路径完成的掩码。无论是否指定路径,输出示例文件都会在与原始文件相同的目录中创建。

于 2012-06-15T06:02:48.257 回答
0
@echo off
set count=0
for /f "tokens=*" %%i in (text01.txt) do (
call :counter %%i
)
goto :eof
:counter
rem echo count is %count%
set /a count+=1
if %count% lss 7 echo %* >> SAMPLE_text01.txt
GOTO :eof
于 2012-06-15T04:11:08.073 回答