Jump to content


Photo

Help testing New script...


  • Please log in to reply
1 reply to this topic

#1 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 07 September 2006 - 03:50 AM

Hey all,
I have written(with help from forums :D) This script that recursivly goes through a directory and removes any charactures that windows does not like. EG, anything that cant be in a file name. And removes them
However, this is going to be run on lots and Lots of data, and we CANT backup it all first. thus i wanna make sure it is going to work.
Could i please have some help testing it on different data, I have done lots of testing, but as everyone knows, no one programmer is perfect, and im sure there will be a bug. And because i wrote it. I wont notice it :S but if someone else does, they might find it :)
So any help?
Thanks...


#!/usr/bin/php
<?PHP
$dir = '/tmp/TESTING';
$replace_cnt = 0 ;
$file_cnt	 = 0 ;
$dir_cnt	 = 0 ;

function remove_bar_char($open_dir){
	global $dir ;
	global $replace_cnt;
	global $file_cnt ;
	global $dir_cnt ;
	$handle = opendir($open_dir);
	echo "\nOPENING: " .$open_dir ."\n" ;
	$dir_cnt++ ;
	while(($file = readdir($handle)) !== false){
		$path = $open_dir . DIRECTORY_SEPARATOR . $file;
		if (is_file($path) && !in_array($path, array('..', '.'))){
			$file_cnt++ ;
			if (preg_match('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', $file)){
				echo "Bad file found: " .$path ."  \n" ;
	           	if (is_file($path) && !in_array($path, array('..', '.'))){
		            $contents = file_get_contents($path);
	            	$temp_path=explode("/", $path) ;
	            	for($i=0; $i < count($temp_path); $i++){
	            		$temp_path[$i]=preg_replace('/[' .preg_quote('\/ : * ? " < > |', '/') .']/', '', $temp_path[$i]);
	            	}
	            	$newPath=implode("/", $temp_path) ;
	            	echo "\t Replacing with " .$newPath ."\n" ;
	            	$replace_cnt++ ;
		            if (file_put_contents($newPath, $contents) !== false){
		               unlink($path); //delete old file only if able to create new
		          	}
		        }elseif(is_dir($path) && !in_array($path, array('..', '.'))){
		    		$new_dir = $dir . DIRECTORY_SEPARATOR . $file;
		    		remove_bar_char($new_dir) ;
	    		}
	    	}
		}elseif(is_dir($file) && !in_array($file, array('..', '.'))){
			$new_dir = $dir . DIRECTORY_SEPARATOR . $file;
			remove_bar_char($new_dir) ;
    	}
	}
}

echo "<------------Starting Replace!------------>\n" ;
remove_bar_char($dir);
echo "\n";
echo "\n";
echo " There were $replace_cnt replacments made with $file_cnt files found, in $dir_cnt directories \n" ;
echo "<------------Replace Finnished------------>\n" ;
?>


#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 07 September 2006 - 07:30 AM

That code is sufficiently complex that I can't spend the time to analyze it..

BUT, there are two good testing techniques you can use.

The first is to create a test directory structure, complete with test files inside, and see if it handles that correctly.

The second is to have it print out everything it will do, but not actually DO anything.  Essentially, you will comment out all the parts where files are created or deleted.  Then you can read through the list of changes and look for anything odd.  The only problem there is that it will fail when a directory should have been renamed.  So for directory renaming, you will have to rely on the first testing technique only.

I have one concern - I don't see any test to see if $newPath already exists, such as "if (file_exists($newPath)) { ... }"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users