If it were just for Win32, I'd use Tye's Win32API::File::MoveFile() to bypass Perl's POSIXisation of rename and have the attempts fail rather silently overwrite an existing file:
use Win32API::File qw[ MoveFile ]; my $i=0; $i++ until MoveFile( $source, compose_name( $base, $ext, $i ) );
Or better yet, Jenda's Win32::FileOp::MoveFileEx() with the FOF_RENAMEONCOLLISION and let the OS take care of finding an available name.
Either of those should work on any Win32 filesystem, though I'm not sure about things like Samba. Combined with ambrus' suggestion for true posix systems, it might get you 90% there for portability, though it would require platform specific code.
Any solution that polls for existance before creation, or relies on cooperative locking leaves a gap. If you are forced to that route, using random numbers rather than sequential would reduce the occurance of collisions, and perhaps reduce the possibility of race condition errors.
In reply to Re: How to move/copy a file without overwriting an existing file
by BrowserUk
in thread How to move/copy a file without overwriting an existing file
by bart
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |