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

i want to open file and insert it into dir with new filename

my $NFILE = "09911"; #NEW FILE NAME my $FILE = '02190.JPG'; #FILE TO OPEN my $openfile = open(DATA, ">$FILE"); #OPEN FILE my $newfile = rename($openfile, $NFILE); #RENAME FILE FROM 02190.JPG T +O 09911.JPG my $writefile = open(DATA,">>", "img/$newfile"); #INSERT THE FILE IN I +MG DIR WITH NEW NAME 09911.JPG close DATA;

Replies are listed 'Best First'.
Re: file handing
by johngg (Canon) on Sep 22, 2018 at 14:56 UTC
    my $openfile = open(DATA, ">$FILE"); #OPEN FILE

    Don't try running that code as you are opening the JPEG file for writing and will therefore clobber the contents, probably not what you want. Instead of trying to roll your own file copying/moving code you might be safer following poj's original advice. If you are doing this as a learning exercise then make good back-ups, read the tutorial poj mentions (and the reference doc) and practice on copies of your files to avoid disappointment.

    Cheers,

    JohnGG

Re: file handing
by poj (Abbot) on Sep 22, 2018 at 13:37 UTC

      now the problem i have. it copies when no rename but when i try to put rename. the file gets renamed but cant be copied after rename

      use strict; use warnings; use File::Copy; my $NFILE = "09911"; my $FILE = '02190.JPG'; my $filetobecopied = $FILE; my $foldertocopyto = "img"; my $rename = rename($FILE, "$NFILE.JPG"); copy($rename, $foldertocopyto) or die "File cannot be copied.";

        see rename

        rename OLDNAME,NEWNAME
        
        Changes the name of a file; an existing file NEWNAME will be clobbered. Returns true for success, false otherwise.
        poj

      i wanted to use open() function

Re: file handing
by lyklev (Pilgrim) on Sep 22, 2018 at 21:29 UTC

    Not trying to be mean, but you are quite clueless about opening files, how the commands work, and how to use Perl effectively.

    I can strongly recommend “Learning Perl”. It taught me a lot, was fun to work with, and made me a better programmer.

Re: file handing
by bliako (Abbot) on Sep 23, 2018 at 23:11 UTC

    Hi bigup401,

    In order to read some data from a file to a perl variable one may use open() and then read from the FILEHANDLE provided by open(). When you are done with it, you close that file, again using the FILEHANDLE which open() gave you when you called it.

    ***BUT*** If you want to rename a file, Perl can do this for you (just as the operating system does it for you (using, for example, the OS commands mv - for move - in Unix from the command line/prompt/terminal by giving it plain old FILENAMES). So, in this case you need to provide FILENAMES (and NOT filehandles).

    Which means, you do not need to open() the file first to obtain a filehandle. What's more, you should not open that file in WRITING MODE (i.e the ">$FILE", note the > - it means OVERWRITE what's already in that file, and zero its contents to start with. Your file's previous contents are gone!)

    Bottomline is: RENAME in Perl, operates with FILENAMES. NOT filehandles. So, do not open() a file first if you want to rename it. Just use one of the suggestions fellow Monks have already made. BUT NOTE: rename/delete/copy file operations using Perl require FILENAMEs and NOT filehandles. (of course there is another way to do it, but let's concentrate on this problem for now)

    Now, there is a second issue here which holds down your learning curve from shooting upwards.

    It is that you are confused as what RENAMES a file can do: YES you can rename a file so that it ends up in a different dir.

    You do not need to first change its name in same dir and then move it to a different dir with a combination of open() and rename() commands. which eventually end up truncating (this is what open(DATA, ">hello"); does to the file "hello" if it exists: truncate = zero its contents.

    Finally, reading your follow-up answers, I suspect that you are trying to get some data from client via your CGI script and save it to client-specified filename but you want it to be in another dir. In any event you want to specify a dirname along with a filename for the uploaded data to be saved do locally. Just go for it. open() dirname+filename for writing and save upload contents to it. This is valid, provided that dirname already exists and your script has writing permission. No fussing with rename() at all in this case.

Re: file handing
by Anonymous Monk on Sep 23, 2018 at 08:11 UTC
    #!/usr/bin/perl use strict; use warnings; use autodie; my $OLDFILE = '02190.JPG'; my $NEWFILE = 'img/09911.JPG'; open my $fh, '<', $OLDFILE; binmode $fh; my $data = join '', <$fh>; close $fh; open $fh, '>', $NEWFILE; binmode $fh; print $fh $data; close $fh; print "Copied $OLDFILE to $NEWFILE\n";

      thanks but i know file handling. the only problem i have am trying to play around it for more knowledge

      the problem. is now getting file with zero byte. the debt was someone telling me u cant rename, write, read and check image extension with built in funcation open() that i have to use module like image magick bra bra bra. but i know i can archive this without any single module using only built in function open(). and so far here is my code works well the problem i have now is the file uploaded in zero byte. that means cant open

      my $photo = $CGI->param("pic") my $copydir = 'img'; my ($file_extension) = $photo =~ /([^.]+)$/; #get photo extension my $newf = "011190.$file_extension"; #rename photo to its exte +nsion to keep the original resolutions of the file rename($photo, $newf); $upload = $CGI->upload("pic"); open ( UPLOADFILE, ">$copydir/$newf"); close UPLOADFILE;
        "... i know file handling...trying to play around it for more knowledge..."

        Sure. No doubt. But now become more modern and try something like use Path::Tiny; path("nose.jpg")->copy("cuke.jpg");. Not tested - guaranteed.

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        You forgot to print $upload to UPLOADFILE:
        $upload = $CGI->upload("pic"); open UPLOADFILE, ">", "$copydir/$newf"; binmode UPLOADFILE; print UPLOADFILE $upload; # YOU FORGOT THIS! :-) close UPLOADFILE;
        Also try these lines at the top of your script to more easily spot any errors:
        use autodie; use CGI::Carp qw(warningsToBrowser fatalsToBrowser);