Search files with a special word


  1. Posts : 2
    Windows 7 32 bit
       #1

    Search files with a special word


    Hi, With command line, I want to search all the files *.txt in the directory C:\MyDirectory to find files that contains the word "tag" like xytag or Tag123 or /tAg. Regards
      My Computer


  2. Posts : 6,285
    Windows 10 Pro X64
       #2

    Install Grep for Windows - a port from Linux. I use it all the time.

    For example, for me to search all my html files in my web server root directory, for the word message:

    grep -i "message" *.html (-i means case insensitive search)

    For your case. it would be:

    grep -i "tag" *.txt

    You can even direct the output to a file if you want:

    grep -i "tag" *.txt > results.txt

    Great little program from SourceForge.

    If you want to see the output and save in a file, use another little Linux port of a program called tee which the author calls wtee.

    The you would do this:

    grep -i "tag" *.txt | wtee results.txt

    (wtee.exe has to be in a directory in your path environment variable)
      My Computer


  3. Posts : 2
    Windows 7 32 bit
    Thread Starter
       #3

    Thank you very mush for your help, it seem windows is not capable for doing this task by itself from command line not like linux did. Okay, so can you just tell me how to search for files with the extension *.txt in C:\MyDirectory if that is possible of course.
      My Computer


  4. Posts : 640
    Windows 7 Ultimate x64
       #4

    EDIT: This and my next post will help with your post above.
    I originally wrote this in response to your first post untill I read it again and realised it's not what you asked. I'll look into your first post tomorrow.

    If you only want to output a list of these files then try this in cmd, but it will give you the full path to the file not just the filename.

    dir /b /s /a:-d "C:\MyDirectory\*tag*" (output to screen)
    or
    dir /b /s /a:-d "C:\MyDirectory\*tag*" > results.txt (output to text file)

    use dir /? for a explanation of the switches

    results.txt will be in which ever folder your cmd prompt is in, eg your cmd prompt is C:\Windows\System32> then it will be found at C:\Windows\System32\results.txt. You can also specify a full path for the results.txt file.

    If your only searching a single directory, not sub directories then drop the /s and you don't get the full path.

    If this looks like it might work then have a play with the switches, although you might not be able to get exactly what you want.

    Please let us know exactly what format you want the results returned.

    ? = Any char, eg. \tag?? will find tag12, tagak, tagml etc... But won't find atag12, MyTag, ThisTag123 etc...

    * = Any number of any char, eg. \*tag* will find atag12, MyTag, ThisTag123, tag_me etc... \*.txt will find all text files, *.bmp will find all bitmap files, *tag*.txt will find all text files with the word tag in the filename etc...
      My Computer


  5. Posts : 640
    Windows 7 Ultimate x64
       #5

    You might prefer this to output filename and extension only.

    for /r "C:\MyDirectory\" %i in (*tag*.txt) do @echo %~nxi

    It can be modified to output the fullpath, you can find the modifiers here Microsoft Corporation, about half way down the page look for Variable with modifier and Variable with combined modifiers in the first column in a blue box.

    This command will output a space at the end of each line though.

    To output this to a text file you need to use >> results.txt.
    > creates or overwrites, >> creates or appends.

    And if you plan to use this in a cmd or bat file the %i and %~nxi needs to be %%i and %%~nxi.

    Sorry if I explained stuff you already know but I don't know your level of experience with cmd.
      My Computer


  6. Posts : 640
    Windows 7 Ultimate x64
       #6

    Thought I'd have a look quick look tonight. Give this a test and see if it works.

    findstr /i /s /m .*tag.* "C:\MyDirectory\*.txt" -- Outputs full path

    Check it out using findstr /? or here Microsoft Corporation.

    Grep could be better, I don't know, but if this works and you prefer this but you only want the filename let me know and I'll see if I can find a way.
      My Computer


  7. Posts : 6,285
    Windows 10 Pro X64
       #7

    I just tried findstr and grep.

    findstr found two files then throws a "Out of memory" error processing the 3rd which has 450+ hits of the search target.
    grep found and reported all with no problems.
      My Computer


  8. Posts : 640
    Windows 7 Ultimate x64
       #8

    @Ztrucker
    Don't know about the "Out of memory" message, it worked for me with a file 30.9mb in size that had 74993 hits. To count the hits I used - find /i /c "com" "E:\MyDirectory\*.txt"

    I would still consider using Grep because findstr would not work with 1 of my files I was using, although I don't know if Grep would do any better because something fishy was going on with that file.
    It was 61.8mb in size but when I copied the text into a new file the new file was 30.9mb and when I just deleted the text from the original file it was still 2 bytes in size when it should have been 0 bytes.

    I've attached the file with just a few words I put in if you would like to test Grep with it. I would also be intrested in knowing if findstr works with it on your computer. The OP would also probably like to know.

    EDIT: In the screen shot Fishy File was originally "Office XP Media....". I had already copied and changed the contents before posting the screen shot. New text document is the copy of the text from "Office XP Media...." pasted into the New text document.
    Attached Thumbnails Attached Thumbnails Search files with a special word-findstr.jpg  
    Search files with a special word Attached Files
    Last edited by Duzzy; 25 Feb 2012 at 07:33. Reason: Added screen shot
      My Computer


  9. Posts : 17,545
    Windows 10 Pro x64 EN-GB
       #9

    Absolutely no need to install third party tools. The Windows Search syntax for your search would be:
    Code:
    ext:txt content:tag
    (Search all files with extension txt for the string tag.)

    Here's an example. I created three txt files, Testfile 1 and Testfile 2 include string tag whereas Testfile 3 does not contain that string. First searching for all files with extension txt, syntax ext:tag:

    Search files with a special word-search_1.png

    As should be, Windows Search finds all three files. Now I narrow the search, searching files with extension txt containing string tag, syntax ext:txt content:tag:

    Search files with a special word-search_2.png

    Testfile 1 and Testfile 2 contain string tag, therefore they are found.

    Easy and fast.

    Kari
      My Computer


  10. Posts : 640
    Windows 7 Ultimate x64
       #10

    @Kari
    1st - The OP want's to do it from the command line
    2nd - It doesn't work with wildcard char to find, and I quote, like xytag or Tag123 or /tAg
    3rd - it's much slower than the command line if working with large files, eg. 30.9mb without the folder being indexed.
    4th - Forced to search subfolders if normally searched

    Actually number 2 is true and false for me, changing it to - ext:txt content:*tag*, and adding Tag123 or /tAg to one file it works but adding xytag it does not. File also contains avtag and copytagall in all test.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 16:31.
Find Us