?
Solved

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

Posted on 2011-03-16
13
Medium Priority
?
625 Views
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
T:/Project_ccc//libLankini/rahul.txt
T:/Project_dda//lib/ani_Final.bat
T:/Project_s4f//lib//lanka.avi
0
Comment
Question by:armstrongnt
[X]
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
13 Comments
 
LVL 14

Expert Comment

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

http://stackoverflow.com/questions/106053/how-can-i-make-a-batch-file-to-act-like-a-simple-grep-using-perl
0
 
LVL 14

Expert Comment

by:sentner
ID: 35151175
findstr xxxxx <filename>
0
 
LVL 41

Expert Comment

by:HonorGod
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()
  sys.exit()

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
      Usage()
  else :
    print ' Error: Specified directory not found: %s\n' % dirname
    Usage()

def process( dirname ) :
  pat = re.compile( 'file\s+-rdi.+"([^"]+)".+"([^"]+)".+"([^"]+)"' )
  names = os.listdir( dirname )
  names.sort()
  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 :
        fh.close()

Open in new window

0
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

by:sentner
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

0
 

Author Comment

by:armstrongnt
ID: 35152119
@HonorGod:

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..
0
 
LVL 14

Expert Comment

by:sentner
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).
0
 

Author Comment

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

Expert Comment

by:sentner
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
"T:/Project_ccc//libLankini/rahul.txt";
"T:/Project_dda//lib/ani_Final.bat";
"T:/Project_s4f//lib//lanka.avi";


0
 
LVL 41

Expert Comment

by:HonorGod
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

or


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

which do you get?
0
 

Author Comment

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

Accepted Solution

by:
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:

  ;C:\Python27\scripts;C:\Python27

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

  C:\Python27\scripts;C:\Python27

  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
0
 

Author Closing Comment

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

Expert Comment

by:HonorGod
ID: 35180157
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
Introduction to Processes
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 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