Ruby implements batch code sharing by adding prefixes to files, and ruby Prefix code sharing
From the perspective of the designer, we get a large number of images, and the results are all file names such as 1.png and 2.png. We also need to convert these files into readable file names and do not want to modify them one by one, so we wrote a simple script, you can add prefixes to multiple files in batches. You can modify the prefixes later.
Code
Copy codeThe Code is 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
Usage
1. ruby add_prefix_files.rb dest_folder prefix pattern
2. dest_folder: the Basic directory required for the operation. It is not always the parent directory.
3. The prefix name must end _.
4. The pattern is optional. If it is not set to a direct sub-file (including directories) of dest_folder, the pattern provided by the application matches
Example
Add the test _ prefix to all files in the current directory.
Copy codeThe Code is as follows:
~ /Rubydir/tools/add_prefix_files.rb./test _
Add the test _ prefix to res/drawable-hdpi/All png files in the current directory.
Copy codeThe Code is as follows:
~ /Rubydir/tools/add_prefix_files.rb./test _ "res/drawable-hdpi/*. png"