copying files from Unix to Win system

Tom Keller kellert at ohsu.edu
Mon Jun 24 12:15:44 CDT 2002


David Cross in his excellent book "Data Munging with Perl" suggests 
the following:
#! /usr/local/bin/perl  -w
use strict;

(@ARGV == 2) or die "Error" source and target formats not given.";

my ($src, $tgt) = @ARGV;

my %conv = (
	CR => "\cM",	## Mac line ending
	LF => "\cJ",	## Unix
	CRLF => "\cM\cJ");	## Windows

$src = $conv{$src};
$tgt = $conv{$tgt};
$/ = $src;
while (<STDIN>) {
	s/$src/$tgt/go;
	## now do your data munging
}

I do a lot of moving of text files between Unix, Mac and Windows 
machines, and I've found this sniplet really useful.

Regards,
Tom K.


At 9:24 AM -0700 6/24/02, mikeraz at patch.com wrote:
>Hi All,
>
>I'm automating a process that receives text files via ftp and then mounts
>a SAMBA share to copy the files to a Windows based file server.
>
>The current method is open file, set $/ to "", slurp file, write 
>file and close:
>
>    open INFILE, "<$_" or die "cannot open local $_";
>    open OUTFILE, ">$tmnt/$dest/$_" or die "cannot open destination $_";
>    $fs = $/;
>    undef $/;
>    $cont = <INFILE>;
>    print OUTFILE $cont;
>    $/ = $fs;
>    close INFILE;
>    close OUTFILE;
>
>
>Problem with this approach is that the line endings are not converted.  Same
>problem for
>
>
>    while(<INFILE>) {
>         print OUTFILE;
>    }
>
>While I'm scratching my head over why this isn't being done automagically,
>I'm considering:
>
>    @lines_in_file = <INFILE>;
>    foreach (@lines_in_file) {
>	chomp;
>    }
>
>    $/ = "\015\012";  # from newlines in perlport
>    foreach (@lines_in_file) {
>   	print OUTFILE "$_$/";
>    }
>
>
>But that feels wrong as in there has to be a better way wrong.
>
>Now if someone is going to tell me that Perl has a built in file 
>copy funcation . . . .
>
>--
>     Michael Rasmussen  aka  mikeraz
>    Be appropriate && Follow your curiosity
>
>    "They that give up essential liberty to obtain
>    temporary safety, deserve neither liberty nor safety."
>				-- Benjamin Franklin
>
>    and the fortune cookie says:
>"The chain which can be yanked is not the eternal chain."
>-- G. Fitch
>TIMTOWTDI


-- 
Thomas J. Keller, Ph.D.
MMI Research Core Facility
Oregon Health & Science University
3181 SW Sam Jackson Park Rd
Portland, Oregon  97201
TIMTOWTDI



More information about the Pdx-pm-list mailing list