Copy lines from .csv file into another .csv file using batch script

前端 未结 2 1357
日久生厌
日久生厌 2021-01-24 06:38

I am creating .csv files from a device (output) and need to copy a specific number of lines from this file into another .csv file which has the same format.

They are luc

2条回答
  •  暖寄归人
    2021-01-24 07:13

    There is absolutely no need to use a batch file that creates and invokes a VBScript.


    You could accomplish the task by a pure batch script like this:

    @echo off
    setlocal EnableExtensions EnableDelayedExpansion
    
    rem // Define constants here:
    set "_FILEIN=!USERPROFILE!\Desktop\Dev\Test\Default.csv"
    set "_FILEOUT=!USERPROFILE!\Desktop\Dev\Test\OutputData.csv"
    set /A "_LINEFROM=68"
    set /A "_LINETO=107"
    
    rem // Count number of lines of input file:
    for /F %%C in ('^< "!_FILEIN!" find /C /V ""') do (
        rem // Read and write files using redirection:
        < "!_FILEIN!" > "!_FILEOUT!" (
            rem // Iterate through all available lines:
            for /L %%I in (1,1,%%C) do (
                rem // Read a single line:
                set "LINE=" & set /P LINE=""
                rem // Check position (line number) of current line:
                if %%I GEQ %_LINEFROM% if %%I LEQ %_LINETO% (
                    rem // Return current line conditionally:
                    echo(!LINE!
                )
            )
        )
    )
    
    endlocal
    exit /B
    

    Note that pure batch file solutions may be limited in line lengths and file sizes. The aforementioned approach cannot handle lines longer than 1023 bytes and files with more than 2147483647 lines.


    Here is another but slower pure batch script solution:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    rem // Define constants here:
    set "_FILEIN=%USERPROFILE%\Desktop\Dev\Test\Default.csv"
    set "_FILEOUT=%USERPROFILE%\Desktop\Dev\Test\OutputData.csv"
    set /A "_LINEFROM=68"
    set /A "_LINETO=107"
    
    rem // Write file using redirection:
    > "%_FILEOUT%" (
        rem // Read file using a loop, prefixed with line index:
        for /F "delims=" %%L in ('findstr /N "^" "%_FILEIN%"') do (
            rem // Extract line number:
            2> nul set /A "IDX=%%L"
            rem // Read current line:
            set "LINE=%%L"
            setlocal EnableDelayedExpansion
            rem // Check position (line number) of current line:
            if !IDX! GEQ %_LINEFROM% if !IDX! LEQ %_LINETO% (
                rem // Return current line conditionally:
                echo(!LINE:*:=!
            )
            endlocal
        )
    )
    
    endlocal
    exit /B
    

    This approach cannot handle lines longer than 8191 - 7 = 8184 bytes and files with more than 2147483647 lines.

提交回复
热议问题