Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3696348
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:43:42+00:00 2026-05-19T04:43:42+00:00

I need to create a script to search through just below a million files

  • 0

I need to create a script to search through just below a million files of text, code, etc. to find matches and then output all hits on a particular string pattern to a CSV file.

So far I made this;

$location = 'C:\Work*'

$arr = "foo", "bar" #Where "foo" and "bar" are string patterns I want to search for (separately)

for($i=0;$i -lt $arr.length; $i++) {
Get-ChildItem $location -recurse | select-string -pattern $($arr[$i]) | select-object Path | Export-Csv "C:\Work\Results\$($arr[$i]).txt"
}

This returns to me a CSV file named “foo.txt” with a list of all files with the word “foo” in it, and a file named “bar.txt” with a list of all files containing the word “bar”.

Is there any way anyone can think of to optimize this script to make it work faster? Or ideas on how to make an entirely different, but equivalent script that just works faster?

All input appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:43:42+00:00Added an answer on May 19, 2026 at 4:43 am

    If your files are not huge and can be read into memory then this version should work quite faster (and my quick and dirty local test seems to prove that):

    $location = 'C:\ROM'
    $arr = "Roman", "Kuzmin"
    
    # remove output files
    foreach($test in $arr) {
        Remove-Item ".\$test.txt" -ErrorAction 0 -Confirm
    }
    
    Get-ChildItem $location -Recurse | .{process{ if (!$_.PSIsContainer) {
        # read all text once
        $content = [System.IO.File]::ReadAllText($_.FullName)
        # test patterns and output paths once
        foreach($test in $arr) {
            if ($content -match $test) {
                $_.FullName >> ".\$test.txt"
            }
        }
    }}}
    

    Notes: 1) mind changed paths and patterns in the example; 2) output files are not CSV but plain text; there is not much reason in CSV if you are interested just in paths – plain text files one path per line will do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a piece of script that looks through a variety of
I need to create a bash shell script starting with a day and then
I need to write, or find, a script to create a Debian package, using
I need to have script.sh , that would create files f1.txt and f2.txt with
I need to search all the *.c source files in the path to find
I need to create a script where someone will post an opening for a
I need to create an ejabberd user from a PHP script. I also need
I'm writing a script and I need to create a loop that will execute
i have a php script where i need to create a zip archive with
I have simple table creating script in Postgres 9.1. I need it to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.