Batch File to return text file name & data

Posted on 2011-04-20
Last Modified: 2012-05-11

I need a Batch file that will produce a text that will have the name of the file and the data (all the files have 1 line only)

Example - ProdCount.txt has 1 record in it that tells me a count of the Prod.txt file (54000 records in file)

So my output would be...

ProdCount.txt 54000 Records in File
EmpInfo.txt 170000 in file

Any suggestions?

Thanks in advance,


Hope I have this in the right zone!
Question by:Halia
    LVL 25

    Expert Comment

    by:Ron M
    You can use FINDSTR

    If the file only has one line, and you just want to return it's contents...

    FINDSTR . C:\path\filename.txt
    LVL 33

    Accepted Solution

    This will echo the filename first, but it assumes that ALL .txt files in the current directory have only one line of content:

      @echo off
      for %%F in (*.txt) do for /f "delims=" %%C in ('type %%F') do @echo %%F %%C

    you can then re-direct the output of this file to a new file like this:

      mybat.bat > myout.txt
    LVL 33

    Expert Comment

    Actually the FINDSTR approach is easier, but you need to specify a wildcard in order to get the filename as part of the output:

    FINDSTR . C:\path\*.txt > myout.txt
    LVL 33

    Expert Comment

    revising my first script to include "Records in file"

    @echo %%F %%C Records in file

    Author Comment

    Ok....this is working -

    @echo off
      for %%F in (*.count) do for /f "delims=" %%C in ('type %%F') do @echo %%F %%C
    Count.bat > Counts.txt

    I get my file (PERFECT) but the Window is staying open.  I see the last record and it stops and sits there.

    What do I need to change?
    LVL 33

    Expert Comment

    add the following line to the bottom of the script:


    Author Comment

    Thank you so much!!!!!  You have just saved hours of cutting and pasting!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
    Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now