file name and FTP problem formatting date! Please Help!

Discussion in 'Perl Misc' started by Nex_s, Aug 2, 2005.

  1. Nex_s

    Nex_s Guest

    Hi All,

    I'm trying to format a date and store it in a variable for a file
    name. The file gets created but for some reason my FTP process sends
    an empty file with the same name.

    Any help would be greatly appreciated.
    Chris

    Here's how I'm formatting the date.

    $file = "dms_nspwr_" . `date +%b_%d_%y_%H%M.txt`;

    Here's my FTP code:

    $ftp = Net::FTP->new("123.456.789.10", Debug => 0)
    or die "Cannot connect to some.host.name: $@";

    $ftp->login("username",'password')
    or die "Cannot login ", $ftp->message;

    #$ftp->cwd("/pub")
    # or die "Cannot change working directory ", $ftp->message;


    $ftp->put("$file")
    or die "put failed $file", $ftp->message;

    $ftp->quit;
    Nex_s, Aug 2, 2005
    #1
    1. Advertising

  2. Nex_s

    Paul Lalli Guest

    Nex_s wrote:
    > I'm trying to format a date and store it in a variable for a file
    > name. The file gets created but for some reason my FTP process sends
    > an empty file with the same name.
    >
    > Any help would be greatly appreciated.


    > Here's how I'm formatting the date.
    >
    > $file = "dms_nspwr_" . `date +%b_%d_%y_%H%M.txt`;


    There's little need to shell this out to an external command. Check
    out the strftime() function in
    perldoc POSIX

    > Here's my FTP code:
    >
    > $ftp = Net::FTP->new("123.456.789.10", Debug => 0)


    You appear to not be using strict. Please make sure all your scripts
    that you ask for help with here start with
    use strict;
    use warnings;

    Also, as you seem to be having problems with the FTP class, perhaps it
    would be a good idea to enable debugging, to ensure that your program
    is doing what you think it's doing? Try replacing the end of that
    constructor with:
    Debug => 1)

    > or die "Cannot connect to some.host.name: $@";
    >
    > $ftp->login("username",'password')
    > or die "Cannot login ", $ftp->message;
    >
    > #$ftp->cwd("/pub")
    > # or die "Cannot change working directory ", $ftp->message;
    >
    >
    > $ftp->put("$file")


    This is a useless use of double quotes, and is a bad habbit to get
    into. Please read:
    perldoc -q quoting

    > or die "put failed $file", $ftp->message;
    >
    > $ftp->quit;


    Have you verified that the file exists in the current local directory?
    Have you verified that the file has content in the current local
    directory?

    Try adding these two lines before the $ftp->put():

    -e $file or die "File $file does not exist!\n"
    -s _ or die "File $file is empty!\n";

    and let us know the results of those lines plus the Debugging output...

    Paul Lalli
    Paul Lalli, Aug 2, 2005
    #2
    1. Advertising

  3. Nex_s

    Nex_s Guest

    Hi there,

    Thank you kindly for your response. I ran the code again the ftp
    process ran flawlessly until the put statement when the error came
    back:

    File dms_nspwr_2005_08_02_1404.txt does not exist!

    The file is in the same directory as the script and contains data. I
    even tried putting in sleep 10 in case it was necessary to give the
    server time to save the file but to no avail.

    Chris

    P.S. I tired explictly defining all my variables but ran into one
    problem with one used for my e-mail routine $mail. The line in
    question is:
    $mail{From} = 'Data Upload <>';
    Here's the error it gave me.

    Global symbol "%mail" requires explicit package name at line 147.




    Paul Lalli wrote:
    > Nex_s wrote:
    > > I'm trying to format a date and store it in a variable for a file
    > > name. The file gets created but for some reason my FTP process sends
    > > an empty file with the same name.
    > >
    > > Any help would be greatly appreciated.

    >
    > > Here's how I'm formatting the date.
    > >
    > > $file = "dms_nspwr_" . `date +%b_%d_%y_%H%M.txt`;

    >
    > There's little need to shell this out to an external command. Check
    > out the strftime() function in
    > perldoc POSIX
    >
    > > Here's my FTP code:
    > >
    > > $ftp = Net::FTP ->new("123.456.789.10", Debug => 0)

    >
    > You appear to not be using strict. Please make sure all your scripts
    > that you ask for help with here start with
    > use strict;
    > use warnings;
    >
    > Also, as you seem to be having problems with the FTP class, perhaps it
    > would be a good idea to enable debugging, to ensure that your program
    > is doing what you think it's doing? Try replacing the end of that
    > constructor with:
    > Debug => 1)
    >
    > > or die "Cannot connect to some.host.name: $@";
    > >
    > > $ftp->login("username",'password')
    > > or die "Cannot login ", $ftp->message;
    > >
    > > #$ftp->cwd("/pub")
    > > # or die "Cannot change working directory ", $ftp->message;
    > >
    > >
    > > $ftp->put("$file")

    >
    > This is a useless use of double quotes, and is a bad habbit to get
    > into. Please read:
    > perldoc -q quoting
    >
    > > or die "put failed $file", $ftp->message;
    > >
    > > $ftp->quit;

    >
    > Have you verified that the file exists in the current local directory?
    > Have you verified that the file has content in the current local
    > directory?
    >
    > Try adding these two lines before the $ftp->put():
    >
    > -e $file or die "File $file does not exist!\n"
    > -s _ or die "File $file is empty!\n";
    >
    > and let us know the results of those lines plus the Debugging output...
    >
    > Paul Lalli
    Nex_s, Aug 2, 2005
    #3
  4. Nex_s

    Paul Lalli Guest

    > Paul Lalli wrote:
    > > Nex_s wrote:
    > > > I'm trying to format a date and store it in a variable for a file
    > > > name. The file gets created but for some reason my FTP process sends
    > > > an empty file with the same name.


    <snip>

    > > >
    > > > $ftp->put("$file")
    > > > or die "put failed $file", $ftp->message;
    > > >
    > > > $ftp->quit;

    > >
    > > Have you verified that the file exists in the current local directory?
    > > Have you verified that the file has content in the current local
    > > directory?
    > >
    > > Try adding these two lines before the $ftp->put():
    > >
    > > -e $file or die "File $file does not exist!\n"
    > > -s _ or die "File $file is empty!\n";
    > >
    > > and let us know the results of those lines plus the Debugging output...
    > >

    > Hi there,
    >
    > Thank you kindly for your response.


    You're welcome. I would ask that you please show your gratitude by
    following proper quoting guidelines - quote a *relevant* amount of text
    *above* your new message. Thank you.

    > I ran the code again the ftp
    > process ran flawlessly until the put statement when the error came
    > back:
    >
    > File dms_nspwr_2005_08_02_1404.txt does not exist!
    >
    > The file is in the same directory as the script and contains data.


    I'm afraid you're mistaken. That error message is very specifically
    telling you that the file in question (is that the name you were
    expecting for your file?) does not exist. Is your script doing any
    chdir()'s along the way which may be changing the working directory?

    What OS are you using, and how are you executing the script?

    > I
    > even tried putting in sleep 10 in case it was necessary to give the
    > server time to save the file but to no avail.


    You're misunderstanding the error message. The file never made it to
    the server, because the put() was never executed. The error occurred
    at the -e line I asked you to put in, telling you that the file does
    not exist in your local directory.

    > P.S. I tired explictly defining all my variables but ran into one
    > problem with one used for my e-mail routine $mail. The line in
    > question is:
    > $mail{From} = 'Data Upload <>';
    > Here's the error it gave me.
    >
    > Global symbol "%mail" requires explicit package name at line 147.


    That line is not using a variable $mail. It is using the hash variable
    %mail. If you tried to declare that variable with
    my $mail;
    you should change that to
    my %mail;

    $mail and %mail are two independent variables that have nothing to do
    with one another. For more information, please see:
    perldoc perldata

    Paul Lalli
    Paul Lalli, Aug 2, 2005
    #4
  5. In article <>,
    Nex_s <> wrote:
    >
    > Hi All,
    >
    > I'm trying to format a date and store it in a variable for a file
    >name. The file gets created but for some reason my FTP process sends
    >an empty file with the same name.
    >
    > Here's how I'm formatting the date.
    >
    >$file = "dms_nspwr_" . `date +%b_%d_%y_%H%M.txt`;


    As has been stated elsewhere, you're probably better off doing the
    date formatting within Perl.

    But if you are calling an external command to do the work, there
    is probably a trailing newline.

    perldoc -f chomp

    It's often a good idea to make sure that your variables contain
    *exactly* what you think they should. I usually do something like
    print "--$file--\n";
    to check for extra characters.

    If you're viewing the output through a browser, remember that browsers
    tend to reformat the output and make whitespace (particularly newlines)
    in the input harder, if not impossible, to see. Using something like
    $file =~ s/\n/[NL]/;
    before the print will make possibly-hidden characters more visible.

    Gary Ansok
    --
    The recipe says "toss lightly," but I suppose that depends on
    how much you eat and how bad the cramps get. - J. Lileks
    Gary E. Ansok, Aug 2, 2005
    #5
  6. Nex_s

    Nex_s Guest


    > > > Nex_s wrote:
    > > > > I'm trying to format a date and store it in a variable for a file
    > > > > name. The file gets created but for some reason my FTP process sends
    > > > > an empty file with the same name.

    >



    Hi Paul,

    I'm not changing directories, at least not explicitly. The name
    "dms_nspwr_2005_08_02_1404.txt" is the name of the file I expected. I
    changed it from what I had and decided to use POSIX. I put the ftp
    code into another file by itself and when I ran it, it found the file
    and ftp'd it no problem. So I'm still at a loss as to why there's a
    problem with my main script.

    Chris
    Nex_s, Aug 2, 2005
    #6
  7. Nex_s

    Paul Lalli Guest

    Nex_s wrote:
    > I'm not changing directories, at least not explicitly. The name
    > "dms_nspwr_2005_08_02_1404.txt" is the name of the file I expected. I
    > changed it from what I had and decided to use POSIX. I put the ftp
    > code into another file by itself and when I ran it, it found the file
    > and ftp'd it no problem. So I'm still at a loss as to why there's a
    > problem with my main script.


    Chris,

    At this point, the best advice I can offer is to pare your code down
    to the shortest complete script that still exhibits the problem, and
    then to paste that code here. If you are indeed using
    POSIX::strftime(), then Gary's hint elsewhere in this thread is no
    longer relevant (which is unfortunate, because I was about to remark on
    that as well).

    Paul Lalli
    Paul Lalli, Aug 2, 2005
    #7
  8. Nex_s

    Nex_s Guest


    > Chris,
    >
    > At this point, the best advice I can offer is to pare your code down
    > to the shortest complete script that still exhibits the problem, and
    > then to paste that code here. If you are indeed using
    > POSIX::strftime(), then Gary's hint elsewhere in this thread is no
    > longer relevant (which is unfortunate, because I was about to remark on
    > that as well).
    >
    > Paul Lalli



    Hi Paul,

    I finally figured out after all that head banging what the problem
    was. I forgot to close the file that I was sending!!! ARGH!

    Anyhow, thank you for your patients and help. My hope is the
    information this post provided is not in vain.

    Chris
    Nex_s, Aug 2, 2005
    #8
  9. Nex_s

    Paul Lalli Guest

    Nex_s wrote:
    > Hi Paul,
    >
    > I finally figured out after all that head banging what the problem
    > was. I forgot to close the file that I was sending!!! ARGH!
    >
    > Anyhow, thank you for your patients and help. My hope is the
    > information this post provided is not in vain.


    Chris,

    I'm glad you got your problem resolved. For the future, I would just
    like to point out that this is precisely the reason the Posting
    Guildelines request that you post a short but *complete* script which
    exhibits your problem. If that had been done in the first place, it's
    likely we would have been able to diagnose the problem without all the
    guesswork involved in the snippet of code you did show.

    Regards,
    Paul Lalli
    Paul Lalli, Aug 3, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,185
    Michael Borgwardt
    May 30, 2004
  2. KK
    Replies:
    2
    Views:
    498
    Big Brian
    Oct 14, 2003
  3. Bob
    Replies:
    2
    Views:
    302
  4. Jack-2
    Replies:
    3
    Views:
    248
    Jack-2
    Dec 24, 2003
  5. D. Buck
    Replies:
    2
    Views:
    449
    D. Buck
    Jun 29, 2004
Loading...

Share This Page