delete x number of lines from a CSV file

August 13, 2009 at 07:07:55
Specs: Windows XP
Hi,
Is it possible to delete x number of lines from a CSV file using a batch file ?

What I have is 2 CSV files with a Log of temperatures from an AirCon unit.
The first is an older file contain several months worth of data, the second contains all the original data from the first, but with the next months data tagged onto the bottom of the file.

I am hoping to count the number of lines in the first file using something like:

for /f "delims=" %%i in ('type AC1-Count.txt^|find /c /v "" ') do set count=%%i
echo %count%

but then I want to delete this number of lines from the second file starting at line number 2.

I hope I have explained that in some sort of english.

Thanks in advance

Dave



See More: delete x number of lines from a CSV file

Report •


#1
August 13, 2009 at 10:42:58
Hi Chalkydave
Is this what you want

@echo off
SetLocal EnableDelayedExpansion

for /f "delims=" %%i in ('type AC1-Count.txt ^|find /c /v ""') do set Count=%%i
echo %Count%

set Count2=0
for /f "tokens=*" %%a in (AC1-Count2.txt.) do (
if !Count2! EQU 0 (echo %%a)
if !Count2! GTR %Count% (echo %%a)
set /a Count2+=1
) >> tmp.txt

rem rename out after testing if required
rem del AC1-Count.txt
rem ren tmp.txt AC1-Count.txt


Report •

#2
August 14, 2009 at 00:55:04
dtech10,
You are the man !

I had to tinker with it slightly, but this is the working code:

@echo off
SetLocal EnableDelayedExpansion

for /f "delims=" %%i in ('type 2009_07_01.csv ^|find /c /v ""') do set Count=%%i
echo %Count%
set /a Lines=%Count%-1
echo %Lines%
set Count2=0
for /f "tokens=*" %%a in ('type 2009_08_01.csv') do (
if !Count2! EQU 0 (echo %%a)
if !Count2! GTR %Lines% (echo %%a)
set /a Count2+=1
) >> 2009_08.csv

The only thing I need to try and work out now, is how to set the dates automatically.

i.e. On the first of each month, this batch file uses the current months data against the previous months.

Or, to do the same using the latest two files regardless of the names. (Extension would always be .CSV)

Cheers

Dave


Report •

#3
August 17, 2009 at 07:54:36
Hi all,

Does anybody know why the code below will work for a time and then stop working ?

@echo off
SetLocal EnableDelayedExpansion

Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
set Mth=%Date:~3,2%
set Yr=%Date:~6,4%
if %Mth% LEQ 9 set Mth=%Date:~4,1%
if %Mth% LEQ 9 (
set Mth2=0%Mth%
) else (
set Mth2=%Mth%
)
set /a xMth=%Mth%-1
if %xMth% LEQ 9 (
set xMth2=0%xMth%
) else (
set xMth2=%xMth%
)
if %Mth% LEQ 1 (
set xMth2=12
set /a xYr2=%Yr%-1
) else (
set xYr2=%Yr%
)
echo This Months File = AC1-%Yr%_%Mth2%_01
echo Last Months File = AC1-%xYr2%_%xMth2%_01

for /f "delims=" %%i in ('type AC1-%Yr%_%Mth2%_01.csv ^|find /c /v ""') do set Count=%%i
echo %Count%
set /a Lines=%Count%-1
echo %Lines%
set Count2=0
for /f "tokens=*" %%a in ('type AC1-%xYr2%_%xMth2%_01.csv') do (
if !Count2! EQU 0 (echo %%a)
if !Count2! GTR %Lines% (echo %%a)
set /a Count2+=1
) >> AC1-%Yr%_%Mth2%.csv


Report •

Related Solutions

#4
August 17, 2009 at 15:12:36
Hi ChalkyDave

It's the old leading zero used with set /a command which it takes as being an Octal number.
Enter the extra lines below and see if that cures your problem.
If you need the leading zeros for display. Use this
-------------------------------------------------
If %Day% LSS 10 (set Day=0%Day%)
If %Mth% LSS 10 (set Mth=0%Mth%)
-----------------------------------------------

Your Code
-------------
Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
if "!Day%:~0,1!" EQU "0" (set Day=!Day:~1,1!)
set Mth=%Date:~3,2%
if "!Mth%:~0,1!" EQU "0" (set Mth=!Mth:~1,1!)
set Yr=%Date:~6,4%


Report •


Ask Question