Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Does anybody have an ArcPy script which points to a directory and renames files within that directory?

For example, I have approximately 1500 raster files with names along the lines of m_3609922_nw_14_1_20100425 which need to be subsetted to: "3609922nw".

Thanks much!

-Aaron

share|improve this question
1  
Does it have to be ArcPy? If the filenames are that uniform, it is easy to create a batch file that does what you want using a combination of Excel and a text editor. Otherwise, with moderate difficulty (no more that ArcPy) you could write a batch file that uses regex to rename the files. –  user3461 Jun 13 '12 at 19:31
add comment

1 Answer

up vote 7 down vote accepted

Something like this ought to get you going...

import os
import arcpy
from arcpy import env

# directory containing rasters to be renamed
RASTER_DIR = 'C:/MyRasters/'

env.workspace = RASTER_DIR
for raster in arcpy.ListRasters():

    # get the raster name and file extension
    fileName,fileExtension = os.path.splitext(raster)

    # do some shenanigans to rename the file
    # based on your sample raster name, fileNameParts would look like
    # fileNameParts[0] = m
    # fileNameParts[1] = 3609922
    # fileNameParts[2] = nw
    # fileNameParts[3] = 14
    # fileNameParts[4] = 1
    # fileNameParts[5] = 20100425

    fileNameParts = fileName.split('_')
    compactFileName = fileNameParts[1] + fileNameParts[2] + fileExtension

    arcpy.Rename_management(raster,compactFileName)
share|improve this answer
    
Great, thanks a bunch. That will do it. –  Aaron Jun 13 '12 at 20:19
add comment

protected by whuber Jul 20 '12 at 12:46

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.