I need to create a script for a user that will find all .txt files in a selected directory, look for a specific string in each of them, add a carriage return at the start of this specific string, and then output the edited .txt files into a directory that is one above the initial selected directory.
Preferably, it should be able to run without admin privileges.
To be clear, they desire an effect that would change the .txt file from this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas fringilla neque finibus velit condimentum, sed feugiat diam iaculis. In hac habitasse platea dictumst. TEXT STRING Vestibulum fringilla dui nec diam convallis, et cursus elit finibus. Vivamus porttitor est ac erat gravida pellentesque. Cras rhoncus urna a dui hendrerit auctor. Suspendisse potenti TEXT STRING. Interdum et malesuada fames ac ante ipsum primis in faucibus.
To this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas fringilla neque finibus velit condimentum, sed feugiat diam iaculis. In hac habitasse platea dictumst.
TEXT STRING Vestibulum fringilla dui nec diam convallis, et cursus elit finibus. Vivamus porttitor est ac erat gravida pellentesque. Cras rhoncus urna a dui hendrerit auctor. Suspendisse potenti
TEXT STRING. Interdum et malesuada fames ac ante ipsum primis in faucibus.
This is what I have come up with so far, and it stops working at the Get-Content line:
Function Get-Folder()
{
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
RootFolder = 'MyComputer'
ShowNewFolderButton = $true
}
[void]$FolderBrowser.ShowDialog()
$FolderBrowser.SelectedPath
}
$inputFolder=Get-Folder
foreach($file in Get-ChildItem -name $inputFolder\*.txt){
(Get-Content $file ).Replace("text","`r`n text") | Out-File $inputFolder\..\output\$_
}
pause
This is the error it throws when this script is run:
Get-Content : Cannot find path 'C:\powershell\testing.txt' because it does not exist. At C:\powershell\test_script_02.ps1:22 char:2 + (Get-Content $file ).Replace("text","
r
n text") | Out-File $inputFol ... + ~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\powershell\testing.txt:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommandYou cannot call a method on a null-valued expression. At C:\powershell\test_script_02.ps1:22 char:1 + (Get-Content $file ).Replace("text","
r
n text") | Out-File $inputFol ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNullGet-Content : Cannot find path 'C:\powershell\testing0.txt' because it does not exist. At C:\powershell\test_script_02.ps1:22 char:2 + (Get-Content $file ).Replace("text","
r
n text") | Out-File $inputFol ... + ~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\powershell\testing0.txt:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommandYou cannot call a method on a null-valued expression. At C:\powershell\test_script_02.ps1:22 char:1 + (Get-Content $file ).Replace("text","
r
n text") | Out-File $inputFol ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
It fails to find each text file in the path, which I know to exist. When the Get-Content line is commented out and replaced with a Write-Host command, the script correctly prints every file in the directory. I'm not sure how it seems to be failing to find the correct files.
I know that there must be better ways to do this, I'm very new to Powershell and this is just what I've cobbled together from web searching around. Any help is greatly appreciated.