Mass file renamer... Tricky, Please help :-(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Mass file renamer... Tricky, Please help :-(

Post by Chris Corbyn »

Hi,

I'm trying to write this program which will copy some files in all subdirectories of a folder.

The file will copied if:

1. It has the extension .jpg
2. It is the only jpg in the folder

The idea is that if any single jpg in a subdirectory exists it will be copied to the same subdirectory on renamed to Front.jpg

The parent directory is "My Albums", my code so far creates an array of all the directories (and files if there were any) in that folder.

It then uses a foreach loop to open each directory and look for jpg's. My problem is counting the number of jpg's in each subfolder to determine if it's the only one.

Here's my code so far:

Code: Select all

<?php

function buildmp3() {
	
	$dirarray = array();
	
	$handle = opendir ('c:\My Albums');
	
	while ($folder = readdir($handle)) { /*Get contents of directory*/
		$dirarray[$folder] = $folder;
	}
	
	return $dirarray;
	
}

$dirarray = buildmp3(); /*Create the array of folder contents from the function above*/

foreach ($dirarray as $n => $v) {
	
	if ($n !="." && $n != "..") { /*Ignore .. and . directories*/
	
		$handle2 = opendir ('c:\my albums\''.$n.''); /*Open subfolders*/
		while ($contents = readdir ($handle2)) {
			if (substr($contents, -3) == "jpg") {
				/*I can't work out where to go from here*/
			}
		}
	
	}
}

?>
Any ideas or am I going down the wrong path here?

Thanks :-)
Last edited by Chris Corbyn on Fri May 28, 2004 4:13 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It basically needs to:

1. Open the "C:\My Albums" folder
2. Get a list of the contents
3. Open each subfolder
4. Get a list of each subfolder's contents
5. Count the number of jpg's present in each subfolder
6. If there is only 1 jpg in the subfolder, copy it to that same folder as Front.jpg

Sounds easy right?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I tried doing this... I've added into the foreach loop some code to build another array of all the jpegs in each directory and then print_r what it's got inside it.

The output seems a bit random to me, the arrays it creates don't only contain the jpegs for the specific subfolder but also some jpegs for other subfolders???? I'm getting really confused here. I set out on this task thinking it would be a ten minute job and I'm still here the next day trying to work it out lol :-)

Code: Select all

<?php

function buildmp3() {
	
	$dirarray = array();
	
	$handle = opendir ('c:\My Albums');
	
	while ($folder = readdir($handle)) { /*Get contents of directory*/
		$dirarray[$folder] = $folder;
	}
	
	return $dirarray;
	
}

$dirarray = buildmp3(); /*Create the array of folder contents from the function above*/

foreach ($dirarray as $n => $v) {
	
	if ($n !="." && $n != "..") { /*Ignore .. and . directories*/
	
		$handle2 = opendir ('c:\\My Albums\''.$n.''); /*Open subfolders*/
		
		while($content = readdir($handle2)) {

			if (substr($content, -3) == "jpg") {
				$jpgarray[] = $content;
			}
			
			print_r($jpgarray);
			echo '<p>';
			
			if (array_count_values($jpgarray) == 1) {
				echo "yes<br>";
			} else {
				echo "no<br>";
			}
		}
	
	}
}

?>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

To get directory contents, I am using

Code: Select all

<?php
function get_directory_contents($path){
	$mydirectory = opendir($path);
	while($entryname=readdir($mydirectory)){
		switch (true){
			case ($entryname==".")               : break;
			case ($entryname=="..")              : break;
			case (is_dir($path."/".$entryname))  : $return["dir"][]=$entryname; break;
			default                              : $return["file"][]=$entryname;
		}
	}
	closedir($mydirectory);
	return $return;
}

echo "<h3>directory contents</h3><pre>";
print_r(get_directory_contents("./"));
echo"</pre>";
?>
You get an array back containing the directory structure (incl. subdirectories and their contents) as well as files. Should be easy to filter out the files ending with .jpg with strstr.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Your second code is on the right track, try emptying the array before each subfolder.

Also, the line

Code: Select all

if (substr($contents, -3) == "jpg") {
should read

Code: Select all

if (strtolower(substr($contents, -3) == "jpg")) {
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

How do you empty an array? Is it unset($arrayname)? Thanks
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

yup
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Thanks guys... I got it working in the end. (Sorry patrikG) just kinda used your function totally unmodified for some of it :-( lol

Here's what i did in the end....

Code: Select all

<?php 

function get_directory_contents($path){ 
   $mydirectory = opendir($path); 
   while($entryname=readdir($mydirectory)){ 
      switch (true){ 
         case ($entryname==".")               : break; 
         case ($entryname=="..")              : break; 
         case (is_dir($path."".$entryname))  : $return["dir"][]=$entryname; break; 
         default                              : $return["file"][]=$entryname; 
      } 
   } 
   closedir($mydirectory); 
   return $return; 
} 

function getjpegs($location) {
	
	$jpegs = array();
	
	if (substr($location, -3) != ".db" && substr($location, -3) != "ini") {
		$foldercontents = get_directory_contents("C:\My Albums".$location."");
		
		foreach ($foldercontents['file'] as $s => $t) {
			if (substr($t, -3) == "jpg") {
				$jpegs[] = $t;
			}
		}
	}
		
	return $jpegs;
	
}

function buildmp3() {
	
	$dirarray = array();
	
	$handle = opendir ('c:\My Albums');
	
	while ($folder = readdir($handle)) { /*Get contents of directory*/
		$dirarray[$folder] = $folder;
	}
	
	return $dirarray;
	
}

$dirarray = buildmp3(); /*Create the array of folder contents from the function above*/


foreach ($dirarray as $key => $value) {
	
	$jpegs = getjpegs($value);
	
	echo '<pre>';
	print_r($jpegs);
	echo '</pre>';
	if (count($jpegs) == 1 && $jpegs[0] != "Front.jpg") {
		copy ('C:\\My Albums\''.$value.'\''.$jpegs[0].'', 'C:\\My Albums\''.$value.'\Front.jpg');
		echo 'File was copied for '.$value.'';
	} else {
		echo $value.'&nbsp;Not edited';
	}
	echo '<p>';

}


?>
It's very scrappy I know but I'm not too fussed when it's just for personal use.

Thanks everyone!
Post Reply