Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Search & Replace in Files Listed in Column C

$
0
0
Hi powershell friends,

I am having no luck sorting this problem out. :(

I have a CSV file Set Out Like this

FindString    ____ReplaceString____FileName
X1_____________123_____________1.txt
X1_____________389_____________2.txt
X1_____________098_____________3.txt

I am doing find and replacements in specific files in column C.

taken from:
http://powershell.org/wp/forums/topic/script-to-find-replace-multiple-strings-in-multiple-text-files-using-powershell/

$FolderLocation = 'C:\Users\PW\Desktop\a\'

$oFiles = Get-ChildItem -Path $FolderLocation\*.txt | ForEach-Object FullName

$findReplaceList = Import-Csv -Path $FolderLocation\a.csv       # CSV file


$totalitems = $oFiles.count
$currentrow = 0
foreach ($oTxtFile in $oFiles)
{
    $currentrow += 1
    Write-Progress -Activity "Processing record $currentrow of $totalitems" -Status "Progress:" -PercentComplete (($currentrow / $totalitems) * 100)
    [string] $txtFile = Get-Content $oTxtFile | Out-String
    
 
#### ADD COLUMN C - which has the filename
     # ForEach ($FileName in $findReplaceList)

    ForEach ($findReplaceItem in $findReplaceList)
    {
        $txtFile = $txtFile -replace "$($findReplaceitem.FindString)", "$($findReplaceitem.ReplaceString)"
    }
    
    $txtFile | Set-Content ($oTxtFile)
}


I have tried a lot of things and i cant get this to work :( :(

Where do I add column C - File Name so that the script will pick it up

thank you for your help

pw

Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>