Perl Create Temp folder when you encounter a file of the same name

Source: Internet
Author: User
Tags perl interpreter

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.