Solved

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

Posted on 2011-03-16
13
602 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

  ;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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 67
Windows Service to Receive TCP Packets 4 123
index Out OF Range Exception error 4 54
Reading text file in Python gives strange results 7 68
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

16 Experts available now in Live!

Get 1:1 Help Now