How to suffix and prefix based on folder name?

Welcome Forums General PowerShell Q&A How to suffix and prefix based on folder name?

  • This topic has 1 reply, 2 voices, and was last updated 1 week ago by
    Senior Moderator
    .
Viewing 1 reply thread
  • Author
    Posts
    • #197672
      Participant
      Topics: 2
      Replies: 1
      Points: 8
      Rank: Member

      sorry english is not my first language i will add picture as an example to express my problem.

      assume i have multiple folders. lets say

      Second folder

      User1

      inside folder there is multiple text

      inside text there is multiple line. i want to add suffix and prefix based on folder name in each line like this

       

      how can i do this? it's bit complicated for me. kindly help please

      • This topic was modified 1 week ago by sam202037.
    • #197690
      Senior Moderator
      Topics: 8
      Replies: 1151
      Points: 3,995
      Helping Hand
      Rank: Community Hero

      Follow below steps.

      • Get the content of the folder user1 using Get-ChildItem cmdlet
      • Foreach file do a Get-Content on the file fullname ($_.FullName) at the same time store the file's parent folder base name in a variable $parent,
      • Do a "{1}/$_ {1}:{2}" -f $parent,$parent,$parent
      • Do a Set-Content at the end of Foreach-Object cmdlet.

      A pseudo code below

      $Folder = 'path to user1 dierctory'
      Get-ChildItem -Path $Folder -File | Foreach-Object -Process {
          $ParentFullPath = Split-Path -Path $_.FullName -Parent
          $ParentBase = Split-Path -Path $ParentFullPath -Parent
          $Newcontent = Get-Content -Path $_.FullName | Foreach-Object -Process {
             "{1}/$_ {1}:{2}" -f $ParentBase,$ParentBase,$ParentBase
          }
          $Newcontent | Out-File -FilePath $_.FullName 
      }
      
      • This reply was modified 1 week ago by kvprasoon.
      • This reply was modified 1 week ago by kvprasoon. Reason: Add code
Viewing 1 reply thread
  • You must be logged in to reply to this topic.