Skip to content

Instantly share code, notes, and snippets.

@Tolmark12
Created June 2, 2009 14:39
Show Gist options
  • Save Tolmark12/122264 to your computer and use it in GitHub Desktop.
Save Tolmark12/122264 to your computer and use it in GitHub Desktop.
<?
function fileMover( $src, $root )
{
echo "processing: $src\n";
// Open source
$sourceDir = opendir($src);
// Recurse through contents
while ($file = readdir($sourceDir)) {
// Avoid (.) (..) and (.DSStore)
if ( substr($file, 0, 1) != "." )
{
// item location
$target = "$src/$file";
// If this is a directory...
if (is_dir($target)) {
//...recursive call on dir:
fileMover( $target, $root );
}
// Else, this is a file:
else {
// Create a new directory out of the first 4 characters
$newDestinationDir = $root.'/'.substr($file, 0, 4);
if (!is_dir($newDestinationDir)) mkdir($newDestinationDir,0755);
copy( $target, "$newDestinationDir/$file" );
}
}
}
closedir($sourceDir);
}
// Directory and subs with files
echo "Choose a source directory\n";
$src = trim(fgets(STDIN));
//Place to create our new directories
echo "Choose a destination directory\n";
$dst = trim(fgets(STDIN));
fileMover( $src, $dst );
echo "All files successfully copied to new $dst";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment