locked
Foreach loops output in Different files. RRS feed

  • Question

  • Hello Team,

    As a project requirement, i need the output of a foreach loop to be saved in a different file. (i.e). If a loop runs 100 times the output of the each instance of loop should be saved in a different file, So that i may get 100 output files.

    I do not want to use "append" option, Since this dumps everything into a single file.

    Kind Regards

    Sreeram.R


    Shriram

    Monday, July 15, 2019 10:35 AM

Answers


  • $nums = 1..10
    $i =0
    foreach($num in $nums){
    $i =$i+1
    $num | out-file c:\temp\FileName$i.txt
    }

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    • Proposed as answer by BOfH-666 Monday, July 15, 2019 11:21 AM
    • Unproposed as answer by BOfH-666 Monday, July 15, 2019 11:21 AM
    • Marked as answer by Shriram14 Monday, July 15, 2019 11:48 AM
    Monday, July 15, 2019 10:42 AM
  • This is really very simple once you learn basic PowerShell:

    1..100 |
        ForEach-Object{
        "some thing $_" | out-file FileNum$_.txt
    }

    OR

    for($i=1;$i -le 100;$i++){
        "some thing $i" | out-file FileNum$i.txt
    }


    \_(ツ)_/


    • Edited by jrv Monday, July 15, 2019 11:34 AM
    • Proposed as answer by BOfH-666 Monday, July 15, 2019 11:38 AM
    • Marked as answer by Shriram14 Monday, July 15, 2019 11:48 AM
    Monday, July 15, 2019 11:20 AM

  • $nums = 1..10
    $i =0
    foreach($num in $nums){
    $i =$i+1
    $num | out-file c:\temp\FileName$i.txt
    }

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    This is not a good way:

    It should be like this.

    foreach ($num in (1..10)) {
        $num | out-file c:\temp\FileName$num.txt
    }

    No need for so many variables and statements.


    \_(ツ)_/


    • Edited by jrv Monday, July 15, 2019 11:23 AM
    • Proposed as answer by BOfH-666 Monday, July 15, 2019 11:38 AM
    • Marked as answer by Shriram14 Monday, July 15, 2019 11:49 AM
    Monday, July 15, 2019 11:22 AM

All replies


  • $nums = 1..10
    $i =0
    foreach($num in $nums){
    $i =$i+1
    $num | out-file c:\temp\FileName$i.txt
    }

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    • Proposed as answer by BOfH-666 Monday, July 15, 2019 11:21 AM
    • Unproposed as answer by BOfH-666 Monday, July 15, 2019 11:21 AM
    • Marked as answer by Shriram14 Monday, July 15, 2019 11:48 AM
    Monday, July 15, 2019 10:42 AM
  • This is really very simple once you learn basic PowerShell:

    1..100 |
        ForEach-Object{
        "some thing $_" | out-file FileNum$_.txt
    }

    OR

    for($i=1;$i -le 100;$i++){
        "some thing $i" | out-file FileNum$i.txt
    }


    \_(ツ)_/


    • Edited by jrv Monday, July 15, 2019 11:34 AM
    • Proposed as answer by BOfH-666 Monday, July 15, 2019 11:38 AM
    • Marked as answer by Shriram14 Monday, July 15, 2019 11:48 AM
    Monday, July 15, 2019 11:20 AM

  • $nums = 1..10
    $i =0
    foreach($num in $nums){
    $i =$i+1
    $num | out-file c:\temp\FileName$i.txt
    }

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    This is not a good way:

    It should be like this.

    foreach ($num in (1..10)) {
        $num | out-file c:\temp\FileName$num.txt
    }

    No need for so many variables and statements.


    \_(ツ)_/


    • Edited by jrv Monday, July 15, 2019 11:23 AM
    • Proposed as answer by BOfH-666 Monday, July 15, 2019 11:38 AM
    • Marked as answer by Shriram14 Monday, July 15, 2019 11:49 AM
    Monday, July 15, 2019 11:22 AM
  • Another approach could be something like this:
    for ($i = 0; $i -lt 100; $i++) {
        #  do something useful
        $LogFile = Join-Path -Path 'C:\logFolder' -ChildPath ("LogFile-{0,3:000}.log" -f $i)
        $i | Out-File -FilePath $LogFile
    }

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Monday, July 15, 2019 11:31 AM
    Monday, July 15, 2019 11:30 AM
  • Thanks Jrv ...

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    Monday, July 15, 2019 11:33 AM
  • If you want to force the number format this is easier:

    for($i=1;$i -le 100;$i++){
        $i | Out-File ('C:\logFolder\LogFile-{0,3:000}.log' -f $i)
    }
    


    \_(ツ)_/

    Monday, July 15, 2019 11:34 AM
  • Thanks Jrv ...

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    When coding in any language or system it is always important to avoid interim variables and unnecessary code. This eliminates many mistakes, makes the code easier to maintain, makes the code readable and simplifies debugging.

    See: PowerShell Style Guidelines


    \_(ツ)_/

    Monday, July 15, 2019 11:37 AM