• Powershell – Insert Part of FileName – into Each File Variable

    Home » Forums » Developers, developers, developers » DevOps Lounge » Powershell – Insert Part of FileName – into Each File Variable

    Author
    Topic
    #506105

    Hi folks,

    hows everyone doing this Sunday 🙂

    I am trying to insert part of a file name into each file. Inside each file there is an XX that needs to be replaced with the digit found in the filename.

    Step 1

    find the number in the filename
    Use that number to search inside the file and when it finds XX – replace XX with the number.

    File Name ——– Replace XX Value
    1. Emperor Penguin —- 1
    2. Galapagos Penguin —- 2
    30. Fin Whale —- 30

    So basically the file name contains the number that will replace the XX

    Code:
    $filenames = @("C:UsersPBDesktop1.Emperor Penguin.txt")
    
    $regex = '^([^-](dd).+'
    
    foreach ($file in $filenames) {
    
        $outfile = "$file" + ".txt"
    
        $ReplaceString = ($file | Split-Path -leaf) -replace $regex,'XX'
    
        Get-Content $file | Foreach-object {
           $_ -replace "XX",$ReplaceString
        } | Set-Content $outfile
    }
    

    I came accros this from stack
    http://stackoverflow.com/questions/15506524/insert-data-into-file-based-on-file-name-using-powershell

    but unfortunately my regex skills and something else I did to the code makes it bad form. I have googled to the end of the arctic and cant work it out:confused:

    Is there an easier or better way for me to insert the number found in the filename into each file XX

    thanks for your help

    pb

    Viewing 1 reply thread
    Author
    Replies
    • #1569474

      PB,

      No real need for RegEx if the file pattern is as shown. The following code will do the trick.

      Code:
      $SrcPath = "G:Testpb89TestFiles"
      $DstPath = "G:Test"
      
      $Filenames = Get-ChildItem -path $SrcPath -Filter "*.txt"
      
      ForEach ($file in $Filenames) {
      
          $outfile = "$DstPath" + $file.Name
      
          $ReplaceString = $file.BaseName.split(".")[0]
      
          Get-Content "$($file.FullName)" | 
            Foreach-object { $_ -replace "XX",$ReplaceString} | 
            Set-Content $outfile
      
      } #End ForEach
      

      I used your file names placed them in a directory called G:Testpb89TestFiles and placed the same line in each.
      This is a test to get the File Number XX from the file name.

      Run the program and here is what is in each file:
      1. Emperor Penguin.txt: This is a test to get the File Number 1 from the file name.
      2. Galapagos Penguin.txt: This is a test to get the File Number 2 from the file name.
      30. Fin Whale.txt: This is a test to get the File Number 30 from the file name.

      HTH :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1569477

      Hi RG,

      Aww thanks so much, I’ve been on this problem for 3 days.

      I was determined to code this – but 3 days later I had to admit defeat.:o

      Pass it over to those in the know

      We newbies have a pick and mix approach with code – but it always FAILS!

      I have about 500 of these to number – so it was either power shell or my whole next week doing it bit by bit, so you saved me from a big head ache

      This worked splendidly thanks ever so much!!! 😀

      Hope you have a great rest of Sunday now:)

      pb

    Viewing 1 reply thread
    Reply To: Powershell – Insert Part of FileName – into Each File Variable

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: