Ruby implements batch code sharing by adding prefixes to files, and ruby Prefix code sharing

Source: Internet
Author: User

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"

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.