I have a folder with multiple files containing records.

I want a windows batch script which will copy all the 'file names' into a single output file along with the count of records that are present in all the files.

  • What is a "record"? – RJFalconer Oct 13 '14 at 8:27

Reference FIND Search for a text string in a file & display all the lines where it is found.

find /v /c "" *.* > result.txt

How it works

  • /v inverts the search (so when we search for nothing we find all lines)
  • /c counts matches
  • "" match nothing
  • *.* search all files
  • > result.txt redirect output


  • assumes normal windows end of line delimeters \r\n
  • although FIND can be used to scan large files, it will not detect any string that is positioned more than 1070 characters along a single line (with no carriage return \r) This makes it of limited use in searching binary or XML file types.

To remove the additional - and blank lines we can wrap the above command in a for construct as follows:

for /f "usebackq tokens=2-3" %a in (`find /v /c "" *.*`) do echo %a %b >> result.txt
  • +1 I didn't know about find /c /v "" I'd always done find /c /v "sdfsdf" Interesting that find /c /v "" works.. I see it works as find doesn't match empty string even to empty lines. I see findstr doesn't match "" either though of course can match empty lines with regex of ^$ – barlop Oct 13 '14 at 10:27
  • This worked for me. But I am getting the below as output: ---------- PFIZ_EXT_SEAGRCLMHISD_20141013_1.TXT: 165428 ---------- PFIZ_EXT_SEBMODCHKIND_20141013_1.TXT: 0 ---------- PFIZ_EXT_SECNSLDND_20141013_1.TXT: 13 ---------- PFIZ_EXT_SECNSMRD_20141013_1.TXT: 15788 ---------- PFIZ_EXT_SECNSNTD_20141013_1.TXT: 291825 ---------- PFIZ_EXT_SECPGNPRD_20141013_1.TXT: 24129 ---------- PFIZ_EXT_SEFFLD_20141013_1.TXT: 219599 ---------- PFIZ_EXT_SESVYRSPD_20141013_1.TXT: 123284 Can you please help me in removing the new line at the beginning and '-' – Siddharth Oct 14 '14 at 6:43

Thanks a lot for your responses. Adding to my original requirement, the scenario is that a folder gets created (in YYYYMMDD format) on daily basis at a particular location. This folder contains few files with/without records present in it.

Since this is on a daily basis, I will be using this batch file daily. And whenever I execute this batch file on any particular day, it should then check for the folder which will be created on that particular day and then give an output file which contains the file names and the record count

Is there any way to get this requirement?

  • You now have an answer to your question. Please ask a new question with the new requirements by clicking the Ask Question button. Include a link to this question if it helps provide context. – DavidPostill Oct 14 '14 at 7:06

For just a list of filenames:

dir * /b /s > files.txt
  • * is unneeded here – user373230 Oct 13 '14 at 8:32
  • That only answers the 'easy' part of the question – Jan Doggen Oct 13 '14 at 9:01
  • Prior to my fixing OPs question that's all he asked for – RJFalconer Oct 13 '14 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.