-
-
Save cjheath/6983880 to your computer and use it in GitHub Desktop.
mved: Rename all the files named foo<anything> to bar<anything>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/ruby | |
| # | |
| # mved: Rename files from foo*bar to some*thing. | |
| # The "from" and "to" patterns must have one * or one ? each (escape it fron the shell) | |
| # | |
| # Author: Clifford Heath. | |
| # (c) Copyright 2013. | |
| # Free for any purpose, but don't claim you wrote it. | |
| require "getoptlong" | |
| def Usage(code) | |
| print "Usage: mved [ options ] pattern1 pattern2\n" + | |
| "\t-n:\trename no files, just say what would be done\n" + | |
| "\t-v:\tsay what is being done\n" + | |
| "\t-f:\toverwrite existing files\n" + | |
| "both patterns must contain one wildcard character (* or ?)\n" | |
| exit(code) | |
| end | |
| $NOCHANGE = false | |
| $CAPITALISE_WORDS = false | |
| $VERBOSE = false | |
| $FORCE = false | |
| optionparser = GetoptLong.new | |
| optionparser.set_options( | |
| ['--nochange', '-n', GetoptLong::NO_ARGUMENT], | |
| ['--capitalise-words', '-c', GetoptLong::NO_ARGUMENT], | |
| ['--verbose', '-v', GetoptLong::NO_ARGUMENT], | |
| ['--force', '-f', GetoptLong::NO_ARGUMENT], | |
| ['--help', '-h', '-?', GetoptLong::NO_ARGUMENT]) | |
| begin | |
| optionparser.each_option { |option, value| | |
| case option | |
| when '--nochange' | |
| $NOCHANGE = true | |
| when '--capitalise-words' | |
| $CAPITALISE_WORDS = true | |
| when '--verbose' | |
| $VERBOSE = true | |
| when '--force' | |
| $FORCE = true | |
| when '--help' | |
| Usage(0) | |
| end | |
| } | |
| rescue | |
| exit(1) | |
| end | |
| if (ARGV.length != 2) | |
| Usage(1) | |
| end | |
| pattern1 = ARGV.shift | |
| pattern2 = ARGV.shift | |
| matches = /^([^*?]*)([*?])([^*?]*)$/.match(pattern1) | |
| (oldhead, oldwild, oldtail) = matches[1..3] if matches | |
| matches = /^([^*?]*)([*?])([^*?]*)$/.match(pattern2) | |
| (newhead, newwild, newtail) = matches[1..3] if matches | |
| if (!oldhead || !oldwild || !oldtail || | |
| !newhead || !newwild || !newtail || | |
| oldwild != newwild) | |
| Usage(1) | |
| end | |
| copy = oldhead.length..(-oldtail.length-1) | |
| #print "wild-range '#{copy}'\n" | |
| # Ruby's Dir[] globbing breaks if the pattern contains a space, sigh. | |
| pattern1 = "#{pattern1}" | |
| pattern1.gsub!(/[ \[]/, "?") | |
| #print "Globbing '#{pattern1}'\n" | |
| files = Dir[pattern1] | |
| files.each { |file| | |
| # print "file '#{file}'\n" | |
| # print "wild-match '#{file[copy]}'\n" | |
| newfile = newhead + file[copy] + newtail | |
| if ($CAPITALISE_WORDS) | |
| newfile.gsub!(/[a-z]+/) { |match| match.capitalize } | |
| end | |
| #print "#{newfile}\n" | |
| if (!$FORCE && File.exists?(newfile)) | |
| print "Not overwriting existing file #{newfile}\n" | |
| else | |
| if ($NOCHANGE or $VERBOSE) | |
| print "mv #{file} #{newfile}\n" | |
| end | |
| if (!$NOCHANGE) | |
| File.rename(file, newfile); | |
| end | |
| end | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment