Jump to content

bulk renaming files


hyster

Recommended Posts

<h3>File Upload:</h3>
Select the path to files: <br />
<form action="mov1.php" method="get" enctype="multipart/form-data">
<input type="file" name="pathw" size="50" />
<br/>
<input type="submit" value="Select Path" />
</form>
</body>

<?php
if (isset($_GET["pathw"])) {

          echo ($_GET["pathw"])."</br>";  
          $path1 = pathinfo($_GET["pathw"]);
          $paths = $path1['dirname'];
          echo $path1['dirname'];
?>          
          </br></br>
<?php
           //path to files

          $pathw = $paths;

          //list files in folder
          foreach(glob($pathw.'/*.*') as $filename){         
                        echo $filename."</br>";         
          }

          //form to select what to replace with what
          ?> 
           <table align="center" width="200px" border="1">
               <form name="form1" method="get" action="mov1.php">
                     <tr>
                       <td colspan="2"><div align="center"><a href="mov1.php" target="_self">Reset</a></div></td>
                     </tr>
                     <tr>
                       <td>Path</td>
                                <td><input name="pathw" type="text" id="pathw" value="<?php echo $pathw; ?>"></td>
                     </tr>
                     <tr><td width="71">Replace</td><td width="301"><input name="rep" type="text" id="rep"></td></tr>
                     <tr><td>With</td><td><input name="with" type="text" id="with"></td></tr>
                     <tr><td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td></tr>
               </form>
           </table>
           
<?php
          //replace selected file name parts

          if (isset($_GET["rep"])) {
          $rep1=$_GET['rep'];
          $path=$_GET['pathw'];

                if (isset($_GET["with"])) {
                $with1=$_GET['with'];
                                                                      
                      foreach(glob($path.'/*.*') as $filename){ 

                            $path1 = pathinfo($filename);
                            $dir = $path1['dirname']; 
                            $name = $path1['filename'];
                                                
                            $strip =  str_replace($rep1, $with1, $name); 
                            $done = $dir."/".$strip; 
                            rename($filename,$done.".avi");
                            echo $done."</br>";
                            echo "<meta http-equiv=\"refresh\" content=\"0;URL=mov1.php?pathw=$pathw\">";
                      }
                }
          }
}
?>

I have many 1000's of video files I want to rename so I wrote a php script to do it locally on mc ps so security is not an issue.

the script works fine until it completes the find - replace cycle.

I have tried to code it so that it keeps the folder path until a new 1 is selected.

 

the script works by selecting a file in the folder u want, this is sent to another form where u select the string to replace with another string,

 

this part works fine.

 

I then set it to reset to the previous folder selected.

 

the problem I have when it refresh's is that instead of the file name being removed it then removes the last folder.

 

IE:

G:\vids\Show\season 1\S01E01.drops.hdtv.avi  //select file

 

becomes

G\vids\Show\season 1\ // so all the files in the folder are listed, then u can see what needs to be changed in the file name

 

but after it refresh's it becomes

G:\vids\Show\

 

how do I stop it from removing the last folder on the refresh??

hope this made sense

thanks for any help

 

 

Link to comment
Share on other sites

Here is your problem:

You want the directory of the file you choose in the form above.

 

$path1 = pathinfo($_GET["pathw"]);  
$paths = $path1['dirname']; //selects directory name of the filepath you choose.

 

You then copy the directory to a new variable:

$pathw = $paths;

You then refresh, setting the URL_QUERY_STRING as the current directory:

echo "<meta http-equiv=\"refresh\" content=\"0;URL=mov1.php?pathw=$pathw\">";

When it refreshes,

$path1 = pathinfo($_GET["pathw"]); Get the path info for the DIRECTORY. Since you sent the directory (not file name) in the URL_QUERY_STRING.
$paths = $path1['dirname']; //Choose the directory ONE LEVEL UP.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.