【问题标题】:Batch file to Copy some lines from one text file to another text file批处理文件将一些行从一个文本文件复制到另一个文本文件
【发布时间】:2016-05-02 12:38:29
【问题描述】:

我写批处理文件,我有一个问题:

我希望它写入从当前日期到文件末尾的所有行

文本文件示例:

not copy this line.
not copy this line.
not this line.
5/02/2016  10 20 30 45 05 56 70 (from here )
5/03/2016  10 20 30 45 05 56 70 
5/04/2016  10 20 30 45 05 56 70 
5/05/2016  10 20 30 45 05 56 70

我的代码:

@ECHO OFF
set filename=abc.txt
set filename2=outFile.txt
set find=find.txt
set sourceFolder=c:\1\
IF EXIST "%find%" DEL "%find%"
IF EXIST "%filename2%" DEL "%filename2%"
IF EXIST "SAMPLE_text01.txt" DEL "SAMPLE_text01.txt"
set currentdate=%date%
set newdate=%currentdate:~5,11%
echo %newdate% >> %sourceFolder%%find%

findstr /g:%sourceFolder%%find% %sourceFolder%%filename% > %sourceFolder%%filename2%

set count=0

for /f "tokens=*" %%i in ('findstr /g:%sourceFolder%%find% 
"%sourceFolder%%filename%"') do (

    rem echo count is %count%
    set /a count+=1
    echo.%* >> temp.txt
)

pause

【问题讨论】:

  • 你的代码有什么问题?显示它所有的行?没有?
  • 所以,您的目标是跳过源文本文件中的前 3 行,并将其余部分复制到另一个文件中???
  • 在我看来,他的目标是跳过日期 n 行。
  • 嗨,你能帮我处理这段代码吗,我希望它找到今天的日期并从那里复制到文件末尾。这批只取今天的日期并停止/
  • 您好,谢谢。我希望它找到今天的日期,然后从那里复制到 eof

标签: file batch-file


【解决方案1】:

假设您的文本文件中的日期格式与内置环境变量%DATE% 返回的日期格式相同,那么以下脚本——我们称之为lines-from-today-on.bat——应该适合您:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem Define constants here:
set "INFILE=%~1"
if not defined INFILE set "INFILE=%~dpn0.txt"
set "OUTFILE=%~2"
if not defined OUTFILE set "OUTFILE=con"

> "%OUTFILE%" (
    set "FLAG="
    for /F "delims=" %%L in ('
        findstr /N /R "^" "%INFILE%"
    ') do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        for /F "tokens=1 eol= " %%F in ("!LINE:*:=!") do (
            endlocal
            if "%%F"=="%DATE%" (
                set "FLAG=#"
            )
            setlocal EnableDelayedExpansion
        )
        if defined FLAG echo(!LINE:*:=!
        endlocal
    )
)

endlocal
exit /B

要使用它,通过提供输入文件作为命令行参数来调用它(在本例中为当前目录中的文本文件sample.txt):

lines-from-today-on.bat sample.txt

这会在控制台上显示提取的行。要将它们输出到另一个文本文件(此处为return.txt),请提供另一个参数:

lines-from-today-on.bat sample.txt return.txt

【讨论】:

  • 我认为您比我更了解这个问题。 +1
  • 谢谢,@rojo!我假设输入文件类似于日志文件,因此日期按升序排序(也符合 OP 的示例数据);这允许我进行简单的字符串比较 (==),因此不需要真正的(不支持的)日期比较 (>=)...
【解决方案2】:

由于 Batch 语言没有日期对象,我发现使用具有日期对象的辅助语言很方便。例如,使用 JScript,您可以在日期对象上使用小于/大于比较,并且日期格式更加宽容。 (文本文件是否总是有一个零填充的日期和一个非零填充的月份?JScript 的new Date() 构造函数将在任何一种方式下工作。)

用 .bat 扩展名保存它,看看你的想法。

@if (@CodeSection == @Batch) @then
@echo off & setlocal

set "in=abc.txt"
set "out=outfile.txt"
set "found="

rem // redirect output to outfile
> "%out%" (
    rem // for each line of the text file...
    for /f "usebackq delims=" %%I in ("%in%") do (
            rem // use JScript to check whether line contains a date of today or future
            if not defined found call :isCurrent "%%~I" && set "found=1"
            if defined found echo(%%I
        )
    )
)

goto :EOF

:isCurrent <date>
cscript /nologo /e:JScript "%~f0" "%~1"
exit /b %ERRORLEVEL%

@end // end batch / begin JScript hybrid code

try {
    var today = new Date(new Date().toDateString()), // truncate to midnight
        otherDate = new Date(WSH.Arguments(0).match(/\b\d+\/\d+\/\d+\b/)[0]);
}
catch(e) { WSH.Quit(1); } // exits non-zero if argument contains no dates

// exits non-zero if otherDate is earlier than today
WSH.Quit(!(today <= otherDate));

【讨论】:

  • 嗨,谢谢,我试了一下,但它不起作用我放入 abc.txt --dfsdgfs 8/2/2016 3 5 8 9 fds dfds 5/2/2016 1 23 4 5 6/2/ 2016 3 5 8 9
  • @user3132036 好的,我修改了脚本,所以它会检查整行是否有与您的MM/DD/YYYY 日期匹配的字符串;如果今天,打开闸门并将 + 后面的每一行回显到 outfile.txt 中。这就是你想要做的吗?
  • 嗨,谢谢,它工作正常。你能找到为什么我写的代码不起作用吗
猜你喜欢
  • 2013-04-23
  • 2014-02-24
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多