Swift 实现文件名称批量修改

        var fileManager = NSFileManager()
// 取得文件夹的路径,注意:末尾加/与不加同样,推荐末尾加/ // Define folderPath var filePath = "/Users/Star/desktop/PathName/"

// 获得文件夹下文件名称集合。注:此处获得文件名称,而不是全部路径 // Get all filenames array in the folder, warning: only filename and not full path var oldFileNamesArray = fileManager.contentsOfDirectoryAtPath(filePath, error: nil) as [String] for oldFileName in oldFileNamesArray{
// 取得旧名称的全部路径 // Append to file full name var oldFilePathName = filePath + oldFileName
// 取得需要修改的字的所在范围 // Get range of replace string var rangeString = oldFileName.rangeOfString("OldName", options: NSStringCompareOptions.LiteralSearch, range: nil, locale: nil); if rangeString == nil { continue }
// Swift中的String 是引用类型 // 不能直接修改 for in 遍历的元素 // Define a new string Becauce we cannot direct replacing the for in circulation element var newFileName = oldFileName
// 替换掉Old字符串 // replaced to NewName newFileName.replaceRange(rangeString!, with: "NewName") // 取得新名词的全部路径 // Get the new full path var newFilePathName = filePath + newFileName // 执行文件移动函数,此函数能实现文件名称修改 // Execute the function "moveItemAtPath" with moving the file // The function can replace "rename" // return execution result if fileManager.moveItemAtPath(oldFilePathName, toPath: newFilePathName, error: nil){ println("成功 successful") } else{ println("失败 failed") } }

 

转载于:https://www.cnblogs.com/YHStar/p/4306784.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值