cc has asked for the wisdom of the Perl Monks concerning the following question:

hi

I transfer TEXT files from linux to windows, but all files after transfer are currupt (unix formatted).
howto convert the unix format file to dos (unix2dos) using perl before ftp the files ?
#!/usr/bin/perl -w use strict; use warnings; use Net::FTP; use Net::Netrc; my $server = "192.168.1.1"; my $user = "anonymous"; my $password = ""; my $destination = "/IN"; my $File = "/home/transferfile"; my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3); $ftp or die "$server: cannot connect: $@"; $ftp->login ($user,$password) or die "$_: Could not login: " . $ftp->m +essage; $ftp->cwd($destination); # Put file $ftp->ascii; $ftp->put($File) or die "$server: cannot put $File: " . $ftp->message; $ftp->quit;

I must transfer these files in ASCII mode.

kind regards
cc

Replies are listed 'Best First'.
Re: convert files unix2dos using perl script
by Happy-the-monk (Canon) on Jun 20, 2004 at 22:44 UTC

    perl -i.bak -pe 's/\012/\015\012/g' infile

    Courtesy to "format_c" and others at perl-community.de

    Update:
    Come to think of it, the funny thing is, transferring the files in ASCII mode should actually have taken care of the newline translation: that's what I believed that mode was for. Maybe your ftp client or the host's ftp server have a bug there.

    Cheers, Sören

      It depends on the files whether the auto mode of many of the ftp clients will choose to use ascii or binary mode. The detection isn't always full proof especially with mixed files (i.e. some Word docs).

      Jason L. Froebe

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: convert files unix2dos using perl script
by hbo (Monk) on Jun 21, 2004 at 06:53 UTC
    Well, the ascii() method is supposed to enable line ending translation between differing clients and servers. You should try a manual ftp to see whether or not it works that way. If it doesn't, here's some code to do it yourself in place:
    open TR, "unix2dos $File |" or die $!; # Or FQ path $ftp->binary(); # We translate, not the server. $ftp->put(TR,$File); # must give remote name.
      thanks,but I get ERROR:
      "Bareword "TR" not allowed while "strict subs" in use at test.cgi line 28.
      Execution of test.cgi aborted due to compilation errors."

      on this line:
      $ftp->put(TR,$File); # must give remote name.

      any idea what could be wrong ?
      greetings
      cc
        Perl doesn't know you mean to pass a filehandle to that method, so it passes the bareword "TR" instead. You could try to pass \*TR instead.
        $ftp->put(\*TR,$File);
        Better yet, for a recent enough perl, use a lexical variable to hold the filehandle. It'll look cleaner.
        open my($tr), "unix2dos $File |" or die $!; # Or FQ path $ftp->binary(); # We translate, not the server. $ftp->put($tr,$File); # must give remote name.

        But, IMO, it's overkill to call an external program to handle this little task. Perl will convert line endings for you, when reading from a handle in text mode. Internally, on Windows, text data is Unix text data. So, all you need to do is open the file.

        open my($fh), $File or die $!; $ftp->binary(); # We translate, not the server. $ftp->put($fh,$File); # must give remote name.

        p.s. I really wonder why just transferring data in Ascii mode won't work. Are you sure you don't accidently have double carriage returns in your files?