Task scheduler with batch file running python script

3865
12
Jump to solution
10-14-2021 07:55 AM
JamesStout101
New Contributor II

Have created a script and a batch file to run the process automatically.

Pywin opens the script does not" 

Any help in getting it to open and execute would be appreciated.

 

Batch file as follows

C:\Python27\ArcGIS10.6\Lib\site-packages\pythonwin\Pythonwin.exe
C:\Users\jstout\Desktop\PY\MorningProcess\morningprocess.py
Timeout 15

 

Script as follows

import arcpy, arceditor, time, smtplib

# Set the workspace
arcpy.env.workspace = r'C:\Users\jstout\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\EED_FM@SDE.sde'

# Set a variable for the workspace
adminConn = arcpy.env.workspace


database = "Database Connections/toolboxDEFAULTVersion.sde"
versions = arcpy.ListVersions(adminConn)

# Print the versions available to the user
for version in versions:
print(version)
print "Version list complete."

# Execute the ReconcileVersions tool.
# Use a list comprehension to get a list of version names where the owner
# is the current user and make sure sde.default is not selected.
print 'Reconcile versions started.'

verList = [ver.name for ver in arcpy.da.ListVersions() if ver.isOwner
== True and ver.name.lower() != 'sde.EFAULT']

arcpy.ReconcileVersions_management(adminConn, "ALL_VERSIONS", "SDE.DEFAULT", "", "LOCK_ACQUIRED", "NO_ABORT", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST","DELETE_VERSION")

# Write messages to a Text File
txtFile = open("c:\\temp\\GPMessages.txt","w")
txtFile.write (arcpy.GetMessages())
txtFile.close()

print ('see error log at "c:\temp\GPMessages.txt"')
print'Reconciling Complete'

# Run the compress tool.
print("Running compress")
arcpy.Compress_management(adminConn)

# Use a connection file to create the connection
print "Check state ID"
egdb = r'C:\Users\jstout\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\EED_FM@SDE.sde'
egdb_conn = arcpy.ArcSDESQLExecute(egdb)

table_name = 'EED_FM.sde.sde_States'
field_name = 'state_id'

sql = '''
SELECT {0}, COUNT({0}) AS f_count FROM {1}
GROUP BY {0}
ORDER BY f_count DESC
'''.format(field_name, table_name)

egdb_return = egdb_conn.execute(sql)
for i in egdb_return:
print('{}: {}'.format(*i))
print "State count complete"

# Update statistics and indexes for the system tables
# Note: to use the "SYSTEM" option the user must be an geodatabase or database administrator.
# Rebuild indexes on the system tables
print("Rebuilding indexes on the system tables")
arcpy.RebuildIndexes_management(adminConn, "SYSTEM")

# Update statistics on the system tables
print("Updating statistics on the system tables")
arcpy.AnalyzeDatasets_management(adminConn, "SYSTEM")

print("Finished.")

0 Kudos
12 Replies
JamesStout101
New Contributor II

Using 10.6 have none of these lines.

0 Kudos
DominicRoberge2
Occasional Contributor III

you still should have C:\Python27\ArcGIS10.6\Lib\site-packages

then open the Desktop10.6.pth with notepad

I am using 10.8 and got this

C:\Python27\ArcGIS10.8\Lib\site-packages\Desktop10.8.pth

DominicRoberge2_0-1634235488713.png

 

0 Kudos
JoeBorgione
MVP Emeritus

I've never been able to get a scheduled task to execute properly from a desk top computer; only a server.  These other responses sound like great suggestions.

That should just about do it....
0 Kudos