[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch File to return text file name & data

Posted on 2011-04-20
7
Medium Priority
?
363 Views
Last Modified: 2012-05-11
Hello,

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
etc.

Any suggestions?

Thanks in advance,

Trish

Hope I have this in the right zone!
0
Comment
Question by:Halia
  • 4
  • 2
7 Comments
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 35433908
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
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 total points
ID: 35433956
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
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35433971
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
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 33

Expert Comment

by:knightEknight
ID: 35433989
revising my first script to include "Records in file"

@echo %%F %%C Records in file
0
 

Author Comment

by:Halia
ID: 35434166
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?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35434185
add the following line to the bottom of the script:

exit
0
 

Author Comment

by:Halia
ID: 35434235
Thank you so much!!!!!  You have just saved hours of cutting and pasting!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

872 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