| 1. |
Solve : Finding text in files? |
|
Answer» The DOS DIR command is great, but it does have its limitations. I'm trying to find a particular word as it occurs in all files. and re-direct the results of the search to a text FILE. I'd also like to be able to exclude some folders (like that with my saved email) which are just going to complicate things. I obviously have done something wrong I told you to drive carefully. Actually you did nothing wrong, it was all my doing. I keep getting carried away, forgetting that batch code is not a programming language. So for my next trick, I went all procedural, which should make it easier to read, if nothing else. Code: [Select]@echo off setlocal enabledelayedexpansion :: :: Exclude array :: set array.1=c:\Paprport set array.2=c:\windows for /f "tokens=* delims=" %%f in ('dir c:\ /a:d /s /b') do ( set count=0 for /l %%i in (1,1,2) do ( if /i !array.%%i! EQU %%f set /a count+=1 ) if !count! EQU 0 call :search %%f ) goto :eof :search for /f "tokens=* delims=" %%d in ('dir %1 /a:-d /b') do ( find /i "standard" %%f\%%d > nul if not errorlevel 1 echo %%f\%%d >> FOUNDIT.TXT ) This code has been tested and appears to meet all your specs. Good luck. Quote from: Sidewinder on December 22, 2008, 03:57:21 PM QuoteI obviously have done something wrong I tried (actually am trying now) your new code, and it seems to be working. It's slow, but I expected that (Good;Fast:Cheap: You can only have 2 of them). Thanks so much for your help. And a Merry Christmas (or whatever you prefer) to you. TomThe code finished running, and it looks like it's back to square 1 now. The program found instances of the text I was looking for, and put the name of the file in the new file I wanted it to. Unfortunately, the instances it found were in a sub-directory of a directory I told it to exclude. It did not find the word in a file in the root directory I ran it from. When run with the /s switch omitted, nothing was found, and no new file was created. It indicated while running that access was denied to the directories I wanted excluded, however. The above was run on a machine using WIN2000. When I tried to run it on a machine with WIN98SE, it wouldn't run at all. It reported problems with the syntax. If I wasn't so stubborn, I'd give up, but there's an answer lurking there somewhere. TomWhile I was hunting down my Santa Claus outfit, I found a script in the snippet closet. With a tweak here and a tweak there, we could be talking about a serious script: Code: [Select]On Error Resume Next Const ForReading = 1 xDir = Array("c:\Paprport", "C:\Windows") 'Exclude array Set fso = CreateObject("Scripting.FileSystemObject") Set LogFile = fso.CreateTextFile("c:\temp\foundit.txt", True) Set dc = fso.Drives For each ds in dc Select Case ds.Driveletter Case "C" 'drive to search Set RootDir = fso.GetFolder(ds & "\") 'directory to start search GetTheFolders(RootDir) End Select Next LogFile.Close Function GetTheFolders(Folder) For Each Subfolder in Folder.SubFolders blnExclude = False For Each item In xDir If InStr(1, SubFolder.Path, item, 1) > 0 Then blnExclude = True Next If blnExclude = False Then GetTheFiles(Subfolder.Path) GetTheFolders Subfolder End If Next End Function Function GetTheFiles(FileFolder) Set f = fso.GetFolder(FileFolder) Set fc = f.Files For Each fs in fc If fso.GetExtensionName(fs) = "txt" Then 'Extension to look at Set k = fso.OpenTextFile(fs, ForReading) strFile = k.ReadAll k.Close If InStr(1, strFile, "standard", 1) > 0 Then LogFile.WriteLine fs End If End If Next End Function Yes I know, this is not batch code. Save the script with a vbs extension and run from the command prompt as cscript scriptname.vbs The script will scan the C drive looking for txt files. The drive can be changed and we can add more extensions, but there really is no point in checking exe files for a string. Happy Christmas to you and a Merry New Year. Quote from: Sidewinder on December 23, 2008, 04:48:49 PM While I was hunting down my Santa Claus outfit, I found a script in the snippet closet. With a tweak here and a tweak there, we could be talking about a serious script: Thanks for the code. It looks like it would find a string in a TXT file, but only there. What I was trying to do is find a particular string in files of any type, and to exclude directories where the string I was looking for couldn't possibly be found; and then to put the results into a file. (A word of explanation for my wanting to do a thing like that might be helpful here. It's for my wife, who is looking for something she stored on her computer, but can't remember the name of the file, nor where she put it; only what it was about.) Windows does the first PART of what I want to do with its Search (Find) function, but won't do the 2nd part. Nor will it put the results into a file. The previous code you sent looked like it would remedy those deficiencies, but it didn't work. I'd like to find out why it wouldn't do what it should have done. I'll understand if you throw up your hands at this point. I know I would if I were in your place. I'll keep plugging away at it, though. I hate not being able to figure things out. As I often say: I wish I understood all I know about computers. Tom Quote The previous code you sent looked like it would remedy those deficiencies, but it didn't work. I'd like to find out why it wouldn't do what it should have done. Which previous code? The VBScript worked fine when it was tested. The previous batch code had it's own set of problems. Anyway I find it easier use a tool that has the functionality I need to reach a solution. Personally I find batch code underpowered and logically exhausting. Quote Thanks for the code. It looks like it would find a string in a file.TXT file, but only there. What I was trying to do is find a particular string in files of any type, and to exclude directories where the string I was looking for couldn't possibly be found; and then to put the results into a file. The excluded directories are in the xDIR array and the file names that contained the search argument were logged to the foundit.txt. I used the temp directory on my system for the log file, you may not have that directory on your system. We can change it to a directory you do have. As mentioned, we can add extensions to include or create extensions to exclude. Did you run the VBScript? Can you document any specific problems? Quote from: Sidewinder on December 24, 2008, 08:51:25 AM QuoteThe previous code you sent looked like it would remedy those deficiencies, but it didn't work. I'd like to find out why it wouldn't do what it should have done. The previous code I referred to was the last batch code you sent. I ran the VBScript code you sent, and didn't get any results. I can't document any specific problems, since the code ran silently, showing nothing on the screen, All I know is that no file called "Foundit.txt" was created, even though I had specifically created a TXT file in the Root directory containing the string I was searching for. I'm not familiar with scripts, so I can't manipulate the code you sent in order to make the results appear on the screen; nor can I, for instance, eliminate sub-directories. It looks like I'm going to have to learn scripting, though, since your code, even though it didn't produce results, ran a lot FASTER than if it were a batch file. I do appreciate your efforts, and am sure that the failure to produce results lies with me, not you. TomQuote All I know is that no file called "Foundit.txt" was created, even though I had specifically created a TXT file in the Root directory containing the string I was searching for. Perfect! Well, you can always have the search script search for it's output file. Code: [Select]On Error Resume Next Const ForReading = 1 xDir = Array("c:\Paprport", "C:\Windows") 'Exclude array Set fso = CreateObject("Scripting.FileSystemObject") Set LogFile = fso.CreateTextFile("c:\temp\foundit.txt", True) Set dc = fso.Drives For each ds in dc Select Case ds.Driveletter Case "C" 'drive to search Set RootDir = fso.GetFolder(ds & "\") 'directory to start search GetTheFolders(RootDir) End Select Next LogFile.Close Function GetTheFolders(Folder) For Each Subfolder in Folder.SubFolders blnExclude = False For Each item In xDir If InStr(1, SubFolder.Path, item, 1) > 0 Then blnExclude = True Next If blnExclude = False Then GetTheFiles(Subfolder.Path) GetTheFolders Subfolder End If Next End Function Function GetTheFiles(FileFolder) Set f = fso.GetFolder(FileFolder) Set fc = f.Files For Each fs in fc If fso.GetExtensionName(fs) = "txt" Then 'Extension to look at Set k = fso.OpenTextFile(fs, ForReading) strFile = k.ReadAll k.Close If InStr(1, strFile, "standard", 1) > 0 Then 'LogFile.WriteLine fs wscript.echo fs End If End If Next End Function As before, save the script with a vbs extension and run from the command prompt as cscript scriptname.vbs Quote I'm not familiar with scripts, so I can't manipulate the code you sent in order to make the results appear on the screen; nor can I, for instance, eliminate sub-directories. I modified the script to display the results on the console. It was the sub-directories that gave me problems with the original batch file. With any luck, those problems have been eliminated with the VBScript. Note: c:\windows is excluded from the search, along with all it's sub-directories. Let me know if this is incorrect. Quote It looks like I'm going to have to learn scripting, though, since your code, even though it didn't produce results, ran a lot faster than if it were a batch file. Did you use Windows Search to look for the foundit.txt file (ironic, huh)? It should be in c:\temp. I can't be certain if the CreateTextFile method would create the directory if it was not found. I'm not suggesting it (I've been putting off downloading to my system), but Windows Desktop Search can be scripted through the Search object. Windows Update has been bugging me for moths to download it. Might be time to check it out. I think next Christmas I'll do without the Santa outfit. Sorry, the new code didn't work, either. As before, nothing showed on the console while it ran. A file called "Foundit.TXT" was created, but it had no contents. TomSUCCESS!! The latest code you sent did work after all. The fault, as I suspected, was mine. The word I had chosen to search for didn't exist in a TXT file, and therefore couldn't be found. When I substituted a word which did exist, the program ran and produced the appropriate result. Thanks. Now I have to figure out how to search in files with other extensions, or perhaps learn to exclude those extensions which won't contain the string (like EXE or COM files, or image files) 'll keep plugging away at it. TomQuote SUCCESS!! Glad it worked. Quote Now I have to figure out how to search in files with other extensions, or perhaps learn to exclude those extensions which won't contain the string (like EXE or COM files, or image files) Try creating an array of the extensions like the excluded directories. arrExt = Array("exe", "com", "jpg", "bmp", "png")....add as many as you need, each extension quoted with a comma separator Code: [Select]Function GetTheFiles(FileFolder) Set f = fso.GetFolder(FileFolder) Set fc = f.Files For Each fs in fc blnExclude = False For Each ext In arrExt If LCase(fso.GetExtensionName(fs)) = ext Then blnExclude = True End If Next If blnExclude = False Then Set k = fso.OpenTextFile(fs, ForReading) strFile = k.ReadAll k.Close If InStr(1, strFile, "standard", 1) > 0 Then 'LogFile.WriteLine fs wscript.echo fs End If End If Next End Function Good luck and Happy New Year! Considering all the file extensions on a PC, this could get interesting. |
|