<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Renaming a shapefile using values from a text field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208385#M16108</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To add the shapefile to ArcMap, you will first need to a make a feature layer, then add the feature layer.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;shapefile = r"C:\temp\Airports.shp"

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.MakeFeatureLayer_management(shapefile, "Airports")
arcpy.mapping.AddLayer(df, shapefile, "AUTO_ARRANGE")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To move shapefiles to another directory, you can use the Copy geoprocessing tool.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 10:17:35 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-11T10:17:35Z</dc:date>
    <item>
      <title>Renaming a shapefile using values from a text field</title>
      <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208381#M16104</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does anyone know how I might create a python script that would use an attribute value from a table and use that value as a file name prefix when renaming a shapefile?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, as part of my script I have converted a time field such that it contains no back slashes and can be used as a unique file name. Now that I have a field with the unique text I would like to rename a generic shapefile using the date text as a prefix inorder to make the file name unique. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For further clarification, the input file is always "generic.shp" and the desired output would look something like this, "02202013_generic.shp" based upon what date is used in the date field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pete&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Feb 2013 21:19:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208381#M16104</guid>
      <dc:creator>PeterDalrymple</dc:creator>
      <dc:date>2013-02-20T21:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a shapefile using values from a text field</title>
      <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208382#M16105</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Without having worked through the problem myself, I would say you will probably want to use a search cursor on whatever table has the date field or string field with the date in it.&amp;nbsp; That should give you access to the field values for a particular record in a table or feature class.&amp;nbsp; The new data access module might have something even more interesting, but I haven't gotten to even browsing it yet.&amp;nbsp; You could also do the date conversion in Python itself and just read the date directly from the date field.&amp;nbsp; I don't know what kind of result a search cursor gives for a date field in terms of Python types, but there are date, time, and datetime modules for working with dates and times.&amp;nbsp; That could help you avoid adding any unnecessary fields.&amp;nbsp; Lastly, I would say use arcpy.management.Rename() to rename generic.shp to 02202013_generic.shp.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Feb 2013 04:37:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208382#M16105</guid>
      <dc:creator>NathanHeick</dc:creator>
      <dc:date>2013-02-21T04:37:27Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a shapefile using values from a text field</title>
      <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208383#M16106</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Peter,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You should not name a shapefile beginning with a number:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://support.esri.com/en/knowledgebase/techarticles/detail/23087" rel="nofollow noopener noreferrer" target="_blank"&gt;http://support.esri.com/en/knowledgebase/techarticles/detail/23087&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As the previous post stated, you could do this using a search cursor.&amp;nbsp; Not sure how you want to iterate through the table, but you could do this using a where clause.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;rows = arcpy.SearchCursor(fc, "OBJECTID = 1")
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; time = row.Time

del row, rows

arcpy.Rename_management(fc, time)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:17:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208383#M16106</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T10:17:32Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a shapefile using values from a text field</title>
      <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208384#M16107</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks very much for your replies. The rename.management did work and so did search cursor recommendations. With some small changes I put the date string after the file name (e.g. generic_130220.shp).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now for the next question...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I rename the file, the name does not change in the TOC even after refreshing. I added a line of code to remove the feature from the mxd, but now would like to add the renamed file to the map. I have no problem adding a shapefile to the map at the beginning of the script when the shapefile is the same from day to day. However, once the shapefile has been renamed I am having a hard time finding a way to add the file based on its directory location instead of specific name. I can list the files in the directory, but can anyone point me in the right direction for then adding the listed files to my TOC? The plan is to then move the files to a directory where they will eventually be merged. If you have any recommendations on that process too I would greatly appreciate it since the files to be moved will vary from day to day as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;- Pete&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Feb 2013 00:55:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208384#M16107</guid>
      <dc:creator>PeterDalrymple</dc:creator>
      <dc:date>2013-02-22T00:55:39Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a shapefile using values from a text field</title>
      <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208385#M16108</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To add the shapefile to ArcMap, you will first need to a make a feature layer, then add the feature layer.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;shapefile = r"C:\temp\Airports.shp"

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.MakeFeatureLayer_management(shapefile, "Airports")
arcpy.mapping.AddLayer(df, shapefile, "AUTO_ARRANGE")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To move shapefiles to another directory, you can use the Copy geoprocessing tool.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:17:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208385#M16108</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T10:17:35Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a shapefile using values from a text field</title>
      <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208386#M16109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Jake,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code you posted specifies a specific path to the shapefile to be added. However, if the name of the file changes, the script would not be able to be used repeatedly without updating the file name/path. Is there a way to add/convert the layer based only on the directory in which it resides? That way the process could be automated to convert and add which ever file exists in the directory at the time the script was run. Once the file is copied to a new directory, the process could be run on any new files that are moved into the directory that is referenced by the script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To further explain, I am creating a daily rountine to reformat shapefiles that are updated on a daily basis. The shapefiles are archived with a date in order to give them a unique name. However, the input file name is always the same until the date suffix is added. Once the suffix is added, the file name becomes unique, but the directory is always consistant. There will only ever be one file at a time in the directory where the processing is done. I am fairly new to python scripting so I apologize if I am not explaining this in the best way. For context, I've pasted the script below. Again, my apologies if the script seems a bit amateurish, but I am just geting started with the language:-)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As you can see, the last step is to remove the layer with the generic name. I am getting hung up on performing any further processes once the file name becomes variable. Ideally, I could reference the file based on its directory rather than its file name since the file name will be variable at this point. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]
newlayer = arcpy.mapping.Layer("C:\pathname\detentio.shp")
arcpy.mapping.AddLayer(df, newlayer,"TOP")
arcpy.AddField_management ("detentio", "Date_Txt", "TEXT","","",10,"Date_Txt","NULLABLE","NON_REQUIRED","")
arcpy.CalculateField_management("detentio","Date_Txt","[Date_Visit]","VB","#")
arcpy.ConvertTimeField_management("detentio","Date_Txt","MM/dd/yyyy;1033;;","Date_Mod","TEXT","yyMMdd;1033;;")
arcpy.DeleteField_management("detentio","Date_Txt")
arcpy.AddField_management ("detentio", "PhotoName", "TEXT","","",40,"PhotoName","NULLABLE","NON_REQUIRED","")
arcpy.CalculateField_management ("detentio", "PhotoName","[Date_Mod]&amp;amp; Chr(95) &amp;amp; right([photo],10)","VB")
arcpy.AddField_management ("detentio", "PhotoPath", "TEXT","","",200,"PhotoPath","NULLABLE","NON_REQUIRED","")
arcpy.CalculateField_management ("detentio", "PhotoPath",'"V:\newpathname\\"&amp;amp;[PhotoName]',"VB")
arcpy.DeleteField_management("detentio",["PhotoName"])
arcpy.DeleteField_management("detentio",["photo"])
from arcpy import env
env.workspace = "C:\pathname"
fc = "detentio"
rows = arcpy.SearchCursor("detentio", "FID = 1")
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; time = row.Date_Mod
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del row, rows
arcpy.DeleteField_management("detentio",["Date_Mod"])
arcpy.Rename_management("detentio.shp", "detentio" + "_" + time, "ShapeFile")
mxd = arcpy.mapping.MapDocument("Current")
for df in arcpy.mapping.ListDataFrames(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(mxd, "", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name.lower() == "detentio":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.RemoveLayer(df, lyr)
print "Processing Complete."&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again for your help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;- Pete&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:17:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208386#M16109</guid>
      <dc:creator>PeterDalrymple</dc:creator>
      <dc:date>2021-12-11T10:17:37Z</dc:date>
    </item>
    <item>
      <title>Re: Renaming a shapefile using values from a text field</title>
      <link>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208387#M16110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can iterate through the directory using the arcpy.ListFeatureClasses function, and then add the shapefile that's found.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env
env.workspace = r"C:\temp\python"

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

lstFCs = arcpy.ListFeatureClasses("*.shp")
for fc in lstFCs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management(fc, "fc")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer = arcpy.mapping.Layer("fc")
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = arcpy.mapping.Layer("fc")
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.name = fc.split(".")[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This code finds all shapefiles within the C:\temp\python directory and adds it to ArcMap.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:17:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/renaming-a-shapefile-using-values-from-a-text/m-p/208387#M16110</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T10:17:40Z</dc:date>
    </item>
  </channel>
</rss>

