1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

ArcPy Script to update LYR data source not identifying all layers in folder?

Discussion in 'Programming/Internet' started by cofan, Oct 8, 2018.

  1. cofan

    cofan Guest

    I've put together a script that for the most part works as intended - to walk through a series of folders, find SDE layer files and update their data source to direct connects if the layers support workspacepath. However I've noticed that in some cases it is not finding specific layer files within folders. Here is the script:

    import arcpy
    import os
    from arcpy import env

    env.overwriteOutput = True
    TheLayerDir = r"G:\GIS\Layers"
    NewDataPath = r"G:\GIS\Data\vector_direct.sde"

    for dirname, dirnames, filenames in os.walk(TheLayerDir):

    for filename in filenames:
    newFilename = os.path.join(dirname, filename)
    desc = arcpy.Describe(os.path.join(dirname, filename))

    if newFilename[-4:] == '.lyr':
    lyrFile = arcpy.mapping.Layer(newFilename)

    for lyr in arcpy.mapping.ListLayers(lyrFile):
    if not lyr.isGroupLayer:

    if lyr.supports("WORKSPACEPATH") == True:

    if "sde" in desc.dataElement.catalogPath and lyr.workspacePath != NewDataPath:
    print "------------------------------"
    print "Layer: " + lyr.name
    print "Old Data Source: " + lyr.workspacePath
    lyr.replaceDataSource(NewDataPath,"SDE_WORKSPACE")
    print "New Data Source: " + lyr.workspacePath
    lyrFile.save()

    elif "sde" in desc.dataElement.catalogPath and lyr.workspacePath == NewDataPath:
    print "------------------------------"
    print "Layer: " + lyr.name + " has already been updated with path, skipping..."

    else:
    print "------------------------------"
    print "Layer: " + lyr.name + " is not a SDE Layer"
    else:

    if "sde" in desc.dataElement.catalogPath:
    print "------------------------------"
    print "Layer: " + lyr.name
    print "Layer does not support workspacePath"

    print "Script Complete! - That's all Folks!"


    If I for example substitude the following code as follows:

    if newFilename[-4:] == '.lyr':


    with

    if extension.lower() == ".lyr":


    then the script will successfully identify all layer files within a folder, however without declaring 'newFilename'.

    Login To add answer/comment
     

Share This Page