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