Mass Rename files of a moved directory that had been batch added. Mass Rename files of a moved directory that had been batch added.
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Mass Rename files of a moved directory that had been batch added.

Started by donnoman, March 11, 2005, 02:50:07 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

donnoman

I needed to rename some directories that had already been batch added: this was my solution.

Thought it might be helpful to some other folks.


<html>
<head>
<title>fixes</title>
<style type="text/css">
.title {
  color:white;
  background-color:black;
}
.red {
  color:white;
  background-color:red;
}
.green {
  color:white;
  background-color:green;
}
</style>
</head>
<body>
<?php

$link 
mysql_connect('localhost''sql_user''sql_pass');  //replace with your mysql server, user, and password
if (!$link) {
   die(
'Could not connect: ' mysql_error());
}
mysql_select_db('cpg');  //replace with your database name

//load up the changes you made: FROM then TO
//you should probably use the full path, but mine were unique enough to forgo it.
$changes[]=array('old','new');
$changes[]=array('old2','new2');
$changes[]=array('old3','new3');
// knock yourself out, I used this with 30 changes at once, I have no idea how well it scales beyond that
echo "<table>";
foreach (
$changes as $key => $change) {
    echo 
"<tr class=\"title\">";
    echo 
"<td>$key</td>";
    echo 
"<td>{$change[0]}</td>";
    echo 
"<td>{$change[1]}</td>";
    echo 
"</tr>";
    
$results=mysql_query("SELECT filepath,pid,filename FROM cpg140_pictures WHERE filepath LIKE '%/{$change[0]}/%'");
    while (
$row mysql_fetch_assoc($results)) {
        echo 
"<tr>";
        echo 
"<td>{$row["pid"]}</td>";
        echo 
"<td>{$row["filepath"]}</td>";
        echo 
"<td>{$row["filename"]}</td>";
        echo 
"</tr>";
//        echo "<tr colspan=\"3\">";
        
$row['filepath']=addslashes(str_replace($change[0],$change[1],$row['filepath']));
        
$update_sql="UPDATE cpg140_pictures SET filepath='{$row['filepath']}' WHERE pid='{$row['pid']}' LIMIT 1";
        
$update_results=mysql_query($update_sql);
        if (
$update_results) {
          
$class="green";
        } else {
          
$class="red";
        }
//        echo "<td>$update_sql</td>";
//        echo "</tr>";
        
echo "<tr class=\"$class\">";
        echo 
"<td>{$row["pid"]}</td>";
        echo 
"<td>{$row["filepath"]}</td>";
        echo 
"<td>{$row["filename"]}</td>";
        echo 
"</tr>";
    }
}
echo 
"</table>";

?>

</body>
</html>

gereph

Hey,

As you know I am having trouble with this script. Below is the information that I hope you can use to help me resolve this problem/glitch.


<html>
<head>
<title>fixes</title>
<style type="text/css">
.title {
  color:white;
  background-color:black;
}
.red {
  color:white;
  background-color:red;
}
.green {
  color:white;
  background-color:green;
}
</style>
</head>
<body>
<?php

$link 
mysql_connect('????????''????????''????????');  //replace with your mysql server, user, and password
if (!$link) {
   die(
'Could not connect: ' mysql_error());
}
mysql_select_db('????????');  //replace with your database name
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
// knock yourself out, I used this with 30 changes at once, I have no idea how well it scales beyond that
echo "<table>";
foreach (
$changes as $key => $change) {
    echo 
"<tr class=\"title\">";
    echo 
"<td>$key</td>";
    echo 
"<td>{$change[0]}</td>";
    echo 
"<td>{$change[1]}</td>";
    echo 
"</tr>";
    
$results=mysql_query("SELECT filepath,pid,filename FROM cpg_pictures WHERE filepath LIKE '%/{$change[0]}/%'");
    while (
$row mysql_fetch_assoc($results)) {
        echo 
"<tr>";
        echo 
"<td>{$row["pid"]}</td>";
        echo 
"<td>{$row["filepath"]}</td>";
        echo 
"<td>{$row["filename"]}</td>";
        echo 
"</tr>";
//        echo "<tr colspan=\"3\">";
        
$row['filepath']=addslashes(str_replace($change[0],$change[1],$row['filepath']));
        
$update_sql="UPDATE cpg_pictures SET filepath='{$row['filepath']}' WHERE pid='{$row['pid']}' LIMIT 1";
        
$update_results=mysql_query($update_sql);
        if (
$update_results) {
          
$class="green";
        } else {
          
$class="red";
        }
//        echo "<td>$update_sql</td>";
//        echo "</tr>";
        
echo "<tr class=\"$class\">";
        echo 
"<td>{$row["pid"]}</td>";
        echo 
"<td>{$row["filepath"]}</td>";
        echo 
"<td>{$row["filename"]}</td>";
        echo 
"</tr>";
    }
}
echo 
"</table>";

?>

</body>
</html>


And here is the html ouput that I get when I run the script.


<html>
<head>
<title>fixes</title>
<style type="text/css">
.title {
  color:white;
  background-color:black;
}
.red {
  color:white;
  background-color:red;
}
.green {
  color:white;
  background-color:green;
}
</style>
</head>
<body>
<table>
<tr class="title"><td>0</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>1</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>2</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>3</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>4</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>5</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>6</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>7</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>8</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>9</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>10</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>11</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>12</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>13</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>14</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>15</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>16</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>17</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>18</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>19</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>20</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>21</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>22</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>23</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>24</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>25</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>26</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>27</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>28</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>29</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>30</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>31</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>32</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>33</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>34</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>35</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>36</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>37</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>38</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
<tr class="title"><td>39</td><td>uploads/cg_hentai/Mamori_Gami_Sama/</td><td>uploads/cg_hentai/Mamori_Kamisama/</td></tr>
</table>
</body>

</html>


As you can see if left out the database username etc. (For security reasons). As I mentioned before I am running version 1.4.2 of Coppermine. And the only code changes that I have made is that "Most Viewed" and "Top Rated" now only show 100 images instead of infinite. Although I seriously doubt that this is the problem. Can you rewrite the code for Coppermine 1.4.2 or higher if nessecary please.

Thanks, Gereph.
.

donnoman

A couple of things.

I created it just as a search and replace tool, the initial version assumed incorrectly that the change to be searched and replaced was not a top level folder under albums.

Therefore I've removed the leading and trailing "/"'s that the sql query added. That logic now needs to be taken care of by YOU the user of the script.

The filepath field in the database doesn't use a leading / but it does use a trailing slash at the end of the filepath.

Since it's search and replace you only need to add the terms once.

if you do this:


$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');
$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');


then it will simply repeat itself looking for the same terms, all you needed was this ONCE.


$changes[]=array('uploads/cg_hentai/Mamori_Gami_Sama/','uploads/cg_hentai/Mamori_Kamisama/');


This new version is wrapped with Coppermine. It will use your coppermine configuration as well as table names.  Just rename the attached file from renamer.txt to renamer.php, edit the changes array, and then place in your coppermine directory. Log in as an admin, then in your address bar replace index.php with renamer.php and it will run.

FYI this script only fixes your database AFTER you've moved the files. It makes no attempt to move the files for you.


<?php
define
('IN_COPPERMINE'true);

require(
'include/init.inc.php');

if (!
GALLERY_ADMIN_MODEcpg_die(ERROR$lang_errors['access_denied'], __FILE____LINE__);

pageheader();

//Search and replace: OLD to NEW
//you should probably use the full path, but mine were unique enough to forgo it.
//filepath does not include leading slashes but does include a trailing "/"
$changes[]=array('old','new');
$changes[]=array('old2','new2');
$changes[]=array('old3','new3');
// knock yourself out, I used this with 30 changes at once, I have no idea how well it scales beyond that
echo <<<EOT
<style type="text/css">
.title {
  color:white;
  background-color:black;
}
.red {
  color:white;
  background-color:red;
}
.green {
  color:white;
  background-color:green;
}
</style>
EOT;
echo 
"<table>";
foreach (
$changes as $key => $change) {
    echo "<tr class=\"title\">";
    echo "<td>Key: $key</td>";
    echo "<td>From: {$change[0]}</td>";
    echo "<td>To: {$change[1]}</td>";
    echo "</tr>";
$query_sql="SELECT filepath,pid,filename FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath LIKE '%{$change[0]}%'";
    $results=cpg_db_query($query_sql);
    echo "<tr>";
    echo "<td>pid</td>";
    echo "<td>filepath</td>";
    echo "<td>filename</td>";
    echo "</tr>";
    while (
$row mysql_fetch_assoc($results)) {
        $row['filepath']=addslashes(str_replace($change[0],$change[1],$row['filepath']));
        $update_sql="UPDATE {$CONFIG['TABLE_PICTURES']} SET filepath='{$row['filepath']}' WHERE pid='{$row['pid']}' LIMIT 1";
        $update_results=cpg_db_query($update_sql);
        if ($update_results) {
          $class="green";
        } else {
          $class="red";
        }
        echo "<tr class=\"$class\">";
        echo "<td>{$row["pid"]}</td>";
        echo "<td>{$row["filepath"]}</td>";
        echo "<td>{$row["filename"]}</td>";
        echo "</tr>";
    }
}
echo 
"</table>";
pagefooter();
ob_end_flush();

?>




gereph

Donnoman it works perfect. Sorry I took so long to reply but I think this site was giving trouble yesterday. Anyways thanks alot man. I owe you one.
.