Copying file names in folder directory to FGDB Table

412
4
Jump to solution
08-05-2021 01:25 PM
CraigPrisland2
New Contributor III

Hello,

I'm looking to build a Python script that will take the file names of PDFs in a certain folder directory and copy them to a new FGDB table.  I am able to print these file names using the following simple script:

import os, arcpy

arcpy.env.workspace = r'\\ch_gis\gis\......'
files = arcpy.ListFiles()
for f in files:
   print f

 and get the expected outcome:

CraigPrisland2_0-1628195081366.png

However, I am having troubles trying to copy these file names to an empty FGDB table.  Any assistance would be appreciated.

Thanks,

Craig

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

untested

import arcpy
import os

arcpy.env.workspace = r'\\ch_gis\gis\......'
files = arcpy.ListFiles()
for f in files:
   print(f)

fields = ['your field']
cursor = arcpy.da.InsertCursor(r'path to your table', fields)

for f in files:
    cursor.insertRow((f))

del cursor

View solution in original post

0 Kudos
4 Replies
DavidPike
MVP Frequent Contributor

What do you mean by copy them exactly? as in just add the filepath strings to a column in the table? Does the table and column already exist? I'd say just an insert cursor.

0 Kudos
CraigPrisland2
New Contributor III

David,

To clarify, I do have a table and field already created.  I'm looking to just copy the file names (i.e. WP20150014.pdf) to the table.  Thanks for the suggestion of the insert cursor.

0 Kudos
DavidPike
MVP Frequent Contributor

untested

import arcpy
import os

arcpy.env.workspace = r'\\ch_gis\gis\......'
files = arcpy.ListFiles()
for f in files:
   print(f)

fields = ['your field']
cursor = arcpy.da.InsertCursor(r'path to your table', fields)

for f in files:
    cursor.insertRow((f))

del cursor
0 Kudos
CraigPrisland2
New Contributor III

Thanks David.  This works great.  The only change that I made to your code was to add brackets around 'f' so it read: cursor.insertrow([f])

0 Kudos