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

Posted on 2011-03-16
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

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).
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline


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 500 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

746 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

12 Experts available now in Live!

Get 1:1 Help Now