Re: Search data with vbscript



On 16 mar, 00:11, "Richard Mueller [MVP]" <rlmueller-
nos...@xxxxxxxxxxxxxxxxxxxx> wrote:
mistral wrote:
I 'm trying to write vbscript that will do search for specific key
word or phrase (will be defined in script) inside PHP files in current
folder, include subfolders. Once key word is found, it just opens this
file and highlight this word.
Any help is appreciated.

I have an example VBScript program that searches for files that contain a
specified string linked here:

http://www.rlmueller.net/FindFiles.htm

The recursive subroutine SearchFiles demonstrates how to use the
FileSystemObject for this. You can specify the file extension, the starting
folder, the string to search for, and if sub folders are to be searched.

This example outputs the complete file name (with path) of each "hit". It is
expected that there will be several, perhaps many, file found with the
specified string. You could modify the script to instead load the files into
notepad or the Edit command. I don't know how to highlight the string, but
perhaps you could use code similar to:

' In the main program create reference to the wshShell object, so we can use
the Run method.
Dim objShell
Set objShell = CreateObject("Wscript.Shell")

' In Subroutine SearchFiles, replace this statement.
Wscript.Echo strFile
' with this statement.
Call objShell.Run("%comspec% /c notepad " & strFile, 2, True)

The "True" parameter is needed so the script will wait for you to exit
Notepad before opening the next file. Otherwise, all of the files will be
opened at once in separate instances of notepad.

You might be able to use the wshShell object to send keystrokes to the
notepad app to find the first instance of the string. However, there are
timing issues I have not resolved. When I tried I could not avoid having the
script open all files with the string at once. My attempt included the
following code in the subroutine SearchFiles:
=======
Dim blnSuccess
' ...

If (InStr(strText, LCase(strSearch)) > 0) Then
Call objShell.Run("%comspec% /c notepad " & strFile, 2)
Do Until blnSuccess = True
blnSuccess = objShell.AppActivate("notepad")
Wscript.Sleep 1000
Loop
objShell.SendKeys "%E"
Wscript.Sleep 100
objShell.SendKeys "F"
Wscript.Sleep 100
objShell.SendKeys strSearch
Wscript.Sleep 100
objShell.SendKeys "{Enter}"
Wscript.Sleep 100
intNumber = intNumber + 1
End If
========
This only worked if just one file was found. Otherwise, I see no way to have
the script wait for the user to exit notepad before proceeding to the next
file. If the "True" parameter is used with the Run method, the AppActivate
and SendKeys methods are not called until after the user exits notepad. One
solution is to quit after the first hit.

Finally, since writing the script linked above, I have found the command
findstr. Use can help at a command prompt with:

findstr /?

In my tests it does not appear to be faster than my script (strangely), but
it has regular expression capabilities. It might be possible to use it in a
batch file that also opens each file found, but I don't see how you can
highlight the string found in the file.
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab -http://www.rlmueller.net

----------------

thanks for helpful info. I tried run script from the run prompt in XP
with specified syntax like
cscript FindFiles.vbs <string> <path> <extension> <searchSubs>

but it not worked for me, just show popup Dos window. Probably
vbscript need be in same folder with files where we want to search?

Regards,
mistral
.



Relevant Pages

  • Re: recursive file editing
    ... > 2) each file examined should be edited; IF a string of this type is found ... > the edited output file should also be renamed according to the same rule ... script with the directory you want to process. ... for folder, folders, files in os.walk: ...
    (comp.lang.python)
  • Re: OT:macros or software?
    ... when a .PDF file is stored there, ... words and depending on weather those words are found in that document, move it to a predetermined folder. ... Easy enough if it were a text document, or even Word, as you could write a DOS script, or a VBS script if you had to load Word to read Word documents, although writing such a script can be harder to a beginner than some might think. ... If the string was in the name of the file it would be less work - you can set up the rather marvellous "robocopy" but that won't read inside a file, and certainly not a pdf! ...
    (uk.comp.homebuilt)
  • Re: How to rewrite with awk?
    ... > I'm unfamiliar with tools such as sed & awk. ... Extract the string that matches a RE. ... This script will not only expand all the lines that say "include ... file) and not resetting ARGV(the tmp file), it then lets awk do any ...
    (comp.unix.shell)
  • Re: Slow string
    ... I see virtually no difference in execution time for the code you posted when using cscript versus using wscript; to confirm, I bracketed the code with an initial ... Are you actually entering the script name into a console window? ... You can use the exact same line for every wrapper script - and that includes wsf and js scripts; all you need to do is change the final "vbs" to "wsf" or "js" as appropriate. ... If you build a large string by small concatenations, the performance degrades geometrically - so does array resizing, because they both use the same nasty technique inherited from VB1 of copying the entire data structure to a new structure with the added element. ...
    (microsoft.public.scripting.vbscript)
  • Re: rename files in directory
    ... When I run your script I get an error: " Microsoft VBScript runtime error: ... contain a " - " string. ... as part of the query. ... if the target folder has files with names in a format other ...
    (microsoft.public.scripting.vbscript)

Quantcast