How to append a text to multiple files

March 18, 2011 at 04:41:26
Specs: Windows Vista
I have a 6 line php code in role.php
fwrite ( $file , $_SESSION["username"]."\t".$_SERVER['REQUEST_URI']."\t".$_SERVER['REQUEST_TIME']."\n" );

I want to append this to the beginning of all php files in a project directory and all its sub-directories
Any easy and accurate method please (program/script/command)?
I can also get the 6 lines in a single line as a string

The following worked for me to append at the end of the files for only in the directory and not sub-directories
for %f in (*.php) do copy %f+role.php %f
The resulting files also had an right arrow mark character at the end

For appending at beginning, I tried
%f in (*.php) do copy role.php+%f %f
it didn't work the resulting files had only a right arrow character

See More: How to append a text to multiple files

Report •

March 21, 2011 at 02:14:38
I placed this question in programming section as it was task automation related using scripting or programming. Someone moved this question to php section without reading properly.

Anyway I did it myself after long trial and error using command-line method.
The command:

cd C:\MyProject

for /r %i in (*.php) do copy /y "C:\role.php"+"%i" "C:\temp.php" /b & copy /y "C:\temp.php" "%i"

/r for all sub-directories
/y for by-passing confirm overwrite permission
/b for not appending end-of-file character

Report •
Related Solutions

Ask Question