Welcome Guest, Not a member yet? Register   Sign In
19,000+ file move
#5

[eluser]carvingCode[/eluser]
I've got the basic code worked out. Here's the working loclahost version:

Code:
<?php

$existing_dir = "C:\wamp\www\data\eh\insp_rpts\\";
$new_path = "C:\wamp\www\data\eh\ir_pdf\\";


echo "Starting...<br />";
echo "---------------------<br />";

// open a known directory, and proceed to read its contents
if (is_dir($existing_dir)) {
    if ($dh = opendir($existing_dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != "EHPBCONT.pdf") {          // special file - leave alone
                if (substr($file, -4) == ".pdf") {  // process only .pdf files

                    $ym = substr($file, -12, -6);   // extract YYYYMM from filename
                    $y = substr($ym, 0, 4);         // extrat YYYY
                    $m = substr($ym, -2);           // extract MM
                    $new_dir = $y . "\\" . $m;      // make directoty string

                    // mkdir

                    if (!file_exists($new_path . $new_dir)) {
                        mkdir($new_path . $new_dir, 0755, TRUE);    // 0755
                        echo $y . " - " . $m . "<br />";
                    } else {
                        echo $new_dir . " exists<br />";
                    }

                    // copy files
                    
                    if (!file_exists($new_path . $new_dir . "\\" . $file)) {
                        if (!copy($existing_dir . $file, $new_path . $new_dir . "\\" . $file)) {
                            echo "failed to copy $file...<br />";
                        }
                    } else {
                        echo "file exists: $file...<br />";
                    }
                }
            }
        }
        closedir($dh);
    }
}

My purpose has me extracting a year/month from the pdf's filename and using that, in YYYY/MM format, to create the new directory structure. mkdir's recursive option makes creating sub-directories a breeze.

To use this within my application, I decided it best to copy() rather than rename() as I'll need to do some modification to my application code to integrate the new directory structure. After I get everything worked out, I'll delete the old directory.


Messages In This Thread
19,000+ file move - by El Forum - 08-18-2010, 01:31 PM
19,000+ file move - by El Forum - 08-19-2010, 03:46 AM
19,000+ file move - by El Forum - 08-19-2010, 06:16 AM
19,000+ file move - by El Forum - 08-19-2010, 06:22 AM
19,000+ file move - by El Forum - 08-19-2010, 08:42 AM
19,000+ file move - by El Forum - 08-19-2010, 08:51 AM
19,000+ file move - by El Forum - 08-19-2010, 08:53 AM
19,000+ file move - by El Forum - 08-19-2010, 08:54 AM
19,000+ file move - by El Forum - 08-19-2010, 10:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB