Mate
Mate

Reputation: 21

Batch script to delete specific lines in a text file

I'm trying to automate some process for a new software we use at work. I have a .zip file that gets downloaded automatically from an FTP every 4 hours. The .zip file is then uncompressed and the .txt file in it is renamed to a .csv using 2 .bat files that I managed to scrap together with my very little knowledge and the resulting .csv is imported in the software. This part works correctly.

Now the problem is that the .csv has two redundant lines that I need to remove before I can upload it in the software: line 1 and 3. I haven't found an approach from Google searches.

Can someone help me with the command that would strip those lines?

Also, would there be a way to "merge" my multiple .bat together? This is my extract script:

@echo off

for /R "C:\FTP Download" %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 
)

and this is my rename script:

@echo off
ren *.txt *.csv

I guess there must be a way to put them in the same .bat but I didn't figure that one out yet.

Upvotes: 2

Views: 5995

Answers (2)

JosefZ
JosefZ

Reputation: 30103

Start with next one-line command from command line:

for /F "tokens=1* delims=:" %G in ('findstr /N /R "^" xxxx.txt') do @if not "%G"=="1" @if not "%G"=="3" @echo %G %H

In a .bat script:

for /F "tokens=1* delims=:" %%G in (
   'findstr /N /R "^" xxxx.txt'
) do if not "%%G"=="1" if not "%%G"=="3" echo %%G %%H

No sooner than debugged, replace it with next code snippet:

type nul>xxxx.csv
>>xxxx.csv (for /F "tokens=1* delims=:" %%G in (
   'findstr /N /R "^" xxxx.txt'
) do if not "%%G"=="1" if not "%%G"=="3" echo(%%H)

Update: merge two code snippets to one script (commented code):

@ECHO OFF >NUL
SETLOCAL enableextensions

rem to main working directory
pushd "C:\FTP Download"

for /R %%I in ("*.zip") do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 

  rem need change working directory due to recursion in `for /R %%I ...`  
  pushd "%%~dpI"
  for /F "delims=" %%X in ('dir /B *.txt') do (

    type nul>"%%~nX.csv"
    >>"%%~nX.csv" (for /F "tokens=1* delims=:" %%G in (
       'findstr /N /R "^" "%%~X"'
    ) do if not "%%G"=="1" if not "%%G"=="3" echo(%%H)

    rem delete processed text file 
    del "%%~X"
  )
  rem back to main working directory
  popd
  rem archive processed zip file
  move "%%~fI" "%tmp%\done%%~nxI"
)
popd

Resources (required reading):

Upvotes: 1

canolucas
canolucas

Reputation: 1490

To remove lines 1 and 3:

Assuming that your input is C:\file.txt,

First, lets skip the first line and copy the second line to C:\newfile.txt

@echo off
for /f "skip=1" %%a in (C:\file.txt) do (
    echo %%a >> C:\newfile.txt   
    goto OutOfLoop
)
:OutOfLoop

Now we can ignore the first three lines of the file completely, and proceed to copy the rest of the wanted text to C:\newfile.txt:

for /f "skip=3 delims=*" %%b in (C:\file.txt) do (
    echo %%b >> C:\newfile.txt    
)

This will replace the original file, and remove our temporal copy:

xcopy C:\newfile.txt C:\file.txt /y
del C:\newfile.txt /f /q

NOTE:

  • You need to run this text parser for each of the input files you want to process.

To itarate over the files inside a directory, you can use:

for /r %%file in (*) do (
    REM Here you can put the code to parse each input file
)
  • Your extraction and renaming code should be able to work correctly in one single batch file:

For the renaming to work, first browse to the extracted files' directory.

Your final script will look like the following:

@echo off

REM Browse to the desired directory
cd C:\FTP Download\

REM Extract all zip files
for /r %%I in (*.zip) do (
  "%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpI" "%%~fI" 
)

REM Rename all extracted .txt files to .csv
ren *.txt *.csv

REM For each .csv file
for /r %%file in (*.csv) do (

    REM Copy second line to C:\newfile.txt
    for /f "skip=1" %%a in (%%file) do (
        echo %%a >> C:\newfile.txt
        goto OutOfLoop
    )
    :OutOfLoop

    REM Copy fourth to last line to C:\newfile.txt
    for /f "skip=3 delims=*" %%b in (%%file) do (
        echo %%b >> C:\newfile.txt    
    )

    REM replace original file and remove temporary files
    xcopy C:\newfile.txt %%file /y
    del C:\newfile.txt /f /q
)

Upvotes: 1

Related Questions