Powershell - Find file and remove 1 line in file

Hi,

I have a directory, that looks like this:

Folder
-Subfolder
--File.ini
-Subfolder
--Subfolder
---AnotherFile.ini
-Subfolder
--Subsubfolder
--File.ini
-Subfolder

I would like to remove one line in File.ini in all subfolders, if the line contains a special word. Not in any of the subsubfolder, only in the subfolders.

It this possible and how can I accomplish this?

Thanks in advance!
LVL 1
Sum WumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You mean if AnotherFile.ini had a sibling File.ini in the same folder, you wouldn't want to have that one changed (and neither AnotherFile.ini)?
$searchstring = 'whatever'
get-childitem | ? { $_.PsIsContainer } | get-childitem -filter File.ini | % {
  (get-content $_) -replace "^.*$searchstring.*\$", "" | set-content $_ -Encoding ASCII

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sum WumAuthor Commented:
There will only be one file in each folder named like this. But I only want to remove a line of text in the file that is placed in the "subfolders".
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Above code should do that.
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Sum WumAuthor Commented:
It is missing a closing } somewhere. Where should it be placed?
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
At the very end (line 4).
0
Sum WumAuthor Commented:
I've rewrited yours a bit to this, and it works just as well (I forgot to mention that the file I was looking for was a hidden file):

$Path = 'C:\TEMP\'
$FileName = 'File.ini'
$DeleteString = 'LocalizedResourceName'

Get-ChildItem -Path $Path\*\* -Force -File -Filter $FileName `
	| % {(Get-Content $_) -notmatch $DeleteString `
	| Set-Content $_ -Encoding Ascii}

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Hidden - that's important to know ;-).

It is convenient (or at least I do so) to end a line with pipe. That way you do not need the backtick. And you should indent each foreach again, to make it more clear:
$Path = 'C:\TEMP\'
$FileName = 'File.ini'
$DeleteString = 'LocalizedResourceName'

Get-ChildItem -Path $Path\*\* -Force -File -Filter $FileName |
  % {
     (Get-Content $_) -notmatch $DeleteString | 
        Set-Content $_ -Encoding Ascii
  }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.