Recently I got a lot of pictures from the designer, the results are 1.png,2.png such file name, they also need to make these files into a readable file name, do not want one to be modified, so wrote a simple script to achieve a lot of multiple files to add prefix operation, later modified a bit, share.
Code
Copy Code code as follows:
#!/usr/bin/env Ruby
# Encoding:utf-8
Srcdir= Argv[0]
prefix= Argv[1]
Pattern = ' * '
Pattern = argv[2] if argv.size = = 3
Dir[srcdir + '/' + pattern].each{|child|
ChildName = File.basename (Child)
Destchildname = prefix + childname
Destchild = Child.gsub (ChildName, Destchildname)
System ' MV%s '%s '%[child, Destchild]
}
How to use
The method used
1.ruby add_prefix_files.rb dest_folder prefix pattern
2.dest_folder the underlying directory for the required operation, and not always directly to the parent directory
3.prefix must prefix name suggested end with _ End
4.pattern Optional, if not filled in as Dest_folder Direct sub file (including directories), otherwise the application provided pattern matching
Using the sample
Adds a test_ prefix to all files in the current directory.
Copy Code code as follows:
~/rubydir/tools/add_prefix_files.rb./test_
res/drawable-hdpi/all PNG files to the current directory, adding test_ prefixes
Copy Code code as follows:
~/rubydir/tools/add_prefix_files.rb./test_ "Res/drawable-hdpi/*.png"