-
-
/*
- Convert all .txt files in a certain directory into corresponding htm files in batches. The htm files contain easy-to-read css styles
- generated htm files Place it under the htm directory in the same directory
- Parameter 1: The path of the directory to be converted
- Execute php txt2htm.php "C:\txt\"
- php txt2htm.php "/tmp/txt/"
- php txt2htm.php .
- */
$basedir=$argv[1];
- if(!$basedir||!is_dir($basedir))
- {
- die("please input dir.n");
- }
- //Change working directory
chdir($basedir);
$d = dir(".");
- //Create output directory
$outputdir="./htm/";
- if(!is_dir($outputdir)){
- mkdir($outputdir, 0700);
- }
- //Determine whether the creation is successful< ;/p>
if(!is_dir($outputdir))
- {
- die("cannot mkdir.n");
- }
- while (false !== ($entry = $d->read ()))
- {
- //Judge whether it is a file
if(is_file($entry))
- {
- $filename=strtolower($entry);
- //Judge whether it is a txt File
if(stristr($filename,".txt"))
- {
- $wfile=$outputdir.basename($filename,".txt").".htm";
- //If the file already exists, skip
if(file_exists($wfile))
- {
- echo "**********".$wfile." is exists ,skip this file****************n";
- continue;
- }
- if($str=file_get_contents($entry))
- {
- //Writing style, and newline
-
$str=" ".str_replace("n", "n ",$str);
- if($fp=fopen($wfile,"w"))
- {
- if (fwrite($fp,$str) === FALSE) {
- //write Entry failed
echo $wfile." cover fail! fwrite failn";
- }else{
- echo $wfile." cover success!n";
- }
- fclose($fp);
- }else{
- //Failed to create file
echo $wfile." cover fail! fopen failn";
- }
- }else{
- //Failed to read
- < ;p> echo $wfile." cover fail! file_get_contents failn";
- }
- }
- }
- }
- $d->close();
- ?>
-
Copy code
Run:
Effect:
|