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
$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