Using Part of File Name as Parameter

1932
3
Jump to solution
10-18-2016 06:52 AM
ChrisHolmes
Occasional Contributor III

Hello everyone,

I have a parameter named LocNo. It is used as a parameter in a model or two. Currently I have the user manually enter the LocNo value when the model starts. LocNo also happens to be the first 12 characters in the name of the mxd file.

Is there a way that I can grab the first 12 characters and use them as the value passed into the parameter?

For example here is a part of the model that shows how the LocNo is used:

If I can grab those first 12 characters from the filename and insert that into the parameter in the model I'm sure it would save on having some human error introduced.

Thanks for the help,

Chris

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Frequent Contributor

You can use a combination of Parse Path and Calculate value tool, which are model only tools so only accessible from within model builder. The input would have to be a variable of type ArcMap Document and the model would be:

Model

In the example above I use the python expression "%Value%"[:5] to return the first 5 characters of the mxd name. The user would have to select an existing mxd, even if it is the one they already have open. Also note that the output of Parse Path, value, is a precondition to the Calculate value tool.

View solution in original post

3 Replies
DuncanHornby
MVP Frequent Contributor

You can use a combination of Parse Path and Calculate value tool, which are model only tools so only accessible from within model builder. The input would have to be a variable of type ArcMap Document and the model would be:

Model

In the example above I use the python expression "%Value%"[:5] to return the first 5 characters of the mxd name. The user would have to select an existing mxd, even if it is the one they already have open. Also note that the output of Parse Path, value, is a precondition to the Calculate value tool.

View solution in original post

ChrisHolmes
Occasional Contributor III

Thanks for the info Duncan!

Sent from my iPhone

0 Kudos
ChrisHolmes
Occasional Contributor III

Thought I'd provide a quick update. I've moved this functionality over to the following python script. Works well.

import os
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
locPath = str(os.path.basename(mxd.filePath))
LOC = locPath[0:12]
returnLOC = LOC.upper()
arcpy.SetParameterAsText(0, returnLOC)