Jump to content


This topic is now archived and is closed to further replies.


Help testing New script...

Recommended Posts

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?

$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" ;
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" ;

Share this post

Link to post
Share on other sites
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)) { ... }"

Share this post

Link to post
Share on other sites


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.