Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

Hi Chalkydave
Is this what you want@echo off
SetLocal EnableDelayedExpansionfor /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.txtrem rename out after testing if required
rem del AC1-Count.txt
rem ren tmp.txt AC1-Count.txt

dtech10,
You are the man !I had to tinker with it slightly, but this is the working code:
@echo off
SetLocal EnableDelayedExpansionfor /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.csvThe 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

Hi all,
Does anybody know why the code below will work for a time and then stop working ?
@echo off
SetLocal EnableDelayedExpansionRem 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%_01for /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

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%

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |