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

Test this first on copies, so you don't mess up your files.
=================================================
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%c in ('dir/b/a-d *.csv') do (
set N=0
echo %%cfor /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

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

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

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?

@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%c in ('dir/b/a-d *.csv') do (
set N=0for /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

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

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