Avatar of Roccat
Roccat
Flag for United States of America asked on

script to instert text at the begining of every php file in a directory

Can you help me get started with writing a script that will insert text at the very top of ever php file in a directory? This is what I need.
<?php
if (!isset($_SESSION)){
 session_start();
 }
 ?>
Powershell

Avatar of undefined
Last Comment
Jeremy Weisinger

8/22/2022 - Mon
SOLUTION
David Favor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
gr8gonzo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Roccat

ASKER
Somehting like this is what I was thinking in powershell

Get-ChildItem "C:\Backup1" -Filter *.php | Foreach-Object {
Add-Content -path $_ (Get-Content "C:\Scripts\code.txt")
}

Just trying to get it to work now.
Jeremy Weisinger

Please test this as I have not but something like this might work:

$phpstr = @"
<?php
if (!isset($_SESSION)){
 session_start();
 }
 ?>
"@

Get-ChildItem "C:\Backup1" -Filter *.php | Foreach-Object {
    $content = Get-Content $_
    $phpstr | Set-Content $_ -Force
    $content | Add-Content -path $_ 
}

Open in new window

SOLUTION
gr8gonzo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Roccat

ASKER
Jeremy that script is similar to mine but I think the reason it does not work is because in this line "$content | Add-Content -path $_ " the $_ does not contain the path. It just conaitns the name of the file.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Jeremy Weisinger

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Roccat

ASKER
Thanks
Roccat

ASKER
Here is the final script that I used.  
$phpstr = get-content "C:\Scripts\code.txt"
Get-ChildItem "C:\htdocs" -recurse -Filter *.php | Foreach-Object {
    $content = Get-Content $_.FullName
    $phpstr | Set-Content $_.fullname
    $content | Add-Content -path $_.fullname
}
Jeremy Weisinger

Glad to help. Thanks for sharing your script. :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.