Ruby implementation Batch add prefix code sharing _ruby Topic

Source: Internet
Author: User
Tags parent directory

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"

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.