A script that unifies the filename format of a Comiket object

Source: Internet
Author: User
Tags foreach string format

Again to the Comiket tide, grabbed down the name of the file is always the format is not uniform, sorted by file name is not convenient to view. So it's time to go Ruby again.

(Every time I go to the batch rename file, my first reaction is to open IRB ...) )

The script is available in several categories of directories, so take a note.

Scripting: Adjusts the space in the first part of the file name of a similar "(CXX) (Item_type) [Circle_name] Item_name" to: the prefix label does not include spaces, leaving a space between the whole label and the file name that follows. Normal files and directories are renamed targets. A file that does not conform to this one with two sets of parentheses and a set of brackets with a prefix label is not in the rename target range. I usually use the other foot to put the order in this order and then remove the space together.

Restrictions: If a filename appears with characters that cannot be displayed under the current system locale, the renaming of that file will fail, and the rename failure will not affect subsequent loops, and the failed file name will be displayed to stderr. Anyway, there are special characters of the file/directory will not be a lot, temporarily manual modification forget OTL

Ruby 1.8 has a bit of a problem coding itself, and Ruby 1.9 's string is Unicode, but it doesn't seem to work when it does. Strange, maybe I didn't write right or something. Try again later.

Ruby Code

#!/usr/bin/env Ruby

def reformat_comiket_folder (dir= '. ')
  Dir.entries (dir). Each do |p|
    Begin
      If P =~/^\ ([[^)]+) \) \s*\ ([[^)]+] \) \s*\[([^\]]+) \]\s* (. +) $/
        file.rename p, (#{$1}) (#{$2}) [#{$3}] #{$4 "End
    rescue Systemcallerror
      $stderr. Puts ' IO failed: ' + $!
    End
  End

If __file__ = $
  reformat_comiket_folder Argv[0] | | '.'
End

In fact, the script in the heart a little itchy ... Then I remembered the renaming tool I had written before. It's a good time to finish writing it. T

Of course, there is no coding problem with C # to write this program. Using the same logic as above, rewrite it as:

C # code

Using System;
Using System.IO;
Using System.Linq;

Using System.Text.RegularExpressions;
        Sealed class Reformatcomiketfiles {static bool Trygetrenamename (Regex pattern, string input, string format,//format items in this param correspond to match. Groups[1..$] Out string result, {var match = pattern.
        Match (input); if (match. Success) {var str = string. Format (format, match. Groups. Cast<group> (). Skip (1). Select (g => g.value).
            ToArray ());
            result = STR; return str!= src;
            Using a local variable this saves an indirect read} else {result = input;
        return false;
 } static void Main (string[] args) {DirectoryInfo root;       if (0 < args.
        Length) {root = new DirectoryInfo (args[0]);
        else {root = new DirectoryInfo (environment.currentdirectory);

        var pattern = new Regex (@ "^\ ([^)]+) \) \s*\ ([^)]+) \) \s*\[([^\]]+) \]\s* (. +) $"); foreach (var dir in root.)
            GetDirectories ()) {string dest = null; if (Trygetrenamename (Pattern, dir). Name, "({0}) ({1}) [{2}] {3}", out dest)) {Console.WriteLine (dest
                ); Dir. MoveTo (Path.Combine) (dir.
            Parent.fullname, dest)); } foreach (var file in root).
            GetFiles ()) {string dest = null; if (trygetrenamename pattern, file. Name, "({0}) ({1}) [{2}] {3}", out dest)) {Console.WriteLine (des
                T); File. MoveTo (Path.Combine (file.
            DirectoryName, dest)); }
        }
    }
}

If you try it, it seems to be all right. The operating environment is the. NET Framework 3.5 SP1.

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.