Hyperlink Errors

2106
5
07-13-2016 11:57 AM
ElizabethMinnick
New Contributor

I'm looking for a way to find all errors in a table with hyperlinks, such as wrong paths or missing photos, without having to click on each individual point to make sure the hyperlink functions.

0 Kudos
5 Replies
ChrisSmith7
Frequent Contributor

Elizabeth,

I have a PowerShell script you can use:

#csv path; input must be in this schema: ID,URL,NOTES - format your input this way, or adjust script
$path = "C:\Users\YOU\Desktop\YOUR_CSV.csv" #in csv (you create)
$outPath = "C:\Users\YOU\Desktop\YOUR_OUT_CSV.csv" #out csv (don't create this... let script create for you)
$csv = Import-Csv -path $path
$OutArray = @()

function processURL($inURL){
    # Create request
    $HTTP_Request = [System.Net.WebRequest]::Create($inURL)

    # Get response
    $HTTP_Response = $HTTP_Request.GetResponse()

    # Get response integer code
    $HTTP_Status = [int]$HTTP_Response.StatusCode

    If ($HTTP_Status -eq 200) {
        $ret_val = "OK!"
    }
    Else {
        $ret_val = "CHECK ME!"
    }

    # Clean-up and return
    $HTTP_Response.Close()
    return $ret_val
}

function request($ID, $URL, $NOTES){
    $localResponse = processURL $URL

    export $ID $URL $NOTES $localResponse
}

function export($eID, $eURL, $eNotes, $eResponse){

    $myobj = "" | Select "ID","URL","NOTES", "RESPONSE"
    $myobj.ID = $eID
    $myobj.URL = $eURL
    $myobj.NOTES = $NOTES
    $myobj.RESPONSE = $eResponse

    $OutArray += $myobj
    $myobj = $null 

    #PS4 version:
    $OutArray | export-csv $outPath -Append
    #PS2 version:
    #$OutArray | ConvertTo-Csv -NoTypeInformation | select -Skip 1 | Out-File -Append $outPath
}

foreach($line in $csv)
{
    $properties = $line | Get-Member -MemberType Properties
    for($i=0; $i -lt $properties.Count;$i++)
    {
        $column = $properties[$i]
        $columnvalue = $line | Select -ExpandProperty $column.Name

        switch ($column.Name)
            {
                "ID" {$ID = $columnvalue}
                "URL" {$URL = $columnvalue}
                "NOTES" {$NOTES = $columnvalue}
            }
    }
    if (-Not [string]::IsNullOrEmpty($ID) -and -Not [string]::IsNullOrEmpty($URL)) { request $ID $URL $NOTES }
}

So, in my test, I used a csv like this:

ID,URL,NOTES

1,http://www.google.com, this is google

2,http://www.esri.com, this is esri

3,http://www.microsoft.com, this is microsoft

...and I got an output like this:

#TYPE Selected.System.String

"ID","URL","NOTES","RESPONSE"

"1","http://www.google.com","this is google","OK!"

"2","http://www.esri.com","this is esri","OK!"

"3","http://www.microsoft.com","this is microsoft","OK!"

0 Kudos
ChrisSmith7
Frequent Contributor

If these aren't limited to web hyperlinks, let me know and I'll update the script!

0 Kudos
ElizabethMinnick
New Contributor

Thanks! They are just links to a folder containing photos. I'm looking for mistakes in the path, or paths that point to a photo that didn't get copied into the folder. Can I run the script without ArcGIS Pro?

Elizabeth Minnick

GIS Technician

Town of Abingdon

133 W. Main Street

Abingdon, VA 24210

(276) 492-2129

0 Kudos
ChrisSmith7
Frequent Contributor

Elizabeth,

I wasn't exactly sure of your environment/set-up, so I just wrote a PowerShell script, which can be executed outside of ArcGIS. It should be installed by default on Windows 7+. The script I wrote uses PowerShell 4, but the only change for PowerShell 2 should be the commented out line exporting to CSV, I believe.

You would basically need to export your table as a CSV, open notepad, paste the script and make any needed enviro changes, save as a .ps1, then run the script (here's an example of how to do that - windows - How to run a PowerShell script? - Stack Overflow).

Wes has a Python script you can just run in ArcMap that does exactly what you want, that is, check if the path on disk doesn't exist. I would need to adjust the PowerShell script to check for paths since I thought you were looking for web hyperlinks.

WesMiller
Regular Contributor III

The script below will produce an oid list of paths that don't exist.

import arcpy


fc = r"your\feature\class"
fld = "yourPathField"


desc = arcpy.Describe(fc)
oidName = desc.oidFieldName


oidlist = []
flds = [oidName,fld]
with arcpy.da.SearchCursor(fc,flds) as rows:
    for row in rows:
        if arcpy.Exists(row[1]):
            pass
        else:
            oidlist.append(row[0])
print oidlist