elentz Posted October 7, 2009 Share Posted October 7, 2009 I have several hundred files that are named in a way that will not work for the way I want to use them. We saved these files with filenames and now we want to change the names. For example the files were named with a 4 digit number. It was an account number for the customer that the file was associated with. Now we use the phone number when we save the file. So now I have several hundred files in the wrong naming convention. I have a list of the customer phone numbers and the account codes. What I am looking for is some way to rename the files using the list to remove the account number and replace it with the phone number. Can someone suggest a method of accomplishing this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176861-can-php-do-something-like-this/ Share on other sites More sharing options...
MadTechie Posted October 7, 2009 Share Posted October 7, 2009 Do you have a way to link the account number to the phone number ? if so then yes, you could just use a loop to rename account to the phone.. Quote Link to comment https://forums.phpfreaks.com/topic/176861-can-php-do-something-like-this/#findComment-932554 Share on other sites More sharing options...
zq29 Posted October 7, 2009 Share Posted October 7, 2009 Something like this? foreach(glob("/path/to/files/*") as $f) { $acc = substr($f,0,strrpos($f,".")); $tel = mysql_result(mysql_query("SELECT `tel` FROM `table` WHERE `account`='$acc' LIMIT 1"),0); $c = file_get_contents($f); $h = fopen("/path/to/files/$tel","r"); fwrite($h,$c); fclose($h); } This is NOT tested! Quote Link to comment https://forums.phpfreaks.com/topic/176861-can-php-do-something-like-this/#findComment-932556 Share on other sites More sharing options...
elentz Posted October 7, 2009 Author Share Posted October 7, 2009 That was quick!! I do have a list of the old account numbers and phone numbers that I can put into a mySql table if that is what is needed or it is already in CSV. Quote Link to comment https://forums.phpfreaks.com/topic/176861-can-php-do-something-like-this/#findComment-932558 Share on other sites More sharing options...
MadTechie Posted October 7, 2009 Share Posted October 7, 2009 That was quick!! That's what my girl friend always say! I would probably do this <?php $lines = file('details.csv'); foreach ($lines as $line) { $details = explode(",",$line); $account = $details[1]; //second row = account $phone = $details[5]; //sixth row = phone $path = "/accounts/files/"; if(file_exists($path.$account) && is_file($path.$account)) { rename($path.$account.".txt", $path.$phone.".txt"); //rename account to phone echo "account $account changed to $phone<br />\n"; }else{ echo "<strong>account $account NOT found</strong><br />\n"; } } ?> I assumed the CSV had account on the second row and number on the sixths remember back and trial test EDIT: this isn't perfect but should give you the basic idea! Quote Link to comment https://forums.phpfreaks.com/topic/176861-can-php-do-something-like-this/#findComment-932565 Share on other sites More sharing options...
elentz Posted October 7, 2009 Author Share Posted October 7, 2009 Yeah my wife gives me that only in a look!! ) Anyway, I think I will try on a couple of the files and see what happens Thanks! This just saved me alot of time if it works ) Quote Link to comment https://forums.phpfreaks.com/topic/176861-can-php-do-something-like-this/#findComment-932602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.