Q:

Move batch images matching with folder name

I got  250000 image files and 3100 empty folder. I need to move all the files to appropriate folder. I can do this one by one but it would take months to sort and move all the files. Is there any easy way to this?
Example:
Folder name | File name
111111      | 111111_01.jpg
222222      | 111111_02.jpg
            | 111111_03.jpg
            | 222222_01.jpg
            | 222222_02.jpg
            | 222222_03.jpg

Thanks

A:

Thanks for your replay. My problem is solved, I am posting my solution here if anybody have same kind of problem, this might help.
I deleted all the empty folders and used this php script to create folder, move images and rename all the files.
"files"; where all my images are located and "output" where all the create folders and renamed files will be stored.
<?php
$input_dir = "files";
$output_dir = "output";

$all_files = scandir($input_dir);
$count = 0;
foreach($all_files as $file){
    $ip_file = $input_dir."/".$file;
    if(file_exists($ip_file) && strlen($file) > 6){
        $name = explode("_",$file);
        $op_dir = $output_dir."/".$name[0];
        $op_file = $op_dir."/".$name[1];
        if(is_dir($op_dir)){
            rename($ip_file,$op_file);
        }else{
            mkdir($op_dir);
            rename($ip_file,$op_file);
        }
    }
}

