Wrote a simple program to change the file name in a batch with objective-c

Source: Internet
Author: User



Preface: Because I want to high imitation an app, extract asserts from the app all the picture files, filenames with ~iphone This interference name, in order to remove this ~iphone this string, so I wrote a simple answer to the batch change all the file name of the program.



Programmers should be able to use the program to help themselves lazy, because many of the methods used are not very familiar, but also spent some time to familiar with some of the methods of Nsfilemanager, so write down this note to make a note, save the next time you need to forget the method.






My basic needs, in the wordy next ha:



There are hundreds of picture files in a file directory with ~iphone file names. Write a program that removes all of this string ~iphone.






Code:



This code takes advantage of the regular expression classes that are encapsulated by third parties, and then further realizes my own needs.



This is a pretty good class of regular expressions on GitHub: https://github.com/bendytree/Objective-C-RegEx-Categories



Then there is:


1 //
 2 // main.m
 3 // ChangeFileName
 4 //
 5 // Created by HEYANG on 16/4/3.
 6 // Copyright © 2016 HEYANG. All rights reserved.
 7 //
 8 // cnBlog: http://www.cnblogs.com/goodboy-heyang/
 9 // github: https://github.com/HeYang123456789
10 //
11
12 //-(BOOL) moveItemAtPath: from toPath: to error: err rename or move a file (to cannot exist
13
14 // Enter the file directory here
15 #define FileDirectory @ "/ Users / HeYang / Desktop / hello"
16
17
18 #import <Foundation / Foundation.h>
19 #import "RegExCategories.h"
20
twenty one 
22 // Replace the string with the file name and remove the word ~ iphone from the file name
23 NSString * changeString (NSString * string) {
24 // Use regular expression directly, replace
25 NSString * result = [RX (@ "~ iphone") replace: string
26 with: @ ""];
27 return result;
28}
29
30 // Get the file name and remove ~ iPhone
31 NSString * getFileNameFromDirectory (NSString * directory)
32 {
33 NSFileManager * manager = [NSFileManager defaultManager];
34 NSArray * dirArray = [manager contentsOfDirectoryAtPath: directory error: nil];
35 for (NSString * str in dirArray) {
36 // original file directory
37 NSString * fromFileName = [FileDirectory stringByAppendingPathComponent: str];
38 // changed file name
39 NSString * changedStr = changeString (str);
40 // file directory after change
41 NSString * toFileName = [FileDirectory stringByAppendingPathComponent: changedStr];
42 // Replace, actually rename
43 [manager moveItemAtPath: fromFileName toPath: toFileName error: nil];
44}
45 return nil;
46}
47
48 int main (int argc, const char * argv []) {
49 @autoreleasepool {
50
51 getFileNameFromDirectory (FileDirectory);
52
53}
54 return 0;
55} 


Project source Backup to Baidu Cloud Link: Http://pan.baidu.com/s/1dFjUV5J Password: e5q9






Wrote a simple program to change the file name in a batch with objective-c


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.