Search code examples
powershellsubdirectoryazure-powershellfile-access

Find & Replace text from multiple specific files from sub folders dynamically using powershell


I am working on azure CD pipeline, i want to change content of multiple files which exist in following folder structure.

MainFolder => SubFolder1 => myFile1.txt
              SubFolder2 => myFile2.txt
              SubFolder3 => myFile3.txt

I want to achieve my above requirement using powershell, and i have tried the following code.

$filepath = 'C:\Users\ashishjain06\Desktop\MainFolder'
$mydata = Get-ChildItem $filepath -include *.txt -recurse | Select-Object fullName
$totalRecords = $mydata.Count
for($x = 0; $x -lt $totalRecords; $x++)
{
    ((Get-Content -path $mydata[$x] -Force) -replace 'oldText','newText') | Set-Content -Path $mydata[$x]
}

When i run above code it's give me following output.

Get-Content : Cannot find drive. A drive with the name '@{FullName=C' does not exist.
At line:6 char:7
+     ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (@{FullName=C:String) [Get-Content], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+     ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+                         ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+     ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+                         ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+     ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+                         ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:6 char:25
+     ((Get-Content -path $mydata[$x] -Force) -replace 'oldText ...
+                         ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand

I am newbie on powershell, Please help me out to resolve this issue.


Solution

  • It's still an object with a property unless you do this:

    select-object -expandproperty fullname