Select to view content in your preferred language

loop through MXDs for change of name and type (Shp to GDB) in one layer only

329
3
06-12-2024 12:30 PM
kaww02
by
Occasional Contributor

I thought I had this working before with your help but I was mistaken. I have continued to tweak and here is where I am. When I run it in VB I get the error:  "EOL while scanning string literal"

In all the forums and documents I see I can't seem to find a shapefile to a completely new location/GBD. I wonder if I need to break this change up so that maybe I change name  (to new GBD file name) then change source - IDK. Here is the code:

 

import arcpy
from arcpy import env



# Set the workspace for the folder containing MXDs
arcpy.env.workspace = r"C:\Test2"
mxdList = arcpy.ListFiles("*.mxd")

# Iterate through all MXDs in the workspace
for mxd in mxdList:
    mxdpath = env.workspace + "\\" + mxd
    print (mxd + "Is being processed")
    
    mxdNext = arcpy.mapping.MapDocument(mxdpath)



    for lyr in arcpy.mapping.ListLayers(mxdNext, "old_shapefile", df):
        if lyr.supports("DATASOURCE"):
            if lyr.dataSource == r"C:\Test\Old_Shapefile.shp":
              lyr.replaceDataSource(r"C:\Test\Old_Shapefile.shp", "SHAPEFILE_WORKSPACE",
              r"C:\Production_Data\Test\Final Data.gbd","FILEGDB_WORKSPACE", "New_Name")
             lyr.name == "New_Name":
        print("lyr.name")      

  
    mxdNext.save()
    arcpy.RefreshTOC()
    del mxd

 

Tags (2)
0 Kudos
3 Replies
AlfredBaldenweck
MVP Regular Contributor

On line 23, you have 

 

r"C:\Production_Data\Test\Final Data.gbd"

 

If you change it to 

 

r"C:\Production_Data\Test\Final Data.gdb"

 

 does that fix the issue?

 

If not, please also let us know the line that the error is occurring on.

 

Also, actually more to the point:

Add an "if" to the beginning of line 24 and indent line 25 twice.

0 Kudos
kaww02
by
Occasional Contributor

Thank you @AlfredBaldenweck  I made those changes but still the same error.

The error seems to reference line 21

 

0 Kudos
TonyAlmeida
Frequent Contributor

Your indentation looks off, and I added iteration over data frames.

 

# Assuming a DataFrame is defined or selected in the map document
    for df in arcpy.mapping.ListDataFrames(mxdNext):
        for lyr in arcpy.mapping.ListLayers(mxdNext, "old_shapefile", df):
            if lyr.supports("DATASOURCE"):
                if lyr.dataSource == r"C:\Test\Old_Shapefile.shp":
                    lyr.replaceDataSource(r"C:\Production_Data\Test\Final Data.gbd", "FILEGDB_WORKSPACE", "New_Name")
                    lyr.name = "New_Name"
                    print(lyr.name)