Closed Thread
Results 1 to 9 of 9

Thread: control if exists(file) and copy

  1. #1
    dimitry is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Switzerland
    Posts
    40
    Rep Power
    0

    Question control if exists(file) and copy

    Hi again,
    You help me a lot, I hope help anyone when I can...

    So, my problem;

    In perl again; I would like to verify if a file exists, if it's the case I wanna change his name for exemple if 78oov9.def exists copy and change his name in 78oov9.old... I've made a copy function and after a control iteration:
    Code:
    # $file is the path and the name of my file with the extention added
    # $oldfile is the same, just with an extention .old
    # function for copying
    sub copyf
    {
        my ($from, $to, $buf) = @_;
        if (open (IN, $from))
        {
            if (open(OUT, ">$to"))
            {
                while (read IN, $buf, 4096)
                {
                    print OUT $buf;
                }
                close IN;
                close OUT;
                return 1;
            }
            close IN;
        }
        return 0;
    }
    
    # and the control:
    
    if (-e $file)
    {
        copyf("$file", "$oldfile") or die ("cannor copy\b");
    }
    Anyone has an idea why it doesn't work?
    Thanks from advance!
    Last edited by dimitry; 06-05-2009 at 01:44 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    dimitry is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Switzerland
    Posts
    40
    Rep Power
    0

    Smile Re: control if exists(file) and copy

    So, I've change of idea; I'll not use copy function, just unlink and rename, it's more easy. My code work correctly... just in case; here you are my code:
    Code:
    # if $file already exists
    if (-e $file)
    {
        #if $oldfile already exists
         if (-e $oldfile)
        {
            # "delete" it
            unlink '$oldfile';
            # and rename $file in $oldfile
            rename $file, $oldfile;
        }
        else
        {
            rename $file, $oldfile;
        }
    }
    Bye!
    Lord Darkdriver by Dimitry

  4. #3
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: control if exists(file) and copy

    The code you posted will not work. You have improperly quoted the scalar argument to the unlink function:

    unlink '$oldfile';

    should be:

    unlink $oldfile;

  5. #4
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: control if exists(file) and copy

    You should also be checking the succes or failure of all those system operations using die or warn or something to make sure they are doing what you want instead of blindly running code that might fail and seem to not work as you expect.

  6. #5
    dimitry is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Switzerland
    Posts
    40
    Rep Power
    0

    Smile Re: control if exists(file) and copy

    hello,
    it's funny, it works perfectly... but I'll try on monday what you said about the quote and the warn.
    I gonna give you news. Thanks for your advice.
    Lord Darkdriver by Dimitry

  7. #6
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: control if exists(file) and copy

    probably the rename() function is making it look like unlink worked. Try this:

    unlink '$oldfile' or die "Can't delete $oldfile: $!";

  8. #7
    dimitry is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Switzerland
    Posts
    40
    Rep Power
    0

    Smile Re: control if exists(file) and copy

    Hi,
    that's work too. I was looking for error in the logs but there is any about this java script...

    Thanks again for your help!
    Lord Darkdriver by Dimitry

  9. #8
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    Re: control if exists(file) and copy

    Yea, whatever you say Dimitry.....

  10. #9
    dimitry is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Switzerland
    Posts
    40
    Rep Power
    0

    Post Re: control if exists(file) and copy

    So, I just said that it wasn't any error on the "log file" ....
    What exactly you don't understand?
    Lord Darkdriver by Dimitry

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to find a control that was created through a custom control (dll file)
    By ManyTimes in forum ASP, ASP.NET and Coldfusion
    Replies: 0
    Last Post: 05-31-2011, 04:08 PM
  2. Linux/Bash: Check if a file exists
    By Tor in forum Bash / Shell Scripting
    Replies: 4
    Last Post: 07-10-2009, 11:28 AM
  3. Check if a file exists?
    By Lop in forum Pascal and Delphi
    Replies: 3
    Last Post: 01-06-2007, 10:54 PM
  4. Check if a file exists
    By Lop in forum C and C++
    Replies: 4
    Last Post: 08-07-2006, 08:03 PM
  5. Check if a File Exists
    By NeedHelp in forum C# Programming
    Replies: 4
    Last Post: 08-07-2006, 03:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts