Export the file, and then add (number)--> xxxx (2) If the filename is repeated. xxx

Source: Internet
Author: User

Generally in Windows if the same file is copied under the same path, then a "-copy" file name appears after the filename, which now requires a filename in the format "(2)", especially when the document is exported, as follows:

/** * If a path already exists with a duplicate file name to be saved, add (1) after the new file ....  * @param paths * @param filename * @param num * @return/public static string GetFileName (string
        paths, String filename, int num) {file file = null;
        string[] Filenamearr = Filename.split ("\;");
        int length = Filenamearr.length;
        String filenamestr= ""; The purpose here is to avoid the file name containing the "." When the filename repeats, an error occurs and we have to make sure that it is at the last "."
        Preceded by "(2)" for (int i=0;i<=length-2;i++) {filenamestr = filenamestr+ filenamearr[i]+ ".";} if (num > 0) {file = new file (paths + "\" + filenamestr.substring (0, Filenamestr.length ()-1) + "( "+ string.valueof" (num = 1?)
        2:num) + ")." + filenamearr[filenamearr.length-1]);
        else {file = new file (paths + "\" + filename);
        } if (File.exists ()) {return GetFileName (paths, filename, ++num); else {if (num > 0) {return paths + "\" + filenamestr.substring (0, Filenamestr.length ()-1) + "(" + string.valueof (num = 1?)
            2:num) + ")." + filenamearr[filenamearr.length-1];
            else {return paths + "\" + filename; }
        }
    }

The test results are as follows:

XXXX (2). xxx ">

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.