Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post

    Powershell - Insert Part of FileName - into Each File Variable

    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:\Users\PB\Desktop\1.Emperor Penguin.txt")
    
    $regex = '^([^-](\d\d).+'
    
    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/1...ing-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

    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

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    PB,

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

    Code:
    $SrcPath = "G:\Test\pb89TestFiles"
    $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:\Test\pb89TestFiles 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
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. The Following User Says Thank You to RetiredGeek For This Useful Post:

    pb89 (2016-07-03)

  4. #3
    Star Lounger
    Join Date
    Jan 2016
    Posts
    77
    Thanks
    18
    Thanked 1 Time in 1 Post
    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.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •