<?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: ListFields as a script tool in ModelBuilder in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1312246#M71463</link>
    <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/help/analysis/geoprocessing/modelbuilder/inline-variable-substitution.htm" target="_blank" rel="noopener"&gt;Inline variables&lt;/A&gt; are meant to replace parameter input in the tool's GUI, not in its code.&lt;/P&gt;&lt;P&gt;To get a parameter value inside the tool's code, use the &lt;A href="https://pro.arcgis.com/de/pro-app/latest/arcpy/functions/getparameter.htm" target="_blank" rel="noopener"&gt;GetParameter&lt;/A&gt; functions.&lt;/P&gt;&lt;P&gt;print() won't show anything inside a tool, as the tool doesn't use the Python Window as output. Use &lt;A href="https://pro.arcgis.com/de/pro-app/latest/arcpy/functions/addmessage.htm" target="_blank" rel="noopener"&gt;AddMessage&lt;/A&gt;&amp;nbsp;instead to output into the tool messages.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
if __name__ == "__main__":
    in_table = arcpy.GetParameter(0)
    for f in arcpy.ListFields(in_table):
        arcpy.AddMessage([f.name, f.type])&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1690397772498.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76418i09CBA6B76E603F49/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_0-1690397772498.png" alt="JohannesLindner_0-1690397772498.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then you can easily add it to a model:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1690397818682.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76419i70AB971E2423093E/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_1-1690397818682.png" alt="JohannesLindner_1-1690397818682.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_2-1690397864278.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76420iE505F394A12F13FE/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_2-1690397864278.png" alt="JohannesLindner_2-1690397864278.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want something more permanent than a simple print output, you can use this script as a script tool. It will iterate through each table and feature class in a workspace and store the field information in a new table.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# parameter 1: Workspace, input
# parameter 2: Table, output

import arcpy
from pathlib import Path

in_ws = arcpy.GetParameterAsText(0)
out_table = arcpy.GetParameterAsText(1)

# create output table
folder = str(Path(out_table).parent)
name = Path(out_table).stem
out_table = arcpy.management.CreateTable(folder, name)
arcpy.management.AddField(out_table, "TableName", "TEXT")
arcpy.management.AddField(out_table, "FieldName", "TEXT")
arcpy.management.AddField(out_table, "FieldType", "TEXT")

# start writing
with arcpy.da.InsertCursor(out_table, ["TableName", "FieldName", "FieldType"]) as cursor:
    # iterate through the workspace's feature classes and tables
    walk = arcpy.da.Walk(in_ws, datatype=["FeatureClass","Table"])
    for dir_path, dir_names, file_names in walk:
        for file_name in file_names:
            in_table = str(Path(dir_path) / file_name)
            # get the fields
            fields = arcpy.ListFields(in_table)
            # write the fields into the output table
            for f in fields:
                row = [in_table, f.name, f.type]
                cursor.insertRow(row)

# set the output parameter
arcpy.SetParameter(1, out_table)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_3-1690399312529.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76421iE5D338665336B90B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_3-1690399312529.png" alt="JohannesLindner_3-1690399312529.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_4-1690399348840.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76423iCB27C0A501284AA3/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_4-1690399348840.png" alt="JohannesLindner_4-1690399348840.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jul 2023 19:23:14 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2023-07-26T19:23:14Z</dc:date>
    <item>
      <title>ListFields as a script tool in ModelBuilder</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1311983#M71445</link>
      <description>&lt;P&gt;I've created a simple script tool which should list all field names from all feature classes in a geodatabase. I struggle with the correct syntax when including inline variable in the script, which is why the script tool is not&amp;nbsp; doing what it should within the model. Any links with information or suggestions would be kindly appreciated. I've looked in plenty of forums but not been able to make the script work within the modelbuilder. Also tried implementing it in the calculate value tool but it doesn't seem to work.&lt;/P&gt;&lt;P&gt;The script performs fine when not part of the model and only when a shapefile is specified.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script contents.PNG" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76347i453C315AEC26C937/image-size/large?v=v2&amp;amp;px=999" role="button" title="script contents.PNG" alt="script contents.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script within model.PNG" style="width: 674px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76346iF39B289C5AC7B3E8/image-size/large?v=v2&amp;amp;px=999" role="button" title="script within model.PNG" alt="script within model.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 13:23:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1311983#M71445</guid>
      <dc:creator>DesislavaKsiazek</dc:creator>
      <dc:date>2023-07-26T13:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: ListFields as a script tool in ModelBuilder</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1312246#M71463</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/help/analysis/geoprocessing/modelbuilder/inline-variable-substitution.htm" target="_blank" rel="noopener"&gt;Inline variables&lt;/A&gt; are meant to replace parameter input in the tool's GUI, not in its code.&lt;/P&gt;&lt;P&gt;To get a parameter value inside the tool's code, use the &lt;A href="https://pro.arcgis.com/de/pro-app/latest/arcpy/functions/getparameter.htm" target="_blank" rel="noopener"&gt;GetParameter&lt;/A&gt; functions.&lt;/P&gt;&lt;P&gt;print() won't show anything inside a tool, as the tool doesn't use the Python Window as output. Use &lt;A href="https://pro.arcgis.com/de/pro-app/latest/arcpy/functions/addmessage.htm" target="_blank" rel="noopener"&gt;AddMessage&lt;/A&gt;&amp;nbsp;instead to output into the tool messages.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
if __name__ == "__main__":
    in_table = arcpy.GetParameter(0)
    for f in arcpy.ListFields(in_table):
        arcpy.AddMessage([f.name, f.type])&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1690397772498.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76418i09CBA6B76E603F49/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_0-1690397772498.png" alt="JohannesLindner_0-1690397772498.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then you can easily add it to a model:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1690397818682.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76419i70AB971E2423093E/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_1-1690397818682.png" alt="JohannesLindner_1-1690397818682.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_2-1690397864278.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76420iE505F394A12F13FE/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_2-1690397864278.png" alt="JohannesLindner_2-1690397864278.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want something more permanent than a simple print output, you can use this script as a script tool. It will iterate through each table and feature class in a workspace and store the field information in a new table.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# parameter 1: Workspace, input
# parameter 2: Table, output

import arcpy
from pathlib import Path

in_ws = arcpy.GetParameterAsText(0)
out_table = arcpy.GetParameterAsText(1)

# create output table
folder = str(Path(out_table).parent)
name = Path(out_table).stem
out_table = arcpy.management.CreateTable(folder, name)
arcpy.management.AddField(out_table, "TableName", "TEXT")
arcpy.management.AddField(out_table, "FieldName", "TEXT")
arcpy.management.AddField(out_table, "FieldType", "TEXT")

# start writing
with arcpy.da.InsertCursor(out_table, ["TableName", "FieldName", "FieldType"]) as cursor:
    # iterate through the workspace's feature classes and tables
    walk = arcpy.da.Walk(in_ws, datatype=["FeatureClass","Table"])
    for dir_path, dir_names, file_names in walk:
        for file_name in file_names:
            in_table = str(Path(dir_path) / file_name)
            # get the fields
            fields = arcpy.ListFields(in_table)
            # write the fields into the output table
            for f in fields:
                row = [in_table, f.name, f.type]
                cursor.insertRow(row)

# set the output parameter
arcpy.SetParameter(1, out_table)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_3-1690399312529.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76421iE5D338665336B90B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JohannesLindner_3-1690399312529.png" alt="JohannesLindner_3-1690399312529.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_4-1690399348840.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76423iCB27C0A501284AA3/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_4-1690399348840.png" alt="JohannesLindner_4-1690399348840.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 19:23:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1312246#M71463</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-07-26T19:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: ListFields as a script tool in ModelBuilder</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1312452#M71485</link>
      <description>&lt;P&gt;Hi Johannes! Wow, you've gone above and beyond to answer this. I'll try it and let you know how I got on. Hopefully this will be useful to other users who come across similar issue &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 08:53:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1312452#M71485</guid>
      <dc:creator>DesislavaKsiazek</dc:creator>
      <dc:date>2023-07-27T08:53:08Z</dc:date>
    </item>
    <item>
      <title>Re: ListFields as a script tool in ModelBuilder</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1312463#M71486</link>
      <description>&lt;P&gt;Yep, all worked fine. Great outputs.&amp;nbsp; The table one will be incredibly useful further than my model. Many thanks for helping me&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 09:49:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/listfields-as-a-script-tool-in-modelbuilder/m-p/1312463#M71486</guid>
      <dc:creator>DesislavaKsiazek</dc:creator>
      <dc:date>2023-07-27T09:49:51Z</dc:date>
    </item>
  </channel>
</rss>

