[POWERSHELL] How do you put an element in a specific place in an array

Welcome Forums General PowerShell Q&A [POWERSHELL] How do you put an element in a specific place in an array

Viewing 2 reply threads
  • Author
    Posts
    • #230932
      Participant
      Topics: 3
      Replies: 7
      Points: 37
      Rank: Member

      Hello, i asked this question because I’m blocked !

      I’ll explain you the situation. in the folder name C:\test i have 2 directory.

      in the first directory I have one .txt name sentence.txt with the sentence “disovery of new planets”

      in the second directory I have an other .txt name sentence.txt with the sentence “it’s difficult to discover planet”

      I’ll to put each sentence in my column named “Sentence” but i don’t know how i can do it I try like this but without success :

       

      $path= Get-ChildItem "C:\test\"

      try {

      for ($i=0 ; $i -lt $path.Count ; $i++){

      $result = $path| Select-Object -Index $i
      $source = "C:\test\sentence.txt"

      $Data = Get-Content -Path $source
      $sentence= $Data[0].Split("r”)`

      <# $Display2 = $chemins | ForEach-Object -Process { Add-Member -InputObject $_ -MemberType NoteProperty -Name Commentaire -Value ($sentence) -PassThru } | Format-Table -Property Sentencecolumn -AutoSize #>

      <# function array { $d = New-Object PSObject $d | Add-Member -Name Creation -MemberType NoteProperty -Value $new_name $d | Add-Member -Name ID -MemberType NoteProperty -Value $i $d | Add-Member -Name Commentaire -MemberType NoteProperty -Value $sentence return $d } [email protected]() $Myarray+= ForEach-Object -Begin{Select-Object -Index $i} -Process{ array -Nom "$sentence[$i]" } #>

      else{}

      }

       

      but Each time my column Sentencecolumn is filled by the contained of the last directory but i don’t know why !

      Can you help me ? thanks !

       

       

      • This topic was modified 1 month, 1 week ago by Noyha56.
    • #230956
      Participant
      Topics: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero
      # Recreate environment
      md c:\test, C:\test\folder1, C:\test\folder2 -EA 0 
      “disovery of new planets” | Out-File C:\test\folder1\Sentence.txt
      “it’s difficult to discover planet” | Out-File C:\test\folder2\Sentence.txt
      
      # Script
      $RootFolder = 'c:\test'
      $FileName = 'Sentence.txt'
      $myOutput = foreach ($Subfolder in (Get-ChildItem $RootFolder -Directory)){
          try {
              $Content = Get-Content "$($Subfolder.FullName)\$FileName" -EA 1 
              New-Object -TypeName PSObject -Property @{
                  FileName = "$($Subfolder.FullName)\$FileName"
                  Sentence = $Content
              }
          } catch {
              Write-Warning "No '$FileName' file found under '$($Subfolder.FullName)' folder"
          }
      }
      $myOutput | FT -a 
      
    • #230995
      Participant
      Topics: 3
      Replies: 7
      Points: 37
      Rank: Member

      Thanks for the help i gonna try that

Viewing 2 reply threads
  • You must be logged in to reply to this topic.