Announcement

Collapse
No announcement yet.

Batch script to remove lines 2, 3, and 4

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch script to remove lines 2, 3, and 4

    I would like to use a batch script in order to remove lines 2, 3, and 4 from all the CSV files in a folder. I need to leave the first line in place and the remaining lines should also be left alone.

    Each CSV file begins with the following lines:

    Date,Open,High,Low,Close,
    IQT26210777,,,5/1/1992,4/16/2014,
    IQ_PRICEDATE,IQ_LASTSALEPRICE,IQ_LASTSALEPRICE,IQ_ LASTSALEPRICE,IQ_LASTSALEPRICE,
    ,,,,,
    5/1/1992,3.95,3.95,3.95,3.95,
    5/4/1992,3.9,3.9,3.9,3.9,
    5/5/1992,3.9,3.9,3.9,3.9,
    5/6/1992,3.9,3.9,3.9,3.9,

    I need the output to retain the first line beginning with "Date" and the second line of the output should begin with a date (such as 5/1/1992 in the above example).

    I use the following script to clean these types of CSV files for blank/empty lines and delete them:

    @echo off
    :: Please note eol=$ will skip lines starting with $
    :: All lines of text must be less than 1023 bytes in length
    setlocal
    set "firstLineReady="
    for /f "delims=" %%a in ('dir *.csv /b /s') do (
    echo processing %%a
    (
    for /f "eol=$ delims=" %%b in ('type "%%a"') do (
    if defined firstLineReady (echo.)
    if not defined firstLineReady set firstLineReady=1
    <nul set /p "=%%b"
    )
    ) > out.csv
    move /y out.csv "%%a" >nul
    set "firstLineReady="
    )
    echo.
    echo done.
    pause


    Is it possible to modify this code to also delete the unwanted lines?
    Last edited by Chip4Pips; 17th April 2014, 05:08.
Working...
X