# Convert Mac CR to Windows CRLF for all .cgi, .html, .pl, and .txt files 
# in a directory and its subdirectories.
#
# Notes:
# 1. This script reads in each file in one gulp, so it's not appropriate
# for use with very large files.
#
# 2. If you change the extensions below, be sure you only specify extensions 
# to TEXT files. If you specify an extension to binary files, the script
# may well decide these binaries need converting, which can corrupt them.

use strict;
use File::Find;

my $scanDir = 'C:\Astell-THQ\wikitest';

print "Converting Mac CR to Windows CRLF \n";

find sub 
{
   return if ( not -f $_ );                          # Skip everything but files.
   my $dot = rindex( $_, "." );                      # Get the location of the extension dot.
   my $ext = substr( $_, $dot ) if ( $dot != -1 );   # Get the extension.
   my $file = $_;                                    # Get the file name.

   $ext =~ tr/A-Z/a-z/if ( $dot != -1 );             # Lower case the extension

   # Processes files with these extensions: cgi, html, pl, txt.
   # Also processes files with no extensions. WARNING: This
   # script assumes extensionless files are TEXT. Remove
   # "$dot == -1 or" below if you have extensionless BINARY
   # files (you don't want to change these).
   if ( $dot == -1 
        or $ext eq "cgi"
        or $ext eq "html"
        or $ext eq "pl"
        or $ext eq "txt" )
   {
      open( IN, "$file" );
      my $content = ;
      close( IN );

      unless( $content =~ /\n/ )
      {
         $content =~ s/\r/\n/g ;                     # Convert only if native \n not found.

         open( OUT, ">$file" ) or die( "Could not open $file for writing: $!" );
         print OUT "$content";
         close( OUT );
         print "$file converted \n";
      }
   }
}, $scanDir;