Improve company productivity with a Business Account.Sign Up


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
  • 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
- execute as:

  python directoryName

- e.g.,

  python .
'''Command: %(cmdName)\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) <directory>\n
Example: python %(cmdName) .\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 = line )
          if mo :
#           print 'Groups:', mo.groups()
            print 3 )
      except :

Open in new window

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This article presents several of my favorite code snippets.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

588 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