Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Alter many files, all with same variable in them (http://www.programmingforums.org/showthread.php?t=14964)

woocha Jan 17th, 2008 9:59 AM

Alter many files, all with same variable in them
 
I have 100's of files that access a field in a mySQL DataBase. I had to change the field name and now I have to update 100's of files. The files read:
:

$sql = 'SELECT user_id, username, user_colour, user_birthday FROM '
I need it to now read:
:

$sql = 'SELECT id, nick, user_colour, user_birthday FROM '

I know I could go through all files and execute a replace 'user_id' with 'id', but that would take forever. Does anyone know a program to do this easier?

Sane Jan 17th, 2008 11:17 AM

Re: Alter many files, all with same variable in them
 
LOL!

Folks. A perfect example of why you use common code and constants.

I know it's too late now woocha, but for future reference: if you want to perform the same sql query in multiple places, wrap it in a common function. Similarily, for databases, table names (and maybe even column names), assign them to constant variables.

That would make this process so much easier.

All I can suggest now is that you could write a simple script in a language such as Python or PHP, with a primary function to search and replace the contents of every file in a directory. This wouldn't be so hard to whip up quickly.

P.S. I don't think you should have 100 PHP files... Either you're exaggerating, or you've done something very wrong. The only projects I've seen like that are large-scale reputable open-source web applications such as Wordpress and Xoops. And even then, it still may not even be as many as 100. If the project you're working on is really that large, I'm surprised you haven't figured out the convention of common code and constants yet.

ZenMasterJG Jan 17th, 2008 12:07 PM

Re: Alter many files, all with same variable in them
 
If you're on *nix and you want to replace EVERY INSTANCE of user_id with id in EVERY FILE IN A DIRECTORY, do the following:
1) cd to the directory you want to do the global replace in.
2) execute this command:
:

for file in `ls`; do mv $file $file'.OLD' ; nawk ' { gsub("user_id", "id", $0) ; print $0 } ' $file'.OLD' > $file ; done
check the files are now correct. if they're not, no worries, the old versions just have .OLD at the end of them now. If the replace worked ok, just delete all the *.OLD files.

If you need to replace just certain instances of user_id, i can help you narrow down that awk business a bit. I was just being lazy and its been a damn long time since I've played with awk ;)

mbd Jan 17th, 2008 12:09 PM

Re: Alter many files, all with same variable in them
 
i use TextMate

woocha Jan 17th, 2008 2:20 PM

Re: Alter many files, all with same variable in them
 
Thanks for the inut guys. I acquired custome photohosting software and am trying to combine phpbb3 with phpauction3 and my photohosting software. I might have exaggerated 100's file, but it is probably pretty darn close.
thanks to all :)

Jimbo Jan 17th, 2008 11:26 PM

Re: Alter many files, all with same variable in them
 
You could also do it with sed, again assuming a *nix system or gnu tools installed. I'm rusty, but something along the lines of:
:

for file in `ls`;
do
    sed -i "s/SELECT user_id, username,/SELECT id, nick,/g" $file;
done

If you want a backup copy, use -i.bkup


All times are GMT -5. The time now is 3:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC