<?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 ValidateTableName function in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/validatetablename-function/m-p/1496051#M70898</link>
    <description>&lt;P&gt;Hello, I am relatively new to Python scripting, and trying to automate a task at work. Basically, the script is supposed to copy a shape file [named 'backbone'] from the env.workspace into the specified file GDB. The script works fine, but I am trying to run this script numerous times by changing the env.workspace and re-running the script.&amp;nbsp;&lt;/P&gt;&lt;P&gt;With that being said, if there was a feature class in the file GDB already named 'backbone', the original script would copy the new one over the original. I thought that arcpy.ValidateName() was the function used to prevent this from happening, and it would rename the new feature class so the original would not be written over; is this a misconception on my end?&lt;/P&gt;&lt;P&gt;After some trial and error, I was able to add a nested if statement that checked for a feature class named 'backbone', and if true, would add a '1' to the end of the new feature class's name. Here lies the problem: if I change the env.workspace to another file location, it will just restart my counter variable and overwrite 'backbone1'. I'm sure the solution is simple, just beyond my elementary knowledge base; any help would be greatly appreciated! I have copy and pasted the script below:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;env.overwriteOutput = True&lt;BR /&gt;env.workspace = 'C:/Users/MWright/Downloads/YaleToSC/'&lt;BR /&gt;outputEnv = 'M:/Drafting/IECcoop/PhaseIII/PawneeToYale/PawneeToYales.gdb/'&lt;BR /&gt;x = 'backbone'&lt;BR /&gt;count = 1&lt;/P&gt;&lt;P&gt;fclist = arcpy.ListFeatureClasses()&lt;BR /&gt;print(fclist)&lt;BR /&gt;print('\n')&lt;/P&gt;&lt;P&gt;for fc in fclist:&lt;BR /&gt;&amp;nbsp; &amp;nbsp;fcdesc = arcpy.Describe(fc)&lt;BR /&gt;&amp;nbsp; &amp;nbsp;fcname = fcdesc.basename&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if fcname == 'backbone':&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if arcpy.Exists(x):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;fcout = arcpy.ValidateTableName(fcname, outputEnv)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.CopyFeatures_management(fc, outputEnv + fcout + str(x))&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('\n' + fcname + ' has been copied to GDB.\n')&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x += 1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;fcout = arcpy.ValidateTableName(fcname, outputEnv)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.CopyFeatures_management(fc, outputEnv + fcout + str(count))&lt;BR /&gt;&amp;nbsp; &amp;nbsp;else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; print(fcname + " feature class does not need to be copied to geodatabase.")&lt;/P&gt;&lt;P&gt;print('\nBackbone feature class copy process complete.')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 21 Jun 2024 16:00:27 GMT</pubDate>
    <dc:creator>Trans-TelCentral</dc:creator>
    <dc:date>2024-06-21T16:00:27Z</dc:date>
    <item>
      <title>ValidateTableName function</title>
      <link>https://community.esri.com/t5/python-questions/validatetablename-function/m-p/1496051#M70898</link>
      <description>&lt;P&gt;Hello, I am relatively new to Python scripting, and trying to automate a task at work. Basically, the script is supposed to copy a shape file [named 'backbone'] from the env.workspace into the specified file GDB. The script works fine, but I am trying to run this script numerous times by changing the env.workspace and re-running the script.&amp;nbsp;&lt;/P&gt;&lt;P&gt;With that being said, if there was a feature class in the file GDB already named 'backbone', the original script would copy the new one over the original. I thought that arcpy.ValidateName() was the function used to prevent this from happening, and it would rename the new feature class so the original would not be written over; is this a misconception on my end?&lt;/P&gt;&lt;P&gt;After some trial and error, I was able to add a nested if statement that checked for a feature class named 'backbone', and if true, would add a '1' to the end of the new feature class's name. Here lies the problem: if I change the env.workspace to another file location, it will just restart my counter variable and overwrite 'backbone1'. I'm sure the solution is simple, just beyond my elementary knowledge base; any help would be greatly appreciated! I have copy and pasted the script below:&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;from arcpy import env&lt;/P&gt;&lt;P&gt;env.overwriteOutput = True&lt;BR /&gt;env.workspace = 'C:/Users/MWright/Downloads/YaleToSC/'&lt;BR /&gt;outputEnv = 'M:/Drafting/IECcoop/PhaseIII/PawneeToYale/PawneeToYales.gdb/'&lt;BR /&gt;x = 'backbone'&lt;BR /&gt;count = 1&lt;/P&gt;&lt;P&gt;fclist = arcpy.ListFeatureClasses()&lt;BR /&gt;print(fclist)&lt;BR /&gt;print('\n')&lt;/P&gt;&lt;P&gt;for fc in fclist:&lt;BR /&gt;&amp;nbsp; &amp;nbsp;fcdesc = arcpy.Describe(fc)&lt;BR /&gt;&amp;nbsp; &amp;nbsp;fcname = fcdesc.basename&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if fcname == 'backbone':&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if arcpy.Exists(x):&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;fcout = arcpy.ValidateTableName(fcname, outputEnv)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.CopyFeatures_management(fc, outputEnv + fcout + str(x))&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;print('\n' + fcname + ' has been copied to GDB.\n')&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x += 1&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;fcout = arcpy.ValidateTableName(fcname, outputEnv)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;arcpy.CopyFeatures_management(fc, outputEnv + fcout + str(count))&lt;BR /&gt;&amp;nbsp; &amp;nbsp;else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; print(fcname + " feature class does not need to be copied to geodatabase.")&lt;/P&gt;&lt;P&gt;print('\nBackbone feature class copy process complete.')&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jun 2024 16:00:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/validatetablename-function/m-p/1496051#M70898</guid>
      <dc:creator>Trans-TelCentral</dc:creator>
      <dc:date>2024-06-21T16:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: ValidateTableName function</title>
      <link>https://community.esri.com/t5/python-questions/validatetablename-function/m-p/1496809#M70914</link>
      <description>&lt;P&gt;You're really close, you just need to store the index in the feature names themselves:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from arcpy import env

env.overwriteOutput = True
env.workspace = 'C:/Users/MWright/Downloads/YaleToSC/'
outputEnv = 'M:/Drafting/IECcoop/PhaseIII/PawneeToYale/PawneeToYales.gdb/'
outName = 'backbone'

fclist = arcpy.ListFeatureClasses()

print(fclist)
print('\n')

# Iterate FCs in workspace (YaleToSC)
for fc in fclist:
   fcdesc = arcpy.Describe(fc)
   fcname = fcdesc.basename
   
   # Check if the FC name contains the substring 'backbone'
   if 'backbone' in fcname:
      # Split the FC name into a list of substrings separated by '_', Use *_ to consume any additional substrings beyond the first 2
      # Add None to end of list to prevent ValueError if the FC name does not contain '_' and the list has only 1 element
      name, index, *_ = fcname.split('_') + [None]
      
      # If the name is just 'backbone', initialize the index to 1
      if name == 'backbone' and not index:
         arcpy.CopyFeatures_management(fc, f"{outputEnv}{outName}_1")
      
      # If the name is 'backbone' followed by a number, increment the number by 1
      elif index and index.isdigit():
         arcpy.CopyFeatures_management(fc, f"{outputEnv}{outName}_{int(index) + 1}")
         
   # Ignore all non-backbone feature classes
   else:
      print(f'{fcname} is not a backbone feature class.')
      
print('\nBackbone feature class copy process complete.')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a version that runs as a script and takes a list of workspaces:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from arcpy import env

# Iterate FCs in workspace (YaleToSC)

def copy_backbone(outputEnv, outName, fclist):
    for fc in fclist:
       fcdesc = arcpy.Describe(fc)
       fcname = fcdesc.basename
   
   # Check if the FC name contains the substring 'backbone'
       if 'backbone' in fcname:
      # Split the FC name into a list of substrings separated by '_', Use *_ to consume any additional substrings beyond the first 2
      # Add None to end of list to prevent ValueError if the FC name does not contain '_' and the list has only 1 element
          name, index, *_ = fcname.split('_') + [None]
      
      # If the name is just 'backbone', initialize the index to 1
          if name == 'backbone' and not index:
             arcpy.CopyFeatures_management(fc, f"{outputEnv}{outName}_1")
      
      # If the name is 'backbone' followed by a number, increment the number by 1
          elif index and index.isdigit():
             arcpy.CopyFeatures_management(fc, f"{outputEnv}{outName}_{int(index) + 1}")
         
   # Ignore all non-backbone feature classes
       else:
          print(f'{fcname} is not a backbone feature class.')

def main():
   env.overwriteOutput = True
   outputEnv = 'M:/Drafting/IECcoop/PhaseIII/PawneeToYale/PawneeToYales.gdb/'
   outName = 'backbone'
   workspaces = ['C:/Users/MWright/Downloads/YaleToSC/', '&amp;lt;wsp1&amp;gt;', '&amp;lt;wsp2&amp;gt;', ...]
   
   for workspace in workspaces:
      env.workspace = workspace
      fclist = arcpy.ListFeatureClasses()
      print(f'Processing {workspace}...')
      print(f'Feature classes in {workspace}: {fclist}')
      copy_backbone(outputEnv, outName, fclist)

   print('\nBackbone feature class copy process complete.')

if __name__ == '__main__':
   main()&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 24 Jun 2024 19:12:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/validatetablename-function/m-p/1496809#M70914</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2024-06-24T19:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: ValidateTableName function</title>
      <link>https://community.esri.com/t5/python-questions/validatetablename-function/m-p/1503801#M71033</link>
      <description>&lt;P&gt;This helps so much - thanks for pointing me in the right direction!&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 20:00:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/validatetablename-function/m-p/1503801#M71033</guid>
      <dc:creator>Trans-TelCentral</dc:creator>
      <dc:date>2024-07-10T20:00:31Z</dc:date>
    </item>
  </channel>
</rss>

