Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Python ,VBS or BAT cmd command to list data from text files from a folder

Posted on 2011-03-16
Medium Priority
Last Modified: 2012-05-11
I have Bunch of text Files in folder, and every text file contains some data plus the lines below.

/// text document data //
bla bla blabla bla blabla bla blabla bla blabla bla bla
bla bla blabla bla blabla bla bla

file -rdi 1 -ns "zzzz" -rfn "yyyy" "xxxxxx";
file -rdi 1 -ns "xxxx" -rfn "yyyy" "xxxxxx";
file -rdi 1 -ns "zzzz" -rfn "yyyy" "xxxxxx";
file -rdi 1 -ns "zzzz" -rfn "yyyy" "xxxxxx";

 bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla
 bla bla blabla bla blabla bla blabla bla blabla bla bla

/// text document data //

What i need is.. I want to list out all the values of "xxxxxx"  from all line , all files in the folder

Note : "xxxxxx" contains mostly File Paths
( example from a text file)
 file -rdi 1 -ns "rahul" -rfn "rahuld" "T:/Project_ccc//libLankini/rahul.txt";
file -rdi 1 -ns "rani" -rfn "ranid" "T:/Project_dda//lib/ani_Final.bat";
file -rdi 1 -ns "pradeepd" -rfn "pradeepd" "T:/Project_s4f//lib//lanka.avi";

hat i need is.. I want to list out all the values of "xxxxxx"  from all line , all files in the folder

and list all the paths ( "xxxxxxx" values ) in txt document
like this
Question by:armstrongnt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
LVL 14

Expert Comment

ID: 35150881
Seems like you're looking for a windows program with the functionality of "grep".

LVL 14

Expert Comment

ID: 35151175
findstr xxxxx <filename>
LVL 41

Expert Comment

ID: 35151208
Something like this perhaps?

- save as bob.py
- execute as:

  python bob.py directoryName

- e.g.,

  python bob.py .
'''Command: %(cmdName)s.py\n
Purpose: Process text files in the specified directory
         looking for references to patterns like:\n
file -rdi 1 -ns "zzzz" -rfn "yyyy" "xxxxxx";\n
  Usage: python %(cmdName)s.py <directory>\n
Example: python %(cmdName)s.py .\n'''

import os, os.path, re, sys

def Usage( cmdName = None ) :
  if not cmdName :
    cmdName = os.path.basename( sys.argv[ 0 ] )
  if cmdName[ -3: ] == '.py' :
    cmdName = cmdName[ :-3 ]
  print __doc__ % locals()

def main( dirname ) :
  if os.path.exists( dirname ) :
    if os.path.isdir( dirname ) :
      process( dirname )
    else :
      print ' Error: Specified entry %s is not a directory.\n' % dirname
  else :
    print ' Error: Specified directory not found: %s\n' % dirname

def process( dirname ) :
  pat = re.compile( 'file\s+-rdi.+"([^"]+)".+"([^"]+)".+"([^"]+)"' )
  names = os.listdir( dirname )
  for filename in names :
    if os.path.isfile( filename ) :
#     print filename
      fh = open( filename )
      try :
        for line in fh :
#         if line.startswith( 'file' ) :
#            print line
          mo = pat.search( line )
          if mo :
#           print 'Groups:', mo.groups()
            print mo.group( 3 )
      except :

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Expert Comment

ID: 35151390
Sorry, I misunderstood the ask here, and thought you were looking to pattern-search for "xxxxx" within the files.  From re-reading your question, it looks like you want to pull out the lines that match a pattern and also only print part of those lines.  

This should get you close to what you're looking for:

FOR /F "eol=; tokens=8 usebackq" %i in (`findstr "^file " <filenames>`) do @echo %i


Author Comment

ID: 35152119

thx for the fast Reply

I am not able run the file i(double click not working )s it not possible to specify folder path and file ext directly in python script
so that i can adjust it and run it

Sorry i have no programming skills at all..
LVL 14

Expert Comment

ID: 35152421
Have you tried the code piece I pasted in last?  That is for running directly from a "cmd" shell (can be put in a batch file).

Author Comment

ID: 35152446
Where can i specify the file extension !! because i hve many files( file formats) in those folders
LVL 14

Expert Comment

ID: 35152652
What do you mean by extension?  You replace <filenames> with the actual file(s) you want to do, or a wildcard.

For example, to do all .txt and .dat files, you'd do:

FOR /F "eol=; tokens=8 usebackq" %i in (`findstr "^file " *.txt *.dat`) do @echo %i

Open in new window

Of course I'm looking specifically for lines that begin with "file", and am pulling out the 8th column.  I ran it against your sample file, and got:

C:\Users\Eric>FOR /F "eol=; tokens=8 usebackq" %i in (`findstr "^file " file.txt`) do @echo %i

LVL 41

Expert Comment

ID: 35155529
Do you have python installed on your system?

- Open a command prompt
  Start -> run -> cmd
- At the prompt, type

  python -V

Note: That's a capital V.  The output should either be something like:

Python 2.6.5


'python' is not recognized as an internal or external command,
operable program or batch file.

which do you get?

Author Comment

ID: 35155842
I am having Python 2.7.1 but when i run from cmd it says the command is not recongnised
LVL 41

Accepted Solution

HonorGod earned 1500 total points
ID: 35156010
Which only means that the directory containing the Python executable is not part of your PATH.

We have some options here.
- Locate the "My computer" icon on your desktop
- right-click on it
- Select "Properties"
- There should be an "Advanced" tab, select it
- There should be an "Environment Variables" button, click it
- The window that gets opened should have two sections,
  to change the user environment variables, we will change
  the value on the top
- Scroll the top slider down, and see if you can find "Path"
- If it exists, select it, and click the Edit button
  The "value" field should be selected, press the "end" key
  to move the cursor to the end of the value.  It also
  deselects the value.  We don't want to replace it, we just
  want to add to the end
  Add the following:


- If no Path variable exists here, click the "New" button
  Specify the variable name as "Path", and the  value as:


  Note: Use the actual directory containing your Python executable

- Click "ok" to close the "Edit User Variable" window
- Click "ok" to close the "Environment Variables" window
- Click "ok" to close the "System Properties" window

Now, open a new command prompt (the changes that you made to no change any open command prompts!)

Now, you should be able to type:

python -V

Author Closing Comment

ID: 35172974
Thank you i did it manually ... for time frame..
LVL 41

Expert Comment

ID: 35180157
Thanks for the grade & points.

Good luck & have a great day.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Starting up a Project

636 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