Skip to content

Instantly share code, notes, and snippets.

@vladistan
Created September 12, 2014 01:57
Show Gist options
  • Save vladistan/6992edb00f93d49af535 to your computer and use it in GitHub Desktop.
Save vladistan/6992edb00f93d49af535 to your computer and use it in GitHub Desktop.
Rename set of directories with numeric part in it by adding 1 to the number.
# coding=utf-8
#The MIT License (MIT)
#
#Copyright (c) <year> <copyright holders>
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
# This small script renames a set of directories with numeric part in their name
# by adding 1 to the number. It takes special care to numerically sort the
# list of directories and work from highest numbered directory to the lowest.
# This way there will be no collisions
#
# Example usage.
#
# Say you have directories names /a/bob1.git /a/bob2.git /a/bob3.git
#
# After running this command
#
# ./FolderRotate /a/bob*.git
#
# You will end up with directories named /a/bob2.git /a/bob3.git /a/bob4.git
import re
import sys
import os.path
from os import listdir, rename
def extract_num(name):
"""
>>> extract_num('bob.1')
'1'
>>> extract_num('bob.2')
'2'
>>> extract_num('bob.2.git')
'2'
"""
m = re.search('[0-9]+', name )
return m.group(0)
def incrname(name):
"""
>>> incrname("bob.1.git")
'bob.2.git'
>>> incrname('x4')
'x5'
"""
n = extract_num(name)
return re.sub(n,str(int(n)+1),name)
def ren_list(list):
"""
>>> tList = ['bob.10.git', 'bob.2.git','bob.1.git','bob.3.git']
>>> ren_list(tList)
[('bob.10.git', 'bob.11.git'), ('bob.3.git', 'bob.4.git'), ('bob.2.git', 'bob.3.git'), ('bob.1.git', 'bob.2.git')]
>>> tList = ['bob.8', 'bob.9','bob.10','bob.11']
>>> ren_list(tList)
[('bob.11', 'bob.12'), ('bob.10', 'bob.11'), ('bob.9', 'bob.10'), ('bob.8', 'bob.9')]
"""
return [(x, incrname(x)) for x in sorted(list, key=lambda x: int(extract_num(x)), reverse=True)]
def rename_folders(folders):
for (x,y) in ren_list(folders):
print x, y
os.rename(x, y)
def main():
print sys.argv[1:]
rename_folders(sys.argv[1:])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment