Jump to content

Help testing New script...


Moon-Man.net

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?
Thanks...


[code]
#!/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" ;
?>
[/code]
Link to comment
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)) { ... }"
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.