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

Hi I'm trying to sort some files and i'm haveing a spot of bother with part of the script.

The first time it is run a new folder is created but gives an error 'F:\temp\Test\Test14.jpg' and 'F:\temp\Test\Test14.jpg' are identical (not copied) at E:\Perl\NewDir.pl line 30

The second time creates another new folder and copies the file

Note $Old = $New for testing only hance the hard coding the actual files are in multiple folders the new folder will be based on the file name

#!/usr/bin/perl use File::Copy; use File::Path qw(make_path remove_tree); my $path = "F:\\temp\\Test" ; my $New = "F:\\temp\\Test\\Test14.jpg"; my $Old =$New; my $nFile = "Test14.jpg"; my $I=1; if (-e $New) { my $PATH = $path . $I; until (! -d $PATH) { $I++; $PATH = $path . $I; print "I'm in a loop $PATH\n"; $New = qq($PATH\\$nFile); } $path = $PATH; print "path is $path $Old $New\n" ; make_path($path); if (-d $path) { copy("$Old", "$New"); } else { print "$path not existl\n"; } }

Replies are listed 'Best First'.
Re: New Folders
by ikegami (Patriarch) on Dec 15, 2011 at 11:01 UTC

    Of course

    my $New = "F:\\temp\\Test\\Test14.jpg"; my $Old = $New; copy("$Old", "$New");

    gives an error; one can't copy a file over itself.

    You didn't ask a question. You didn't say what your code should do. I don't know how you want us to proceed.

      it sounded OK in my head

      in the real world I have many groups of files spread out over multiple folders each group has the same file name within it e.g. Test01.jpg .. Test100.jpg so the first group will go into folder1 the second in folder2 etc. First test if Test1\Test01.jpg exists

      If it does then create a new folder by adding a 1 to the end of the folder name and test again until the folder N does not exist then copy the old file into the new folder thus sorting into groups

      What I need to know is why when I first run the script the folder Test1 is created but it still tries to copy the file over itself. Yet the second time I run it Test1 exist but is empty folder Test2 is created and the file is copied

      Hope that make sence

Re: New Folders
by jethro (Monsignor) on Dec 15, 2011 at 11:10 UTC

    If "$path.1" doesn't exist, $New is never changed as the changing line is inside the until {} loop

    By the way, using uppercase, lowercase and mixed variable names together like $path and $PATH and $New is a sure way to put additional bugs into your code. Or do you have a special system that avoids confusion?

      Thanx Moving $New = qq($PATH\\$nFile); out side the loop worked doesn’t explain why it worked on the second run and not the first.

      No I don't have a special code for naming just that’s the way I’ve all ways done it. I agree $PATH and $path is confusing. May be I should of used $Tpath inside the loop

        ...doesn’t explain why it worked on the second run...

        Oh, it does. On the second run the directory $path.1 had already been created (by the first run) and so the loop beginning with "until (! -d $PATH)" did run at least once. When that happens the line "$New=..:" gets executed. Result: $New!=$Old