Resend: DOS files

Walter Pienciak walter at frii.com
Wed Nov 24 11:07:35 CST 1999


From: llornkcor at llornkcor.com
Received: from ship (dialup-209.245.11.131.Denver1.Level3.net [209.245.11.131])
	by charon.host4u.net (8.8.5/8.8.5) with SMTP id OAA19674
	for <boulder-pm-list at happyfunball.pm.org>; Tue, 23 Nov 1999 14:37:36 -0600
Message-Id: <199911232037.OAA19674 at charon.host4u.net>
Date: Tue, 23 Nov 99 13:38:20 Mountain Standard Time
To: <boulder-pm-list at happyfunball.pm.org>
Subject: DOS files
X-Mailer: llornkcor Mail

Well, since I couldn't come up with a way to check the file while loading it into TextUndo
to remove the \r\n, if needed. Here's what I came up with. Please critique.
I check it first to save writing if it's not needed.

sub removeEnd($) {
    my $fro = 0;
    if (open(TEXTFILE, $file_name)) {
	while (<TEXTFILE>) {
	    if($_=~m/\r\n/) { $fro = 1; }
	}
	if ($fro eq 1) {
	    close(TEXTFILE);
	    print "$file_name is a DOS file, and needs to be converted\n";
	    $file_Bak=$file_name."\.BAK";
	    $TmpFile = $file_name . "\.TMP";
	    if (!open(FILE, ">".$TmpFile)) {
		print "Error trying to  open a temp file\n";
		close(TEXTFILE);
		return;
	    }
	    open(TEXTFILE, $file_name);  ## Do I need to open again?
	    while (<TEXTFILE>) {
	    	$_ =~s/\r\n/\n/g;
		print FILE ($_);
	    }
	    close( FILE);
	    close(TEXTFILE);
	    rename($file_name ,$file_Bak);
	    rename($TmpFile ,$file_name );
	    print "$file_name has been renamed as $file_Bak\n";
	} else {   ## conversion not needed
	    close(TEXTFILE);
	    return;
	}
    } else {  # end of if open file
	print "Error trying to open file\n";
    }
return;
}

Now that I have this working, someone will probably give me a one liner to do the same.
:o)

LP






More information about the Boulder-pm mailing list