Delete last line in CSV

March 26, 2010 at 09:15:15
Specs: Windows XP
I need a batch file to delete the last line in every .csv file in a folder. I've seen serveral similar post, but nothing that works. Please helk!

See More: Delete last line in CSV

Report •

March 26, 2010 at 10:28:48
All empty lines are ignored :

@echo off
setlocal enabledelayedexpansion

set /a p_max=0

for /F "tokens=*" %%a in (test.txt) do (
set /a p_max=!p_max!+1

set /a cnt=1

for /F "tokens=*" %%a in (test.txt) do (
if !cnt! LSS %p_max% echo %%a
set /a cnt=!cnt!+1

Report •

March 26, 2010 at 11:15:08
Thanks, but it's not working for me. It's not removing the last line

Report •

March 26, 2010 at 12:28:28
If you dont tell me whats wrong we cant help ya

Report •

Related Solutions

March 26, 2010 at 13:01:54
Maybe an example of the file format will help, see below.

,R.A.,174,,,,,INVOICE TOTAL,,,,,"$49,538.00 ",

I already have a .bat file to remove the first 9 rows,(above format) now I need the last row containing the ",R.A." ect removed from every .csv file in a folder. Roughly 400 in the folder. Then I can merge all the files together and the end up with just the raw data.

Report •

March 26, 2010 at 15:15:18
First of all, Response 1 is a script to handle ONE FILE only. If you need to remove series of files, you either take all of that code, and plug it in another loop, or you call to a file that handles one file each :


@echo off

for %%f in (*.txt) do (
echo --- PUT
echo --- CODE
echo --- HERE
echo --- USING
echo --- %%f
echo --- OR
echo --- CALL HANDLE1FILE.cmd %%f

Report •

March 26, 2010 at 15:17:28
how dependable is the string "R.A" (ie: is it consistantly on the last line?) or how about "INVOICE" or "$"?
if all files have same extension, this might glue them all together and remove the line in one go:
@echo off>allfiles & setlocal
for %%a in (*.txt) do (
find /v "R.A" < %%a >> allfiles
::------ end ------
you get the idea, fix it up as needed. You could also look for a comma as the first byte in the line using FINDSTR, but hopefully one of the strings is consistant at end-of-file line.

Report •

March 26, 2010 at 15:18:06
Then, know that script in Response 1 does not really work, since all it does is ouput what you want, given the inputfile.

Example, if that code is called "handle1file.cmd", you could call it this way :

call handle1file.cmd > myoutputfile.csv

Whatever you do, do not use the same name of the file for both outputfile (myoutputfile.csv) and test.txt (see response 1)

Report •

March 26, 2010 at 15:20:12
Nbrane, it MAY work as well, but how sure are you:
- the last line contains R.A.
- no other line contains R.A

If those 2 are true, I agree.
The problem with my script, is that it removes the last line ... but when you run multiple times, it will reduce multiple lines, and it is hard to check that ...

Report •

March 26, 2010 at 18:58:57
@silencer057, you are using the wrong tool for file processing.
you can download sed and on the command line,

C:\test>type file
last line

C:\test>sed -n "$!p" file

GNU win32 packages | Gawk| sed

Report •

March 29, 2010 at 13:20:36
Thanks all! Since "R.A." is allways the last line in the file, nbrane's solution works liek a charm..

@echo off
setlocal enabledelayedexpansion

for %%a in (*.csv) do (
find /v "R.A" < %%a >> %%~Na.csv

Thanks again!

Report •

Ask Question