Tagged: rename

Python file renaming

A renaming script that I use to rename photos, rendered animation, or video frames. Warning, this script will rename your files! There is no undo. Use with care. Constructive comments about Python usage very welcome.

Will work on Unix-like operating systems, not sure about Windows.

[py]
# Python renamer script: creates a function called pyrename(). pyrename() will rename all files in a given folder.
# Usage: Put all the files you want to rename in an isolated folder. The function can be called by typing pyrename().
# Warning, this script will rename your files. There is no undo. Use with care.
def pyrename():
‘Put all the files you want to rename in an isolated folder. The function can be called by typing pyrename().’
import os

#function to ignore the hidden . files in a directory. Note the use of the ‘yield’ keyword
def listdir_nohidden(path):
for f in os.listdir(path):
if not f.startswith(‘.’):
yield f

path = raw_input(‘path to folder?: ‘)
#get the files from the folder and put the filenames in a list called files
theFiles = listdir_nohidden(path)
files = []
for f in theFiles:
files.append(f)

#user supplied values
print ‘Want to replace a character or string in your file names?’
want_to_replace = raw_input(‘Type y or n. Or to completely rename type w: ‘)
if want_to_replace == ‘y’:
replace = raw_input(‘Type the character or string that you want to replace (FYI can be a space!): ‘)
replace_with = raw_input(‘Type the character or string that you want to replace with: ‘)
elif want_to_replace == ‘w’:
replace = ”
replace_with = raw_input(‘Type new name: ‘)
else:
replace = ”
replace_with = ”

if want_to_replace != ‘w’:
want_numbers = raw_input(‘Want your files numbered? type y or n: ‘)
if want_numbers == ‘y’:
zeros = raw_input(‘Type the amount of padding zeros you need (using a single integer, like 4): ‘)
else:
zeros = 0

if want_to_replace == ‘w’:
zeros = raw_input(‘Type the amount of padding zeros you need (using a single integer, like "4"): ‘)

ext = raw_input(‘Please type the three letter extension you want to use ex: jpg (NOT the .): ‘)

#remove extension, put the filenames in a list called names
names = []
for f in files:
if f[-4] == ‘.’:
names.append(f.replace(f[-4:], ”))
else:
names.append(f)

#add new names, add user supplied extension, put the filenames in a list called namesPlusEx
namesPlusEx = []
count = 0
for f in names:
if want_to_replace == ‘w’:
namesPlusEx.append(f.replace(f, replace_with)+ ((‘.%.’)+zeros+(‘d’))% count +’.’+ ext)
elif want_to_replace != ‘w’ and want_numbers == ‘y’:
namesPlusEx.append(f.replace(replace, replace_with)+ ((‘.%.’)+zeros+(‘d’))% count +’.’+ ext)
else:
namesPlusEx.append(f.replace(replace, replace_with)+’.’+ ext)
count += 1

#rename the actual files
c=0
for f in files:
os.rename(path+’/’+f, path+’/’+namesPlusEx[c])
c+=1

print ‘You have re-named %d files’ % len(files)

[/py]

rename.py

The following script will remove the first four characters of all files in a specified folder. You may alter the script to remove other characters by altering this line: names.append(f.replace(f[0:4], ”)) change the 0:4 to another bit as defined by python slice notation.

[py]

# Python renamer script: creates a function called pyrename(). pyrename() will rename all files in a given folder.
# Usage: Put all the files you want to rename in an isolated folder. The function can be called by typing pyrename().
# Warning, this script will rename your files. There is no undo. Use with care.
def pyrename():
‘Put all the files you want to rename in an isolated folder. The function can be called by typing pyrename().’
import os

#function to ignore the hidden . files in a directory. Note the use of the ‘yield’ keyword
def listdir_nohidden(path):
for f in os.listdir(path):
if not f.startswith(‘.’):
yield f

path = raw_input(‘path to folder?: ‘)
#get the files from the folder and put the filenames in a list called files
theFiles = listdir_nohidden(path)
files = []
for f in theFiles:
files.append(f)

#remove extension, put the filenames in a list called names
names = []
for f in files:
names.append(f.replace(f[0:4], ”))

#rename the actual files
i=0
for f in files:
os.rename(path+’/’+f, path+’/’+names[i])
i+=1

print ‘You have re-named %d files’ % len(files)

[/py]

Rename multiple files with Mac shell script

Simple renaming and renumbering script for multiple files

num=0 for j in *.jpg do mv "$j" `printf "myFILE.%05d.jpg" $num` num=`echo "$num + 1" | bc` done

Usage: In any text editor, modify the script so that the extension in the second line (here .jpg) exactly matches the extension of the files you want to modify. Also, change the file name in line four (here myFILE) to whatever name you want to use. In the Terminal, cd to the directory with your files. Paste the script to the terminal, hit enter.

Note, the way this is set up, the script will rename every file of the specified .extension in the current directory.

Replace file name suffix:

Another script. This script changes the suffix of each specified file in a directory. In this example from “myName_000_01.JPG” to “myName_000.jpg”

for i in *.JPG do mv $i `basename -s _01.JPG $i`.jpg done

Edit the the first line of the script so that it works on the appropriate files. In this example we have several files that have the extension of .JPG in common. Each of these files will be renamed.

The “mv” unix command is used to rename the files. Using the “basename” utility and the “-s” flag we can designate what part original name we want to keep, rather the suffix we want to remove. In this example the suffix we want to remove is “_01.JPG”.

Specify the new suffix that you want to add (here it’s “.jpg”).

Next, open a terminal window. Navigate to the directory where your files reside. Enter “sh” in the window, hit enter. Paste the script, and hit enter. That’s it.

scripts written with the assistance of Jeshua Lacock of OpenOSX.com

MEL renaming

Maya has a built in re-naming tool, Rename Selected Objects, on the right side of the Status Line. Select Rename from the menu of line input operations, to the left of the input box.

image of Maya's Rename selected objects dialog

Basic rename script using Python:

[py]
#rename selected Maya nodes. Adds 4 digit padding to numbered name.
import maya.cmds as mc
mySel = mc.ls(sl=True)
count = 0
for obj in mySel:
count +=1
#customize name below
mc.rename( obj, ‘myObj2_’ + str(‘%04d’ % count))
[/py]

Below are various methods to rename using MEL:

Simple approach

int $i; //Select the object(s) that you want to rename string $rename[] = `ls -sl`; int $selSize = `size $rename`; for ($i =0; $i <$selSize ; $i++){ //Change newName_, below, to the name that you want to replace. rename ($rename[$i]) ("newName_" + ($i +1)); }

Search and replace.
Designed to find and replace part of the name of similarly named nodes.

searchReplaceNames "myNode" "newNode" "selected";

This will change names in a selection. For example you can change 100 selected nodes names from “myNode000 – myNode100” to “newNode000 – etc”. You can replace “selected” with “hierarchy” to rename all of the nodes in a selected hierarchy, or “all” to rename all nodes in the scene.

Search and replace 2

string $search = "myNode"; string $replace = "newNode"; string $Objs[] = `ls -sl`; for($obj in $Objs) { if(`startsWith $obj $search`) { rename $obj (`substitute $search $obj $replace`); } }

Rename and add custom number

//by pappapierre
//catch the object(s) current names, and clear your selection
string $sel[] = `ls -sl`;
select -cl;

//declare a string variable which will become your new name prefix

string $newNamePrefix = "myNewName";

//now run a loop to rename each elemnt in your selection list in the order
//they were selected
for($i=0 ; $i<size($sel); $i++){

//and rename your objects accordingly
rename $sel[$i] ($newNamePrefix + ($i+1));

}
//you can start your numbers anywhere by replacing the "1" in ($i+1) with
//your desired starting number.

Rename shape nodes:

//script 1. rename shapes unique names
string $rename[] = `ls -sl -dag -shapes`;

for ($i = 0; $i<size ($rename); $i++) {

rename($rename[$i]) ("myNewShapeName_" + ($i +1));

}

//script 2. remnameShapes (all the same name)

for ($i in `ls -sl -dag -shapes`) {

rename $i "myShape";

}

//script 3. rename types

for ($i in `ls -sl -dag -type “nCloth”`) {

rename $i "myShape";

MEL change attribute on multiple objects

Using the channel box or the attribute spreadsheet are good ways to manually change attributes on multiple selected objects.

In MEL, this script is set up to change attributes on selected nodes in the scene.

string $mySel[] = `ls-sl`; string $name; int $arraySize = `size $mySel`; for ($i = 0; $i < $arraySize; $i++) { $name = $mySel[$i]; $myNode = ($name + ".rotateX"); setAttr $myNode 100; }

If you want to make changes to the shape node, you will want to use ls -sl -dag -leaf:

string $mySel[] = `ls -sl -dag -leaf`; for ($each in $mySel) { setAttr ($each +".attribute") 1; } print $mySel;