= 2) or die("You must provide a source directory.\n -- Program terminated!"); $readDir = $argv[1]; // if write dir not set - use read dir if ($argv[2]) { // check for dir if (is_dir($argv[2])) { $writeDir = $argv[2]; // group name to append to new files if ($argv[3]) { $group = ucfirst($argv[3]).'.'; } else { $group = ''; } } else { // assume argv[2] is group $group = ucfirst($argv[2]).'.'; // assign write dir to read dir $writeDir = $readDir; } } else { // assign write dir to read dir $writeDir = $readDir; $group = ''; } // get unix time stamp $now = time(); // set newline character $newline ='²'; // Aquire file and process // Open a directory, and proceed to read its contents if (is_dir($readDir)) { if ($dh = opendir($readDir)) { echo "Opening Directory: $readDir\n"; while (($fileName = readdir($dh)) !== false) { // check for text file - may want to use is_file() here if (substr($fileName, -4) == ".txt"){ // get file contents if ($fileContents = file_get_contents($readDir.$fileName)) { echo "Reading: $fileName\n"; // replace all newlines with $newline $fileContents = preg_replace("/\\r\\n/", $newline, $fileContents); // or is it "/\\n/" ? $fileContents = "newline=$newline\ntext=$fileContents\ntime=$now"; // create new file name $newFileName = $group.ucfirst(substr($fileName, 0, -4)); // write to new file. $handle = fopen($writeDir.$newFileName, 'w'); if (fwrite($handle, $fileContents)){ echo "Writing: $writeDir$newFileName\n"; } fclose($handle); } else { echo "Failed! Reading: $fileName\n"; } } else { echo "Skipping $fileName\n"; } } closedir($dh); echo "Conversion complete\n"; } } else { echo " Sorry, '$readDir' is not a valid directory. Please try again."; }