When you do a series of operations in a directory, you may encounter a lot of trouble to create many files or modify files. So, create a new folder, and then create a new file or modify it under this folder. Suppose your code creates a new folder under a directory, named Tmpfolder, and then takes a series of actions under the folder. However, the directory may appear before the tmpfolder, such a mess, will be problematic. How to solve this, there are the following 2:
(1) If you encounter a folder name is Tmpfolder, do not have this folder name, directly with TmpFolder2, not just 2 point it;
Tmpfolder and TmpFolder2 all exist, just use the TmpFolder3 directly, so that has been gracefully iterative down until the old. (as Microsoft handles files of the same name)
(2) Of course, if you are hard to use Tmpfolder, is willful, this is not a way. If there is tmpfolder in the directory, name the folder Tmpfoler a file name that is not currently duplicated, and then create a new folder Tmpfolder, after a series of operations, the folder name is changed. So, here's the question, how do you write this file name that won't be duplicated at the moment? In Perl, $$ refers to the process ID of the Perl interpreter, which is currently unique. So, it can be written as follows,
My $tmpfile = ' tmpfolder '; my $chgfile = ' tmpfolder '. $$;if (-D $tmpfile) { rename ($tmpfile, $chgfile); # If you have a duplicate folder, rename the mkdir $tmpfile; # then create a new temporary folder print " Helo "; # rmdir $tmpfile; # Delete temporary folder rename ($chgfile, $tmpfile); #}else{ mkdir $tmpfile; # If there's no name, there's no problem. print "Yay"; RmDir $tmpfile;}
Perl Create Temp folder when you encounter a file of the same name