Select to view content in your preferred language

Batch shorten/rename .ASC raster names

2813
3
09-24-2013 05:01 PM
by Anonymous User
Not applicable
Original User: evandebroek

Hi,
I have hundreds of ASCII rasters with very long file names that I need to shorten before I can do subsequent geoprocessing (I get a flurry of errors because the names are too long and have unusual characters in them). Unfortunately, these files are automatically named by a modeling program and I cannot change the way they are named to start with.

For example, I want to rename:
Run3-37_output, 2080, 1.664 meters _GIS.ASC

To:
R3372080n.ASC

What is the best way to do this? All the file names are the same structure, so I could use string parsing functions like "Left()" and "Right()" but by what means? Ideally I'd like to do this in ModelBuilder.

Thanks for your suggestions!
Elena V
0 Kudos
3 Replies
NidhinKarthikeyan
Honored Contributor
You can try this Python script:


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)
0 Kudos
NidhinKarthikeyan
Honored Contributor
0 Kudos
by Anonymous User
Not applicable
Original User: evandebroek

Thanks Nidhin. I'm not sure I understand this script. Sorry, I'm not very familiar with python. What are the different "filenameparts[]" that you're creating? like what is "m" and "3609922" and "n". Are these look-up codes or something?

Run3-37_output, 2080, 1.664 meters _GIS.ASC

I'm trying to combine "R" + "3-37" + Year + "n" + .ASC 

(n stands for another input to the model that is not in the file name)

How does your code do that? Once the code is complete, I just paste it in the Python window within GIS, correct?
Thanks!


You can try this Python script:


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)
0 Kudos