[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Unique File Name



>Kevin Walker wrote:
>> Here's how I do it:
>>
>>   $name = ..... # whatever
>>   if (-e $name) {
>>     for (my $i = 1; -e ($name = $name."_$i"); $i++) {};   # get unique name
>>   }
>
>(Ok - here's my stupidity showing up again! ;-) )
>
>Won't that make names like this:
>
>	myFile
>	myFile_1
>	myFile_1_2
>	myFile_1_2_3

Oops.  I edited/simplified working code but didn't test the results.  I
should have said something like

   $name = ..... # whatever
   if (-e $name) {
     for (my $i = 1; -e ($name2 = $name."_$i"); $i++) {};   # get unique name
     $name = $name2;
   }


>They are unique though.  :-)
>
>Maybe something like this?
>
>	$i = 0;
>	$theFile = "myFile.html";
>	while( -e $theFile ){
>		$i++;
>		$theFile = s/_.*$/_$i\.html/;
>		}
>
>This should give you:
>
>	myFile.html
>	myFile_1.html
>	myFile_2.html
>	etc....




***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch