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 7517393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:26:12+00:00 2026-05-30T01:26:12+00:00

I would like the file to save with the count variable between the file

  • 0

I would like the file to save with the count variable between the file prefix and the file extension. Out-File (file prefix + count variable + file extension). sort of thing. I’m ridiculously new to Powershell so please be merciful 😛

$max = get-content h:test1\test.txt | Measure-Object 
$h = get-content h:test1\test.txt
For($i=0; $i -lt $max.count ; $i++){
select-string h:test2\*.txt -pattern $($h[$i]) | Format-List | Out-File (Join-Path h:\text  $i)
}
  • 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-30T01:26:13+00:00Added an answer on May 30, 2026 at 1:26 am

    Simply use

    Out-File h:\text\$i.extension
    

    PowerShell’s parsing gives rise to a few very weird problem in edge cases, but overall it is designed to work as you would expect it to work in most, if not all, common cases.

    In your case I would probably solve the problem a little differently:

    Get-Content h:\test1\test.txt |
      ForEach-Object { $i = 0 } {
        Select-String H:\test2\*.txt -pattern $_ |
          Format-List |
          Out-File h:\text\$i.extension
        $i ++
      }
    

    The first block for the ForEach-Object in this case is run once at the start of the loop and initialises the counter variable. I tend to avoid explicit looping constructs (such as for (x; y; z) or foreach (x in y)) in favour of pipeline solutions.

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

Sidebar

Related Questions

I would like to download an mp3 file from some site, save it to
I am attempting to save an rtf file using FileDialog and would like to
In C# ASP.Net, I would like to create and save a text file to
I would like to parse an XML file and save this information into object.
I just would like to delete HTML tag and then re-save file in same
I would like to prompt the user for a save file dialog when he
When someone installs this plugin, I would like a file to be copied into
I would like a batch file to launch two separate programs then have the
I would like copy just file.xml without folder structure using overlays like this: <overlays>
I would like to move a file or folder from one place to another

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.