count lines of files in dir: if file has only 1 line - move

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tomj
Posts: 2
Joined: 17 Jun 2013 14:22

count lines of files in dir: if file has only 1 line - move

#1 Post by tomj » 17 Jun 2013 16:16

Hi -

I need help to get a batch file working.

About 80 .csv files get dumped into a directory each day to be loaded to a dB.
But many of the files have only 1 line (header). Those 1-line files need to
be moved to a directory '/archive,' leaving the greater than 1-line files in place.


I need to :
1. get line count of each file
2. write each <filename> & <row_count> to a file ("filelist.txt")
3. move files with only 1-line to '/archive'

What I have working is . . .

Code: Select all

find /c /v "" *.csv >filelist.txt


I know it's got to be fairly basic. I've been all over the (really well done) DosTips areas but can't make anything work.

How do I get this to work with a logical comparison to only move the 1-line files?

thanks-

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: count lines of files in dir: if file has only 1 line - m

#2 Post by foxidrive » 17 Jun 2013 22:17

Edited to correct the find command line

Try this:

Code: Select all

@echo off
for %%a in (*.csv) do (
for /f %%b in ('find /c /v "" ^<"%%a"') do (
>>filelist.txt echo lines=%%b Filename=%%a
if %%b EQU 1 move /-y "%%a" \archive
)
)

tomj
Posts: 2
Joined: 17 Jun 2013 14:22

Re: count lines of files in dir: if file has only 1 line - m

#3 Post by tomj » 18 Jun 2013 17:02

Thanks Foxidrive !

It works.....up until the 'Find'. Find is not picking up the # of rows as Var.
BUT I tweaked the 'Find' piece so it works perfect:

('find /c /v "" "%%a"')
....to:
('find /c /v "" ^<"%%a"')

this works....

Code: Select all

@echo off
for %%a in (*.csv) do (
for /f %%b in ('find /c /v "" ^<"%%a"') do (
>>filelist.txt echo lines=%%b Filename=%%a
if %%b EQU 1 move /-y "%%a" "\archive"
)
)
Thanks again Foxidrive - I am grateful for your suggestion/solution.
This was my first post to any forum ever.
-tomj

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: count lines of files in dir: if file has only 1 line - m

#4 Post by foxidrive » 19 Jun 2013 03:43

I'm glad you solved the problem and gave us feedback too.

Cheers.

Post Reply