![]() |
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 ':
$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? |
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. |
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 ; doneIf 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 ;) |
Re: Alter many files, all with same variable in them
i use TextMate
|
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 :) |
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`; |
| 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