Swift implements file name batch modification

Source: Internet
Author: User

        varFileManager = Nsfilemanager()
//get the path to the folder, note: At the end plus/without the same, the recommended end Plus///Define FolderPath varFilePath ="/users/star/desktop/pathname/"

//gets the folder under the file name collection. Note: Get the file name here instead of the full path//Get all filenames array in the folder, warning:only filename and not full path varOldfilenamesarray = Filemanager.contentsofdirectoryatpath (FilePath, Error:Nil) as[String] forOldfilenameincholdfilenamesarray{
//get the full path of the old name//Append to file full name varOldfilepathname = FilePath +Oldfilename
//get the range of words that need to be modified//Get range of replace string varrangestring = oldfilename.rangeofstring ("oldname", Options: nsstringcompareoptions.literalsearch, Range:nil, Locale:nil); ifRangestring = =Nil {Continue }
//string in Swift is a reference type//You cannot directly modify the elements that are traversed by//Define A new string becauce we cannot direct replacing the for in circulation element varNewFileName =Oldfilename
//Replace old string//replaced to NewNameNewfilename.replacerange (rangestring!, with:"NewName") //get the full path of the term//Get The new full pathvar newfilepathname = FilePath +NewFileName//Execute file Move function, this function can implement file name modification//Execute the function "Moveitematpath" with moving the file//the function can replace "rename"//return execution Result ifFilemanager.moveitematpath (oldfilepathname, Topath:newfilepathname, error: nil) {println ("Success Successful") } Else{println ("Failure failed") } }

Swift implements file name batch modification

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.