Computing.Net > Forums > Programming > batch file help with csv files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch file help with csv files

Reply to Message Icon

Name: domscand
Date: November 3, 2008 at 13:21:36 Pacific
OS: xp pro
CPU/Ram: 3gz 2gb
Product: na
Comment:

I need to create a batch file to go through
all the .CSV files in the local directory. I
want to simply delete lines 2-29, 31-59, etc
deleting every 29 rows and keeping one. I
obviously want to keep the header row (row 1)
as well. Through my searches I am seeing
that this type of thing is possible, but I am
not seeing any applications close enough to
mine to figure out my own.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: November 3, 2008 at 14:01:09 Pacific
Reply:

Test this first on copies, so you don't mess up your files.

=================================================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%c in ('dir/b/a-d *.csv') do (
set N=0
echo %%c

for /f "tokens=* delims= " %%a in (%%c) do (
set /a N+=1
if !N! equ 1 echo %%a > ##

for /L %%i in (30 30 3000) do (
if !N! equ %%i echo %%a >> ##
)
)
copy ## %%c
)
del ##


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: domscand
Date: November 3, 2008 at 14:19:13 Pacific
Reply:

Thank you, but that actually deleted every
row but the header row. all I am left with
is the header row of the document


0

Response Number 3
Name: Mechanix2Go
Date: November 3, 2008 at 14:55:24 Pacific
Reply:

I don't know what's going on but I just tested it.

If you typed it in instead of pasting, I highly recommend pasting.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: domscand
Date: November 3, 2008 at 20:00:15 Pacific
Reply:

I dont know why it was not before, but it is working now. It cuts off at 3000 rows though. Should I just increase the 3000 you have in your code or is there a way to get the actual row max before running?


0

Response Number 5
Name: Mechanix2Go
Date: November 5, 2008 at 11:41:40 Pacific
Reply:

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%c in ('dir/b/a-d *.csv') do (
set N=0

for /f "tokens=* delims= " %%r in ('find /v /c "" ^< %%c') do (
set /a T=%%r
)

for /f "tokens=* delims= " %%a in (%%c) do (
set /a N+=1
if !N! equ 1 echo %%a > ##

for /L %%i in (30 30 !T!) do (
if !N! equ %%i echo %%a >> ##
)
)
copy ## %%c > nul
)
del ##


=====================================
If at first you don't succeed, you're about average.

M2


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file help with csv files

Batch script: Help with FOR file IO www.computing.net/answers/programming/batch-script-help-with-for-file-io/14915.html

I need help with using files in C# www.computing.net/answers/programming/i-need-help-with-using-files-in-c/12561.html

Need help with bat file www.computing.net/answers/programming/need-help-with-bat-file/13270.html