1 # ---------------------------------------------------------------------------------------------------------------------- 2 # santa_cruz_sanitation.py 3 # version #2 -- tracks duplicate file references. 4 # Christopher Prendergast 5 # 2024/05/04 6 # ---------------------------------------------------------------------------------------------------------------------- 7 import arcpy 8 from pathlib import Path 9 from shutil import copytree 10 11 # Set project directory, geodatabase, feature class, field, and old/new pdf subdirectory names. 12 my_proj_dir = r"C:\ArcGIS_local_projects\SantaCruzSanitation" 13 my_gdb = "SanitationData.gdb" 14 my_fc = "SanitationMapIndex" 15 my_field = "FileName" 16 my_old_pdf_subdir = "SanitationMaps" 17 my_new_pdf_subdir = "NewSanitationMaps" 18 my_save_pdf_subdir = "SanitationMapsSave" 19 line = "-" * 80 20 21 # Establish the old directory and check it exists. 22 old_dir = Path(my_proj_dir, my_old_pdf_subdir) 23 assert old_dir.is_dir(), "old_dir: {0} is not a directory or does not exist.".format(old_dir) 24 print("...old_dir", old_dir) 25 26 # Establish the save directory and create it if it doesn't already exist. 27 # Make a backup copy just in case! 28 save_dir = Path(my_proj_dir, my_save_pdf_subdir) 29 if not save_dir.is_dir(): 30 copytree(old_dir, save_dir) 31 assert save_dir.is_dir(), "save_dir: {0} is not a directory or does not exist.".format(save_dir) 32 print("...save_dir:", save_dir) 33 34 # Establish the new directory and create it if it doesn't already exist. 35 new_dir = Path(my_proj_dir, my_new_pdf_subdir) 36 new_dir.mkdir(parents=True, exist_ok=True) 37 assert new_dir.is_dir(), "new_dir: {0} is not a directory or does not exist.".format(new_dir) 38 print("...new_dir:", new_dir) 39 40 # Establish the geodatabase and check it exists. 41 sanitation_gdb = Path(my_proj_dir, my_gdb) 42 assert sanitation_gdb.is_dir(), "sanitation_gdb: {0} is not a directory or does not exist.".format(sanitation_gdb) 43 print("...sanitation_gdb", sanitation_gdb) 44 45 # Establish the feature class. 46 fc = Path(sanitation_gdb, my_fc) 47 print("...fc:", fc) 48 49 # Check that the feature class exists. 50 arcpy.env.workspace = str(sanitation_gdb) 51 assert arcpy.Exists(str(fc)), "fc: feature class {0} does not exists".format(fc) 52 53 # Check that field exists. 54 fields = arcpy.ListFields(str(fc), my_field) 55 assert len(fields) == 1 and fields[0].name == my_field, "my_fields: field {0} not found".format(my_field) 56 print("...my_field:", my_field) 57 print(line) 58 59 # Establish empty lists to hold any files that are not found in the old directory or are duplicate references. 60 files_not_found = [] 61 duplicate_file_refs = [] 62 63 # Establish a search cursor. 64 cursor = arcpy.da.SearchCursor(str(fc), ["OID@", my_field]) 65 # Iterate through records in cursor. 66 for row in cursor: 67 # Get the file name from the current record. 68 curr_id, curr_file = row 69 old_pdf_file = Path(old_dir, curr_file) 70 new_pdf_file = Path(new_dir, curr_file) 71 72 if old_pdf_file.is_file() and not new_pdf_file.is_file(): 73 # If the file exists then move it to the new directory. 74 old_pdf_file.rename(new_pdf_file) 75 print("-->", "{0}: File {1} moved to {2}".format(curr_id, curr_file, new_pdf_file)) 76 elif new_pdf_file.is_file(): 77 # If thee file already exists in the target directory then add it to a list of duplicate references. 78 print("***", "{0}: Duplicate reference. File {1} already exists in {2}".format(curr_id, curr_file, new_dir)) 79 duplicate_file_refs.append(curr_file) 80 else: 81 # Otherwise, if the file is not found, add it to the list of missing files. 82 print("***", "{0}: file {1} not found.".format(curr_id, old_pdf_file)) 83 files_not_found.append(old_pdf_file) 84 85 # Print list files in the save directory. 86 print() 87 print(line) 88 print("Files in the save directory {0}".format(save_dir)) 89 print(line) 90 n = 0 91 for f in save_dir.iterdir(): 92 print("\t{0}".format(f.name)) 93 n += 1 94 print("Total number of files in the save directory: {0}".format(n)) 95 96 # Print list of file references where the file was not found in the original directory. 97 print() 98 print(line) 99 print("Files not found in original directory {0}".format(old_dir)) 100 print(line) 101 n = 0 102 for f in files_not_found: 103 print("\t{0}".format(f.name)) 104 n += 1 105 print("Total number of file references not found in original directory: {0}".format(n)) 106 107 # Print list of duplicate file references. 108 print() 109 print(line) 110 print("Duplicate file reference where the same file has already been moved.") 111 print(line) 112 n = 0 113 for f in duplicate_file_refs: 114 print("\t{0}".format(f)) 115 n += 1 116 print("Total number of duplicate file references: {0}".format(n)) 117 118 # Print list of files that remain in the original directory. 119 print() 120 print(line) 121 print("Files remaining in the original directory after move {0}".format(old_dir)) 122 print(line) 123 n = 0 124 for f in old_dir.iterdir(): 125 print("\t{0}".format(f.name)) 126 n += 1 127 print("Total number of files remaining in the original directory after move: {0}".format(n)) 128 129 # Print list of files found in the new directory. 130 print() 131 print(line) 132 print("Files found in new directory after move {0}".format(new_dir)) 133 print(line) 134 n = 0 135 for f in new_dir.iterdir(): 136 print("\t{0}".format(f.name)) 137 n += 1 138 print("Total number of files found in new directory after move: {0}".format(n)) 139 140 141 # ---------------------------------------------------------------------------------------------------------------------- 142 # Output 143 # ---------------------------------------------------------------------------------------------------------------------- 144 # "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" C:\ArcGIS_local_projects\SantaCruzSanitation\santa_cruz_sanitation.py 145 # ...old_dir C:\ArcGIS_local_projects\SantaCruzSanitation\SanitationMaps 146 # ...save_dir: C:\ArcGIS_local_projects\SantaCruzSanitation\SanitationMapsSave 147 # ...new_dir: C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps 148 # ...sanitation_gdb C:\ArcGIS_local_projects\SantaCruzSanitation\SanitationData.gdb 149 # ...fc: C:\ArcGIS_local_projects\SantaCruzSanitation\SanitationData.gdb\SanitationMapIndex 150 # ...my_field: FileName 151 # -------------------------------------------------------------------------------- 152 # --> 1: File A-082_COMPLETE.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-082_COMPLETE.pdf 153 # --> 2: File A-200.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-200.pdf 154 # --> 4: File A-018a.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-018a.pdf 155 # --> 7: File C-055.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-055.pdf 156 # --> 9: File A-216.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-216.pdf 157 # --> 11: File A-030.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-030.pdf 158 # *** 12: Duplicate reference. File A-082_COMPLETE.pdf already exists in C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps 159 # --> 14: File A-077.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-077.pdf 160 # --> 15: File C-128.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-128.pdf 161 # --> 16: File A-021.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-021.pdf 162 # --> 18: File A-031.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-031.pdf 163 # --> 19: File A-027.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-027.pdf 164 # --> 21: File A-018.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-018.pdf 165 # --> 22: File C-125.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-125.pdf 166 # --> 23: File A-191.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-191.pdf 167 # --> 25: File A-029.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-029.pdf 168 # *** 26: Duplicate reference. File A-082_COMPLETE.pdf already exists in C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps 169 # --> 28: File C-120.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-120.pdf 170 # --> 29: File C-021.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-021.pdf 171 # *** 31: Duplicate reference. File A-082_COMPLETE.pdf already exists in C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps 172 # --> 32: File C-116.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-116.pdf 173 # --> 34: File C-008.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-008.pdf 174 # --> 35: File C-109.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-109.pdf 175 # --> 36: File A-020.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-020.pdf 176 # --> 37: File A-149.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-149.pdf 177 # --> 38: File A-022.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-022.pdf 178 # --> 39: File A-196.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-196.pdf 179 # --> 40: File A-019.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-019.pdf 180 # --> 42: File C-129.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-129.pdf 181 # --> 43: File A-017.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-017.pdf 182 # --> 44: File A-032.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\A-032.pdf 183 # --> 45: File C-123.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-123.pdf 184 # --> 46: File C-050.pdf moved to C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps\C-050.pdf 185 # 186 # -------------------------------------------------------------------------------- 187 # Files in the save directory C:\ArcGIS_local_projects\SantaCruzSanitation\SanitationMapsSave 188 # -------------------------------------------------------------------------------- 189 # A-017.pdf 190 # A-018.pdf 191 # A-018a.pdf 192 # A-019.pdf 193 # A-020.pdf 194 # A-021.pdf 195 # A-022.pdf 196 # A-024.pdf 197 # A-025.pdf 198 # A-026.pdf 199 # A-027.pdf 200 # A-029.pdf 201 # A-030.pdf 202 # A-031.pdf 203 # A-032.pdf 204 # A-034.pdf 205 # A-036.pdf 206 # A-037.pdf 207 # A-039.pdf 208 # A-040..pdf 209 # A-043.pdf 210 # A-044.pdf 211 # A-048.pdf 212 # A-050.pdf 213 # A-051.pdf 214 # A-053.pdf 215 # A-054.pdf 216 # A-056.pdf 217 # A-059.pdf 218 # A-061.pdf 219 # A-062.pdf 220 # A-063.pdf 221 # A-064.pdf 222 # A-065.pdf 223 # A-067.pdf 224 # A-068.pdf 225 # A-069.pdf 226 # A-070.pdf 227 # A-072.pdf 228 # A-073.pdf 229 # A-074.pdf 230 # A-075.pdf 231 # A-076.pdf 232 # A-077.pdf 233 # A-078.pdf 234 # A-082_COMPLETE.pdf 235 # A-149.pdf 236 # A-150.pdf 237 # A-151.pdf 238 # A-186.pdf 239 # A-187.pdf 240 # A-188.pdf 241 # A-189.pdf 242 # A-190.pdf 243 # A-191.pdf 244 # A-194.pdf 245 # A-195.pdf 246 # A-196.pdf 247 # A-197.pdf 248 # A-198.pdf 249 # A-200.pdf 250 # A-202.pdf 251 # A-209.pdf 252 # A-210.pdf 253 # A-211.pdf 254 # A-212.pdf 255 # A-212B.pdf 256 # A-213.pdf 257 # A-213B.pdf 258 # A-214.pdf 259 # A-215.pdf 260 # A-216.pdf 261 # A-217.pdf 262 # A-218.pdf 263 # A-219.pdf 264 # A-220.pdf 265 # A-78.pdf 266 # C-001A.pdf 267 # C-008.pdf 268 # C-021.pdf 269 # C-050.pdf 270 # C-055.pdf 271 # C-108A.pdf 272 # C-109.pdf 273 # C-110.pdf 274 # C-110A.pdf 275 # C-111.pdf 276 # C-112.pdf 277 # C-113.pdf 278 # C-114.pdf 279 # C-115.pdf 280 # C-116.pdf 281 # C-117.pdf 282 # C-118.pdf 283 # C-119.pdf 284 # C-120.pdf 285 # C-121.pdf 286 # C-122.pdf 287 # C-123.pdf 288 # C-124.pdf 289 # C-125.pdf 290 # C-126.pdf 291 # C-127.pdf 292 # C-128.pdf 293 # C-129.pdf 294 # C-130.pdf 295 # Total number of files in the save directory: 106 296 # 297 # -------------------------------------------------------------------------------- 298 # Files not found in original directory C:\ArcGIS_local_projects\SantaCruzSanitation\SanitationMaps 299 # -------------------------------------------------------------------------------- 300 # Total number of file references not found in original directory: 0 301 # 302 # -------------------------------------------------------------------------------- 303 # Duplicate file reference where the same file has already been moved. 304 # -------------------------------------------------------------------------------- 305 # A-082_COMPLETE.pdf 306 # A-082_COMPLETE.pdf 307 # A-082_COMPLETE.pdf 308 # Total number of duplicate file references: 3 309 # 310 # -------------------------------------------------------------------------------- 311 # Files remaining in the original directory after move C:\ArcGIS_local_projects\SantaCruzSanitation\SanitationMaps 312 # -------------------------------------------------------------------------------- 313 # A-024.pdf 314 # A-025.pdf 315 # A-026.pdf 316 # A-034.pdf 317 # A-036.pdf 318 # A-037.pdf 319 # A-039.pdf 320 # A-040..pdf 321 # A-043.pdf 322 # A-044.pdf 323 # A-048.pdf 324 # A-050.pdf 325 # A-051.pdf 326 # A-053.pdf 327 # A-054.pdf 328 # A-056.pdf 329 # A-059.pdf 330 # A-061.pdf 331 # A-062.pdf 332 # A-063.pdf 333 # A-064.pdf 334 # A-065.pdf 335 # A-067.pdf 336 # A-068.pdf 337 # A-069.pdf 338 # A-070.pdf 339 # A-072.pdf 340 # A-073.pdf 341 # A-074.pdf 342 # A-075.pdf 343 # A-076.pdf 344 # A-078.pdf 345 # A-150.pdf 346 # A-151.pdf 347 # A-186.pdf 348 # A-187.pdf 349 # A-188.pdf 350 # A-189.pdf 351 # A-190.pdf 352 # A-194.pdf 353 # A-195.pdf 354 # A-197.pdf 355 # A-198.pdf 356 # A-202.pdf 357 # A-209.pdf 358 # A-210.pdf 359 # A-211.pdf 360 # A-212.pdf 361 # A-212B.pdf 362 # A-213.pdf 363 # A-213B.pdf 364 # A-214.pdf 365 # A-215.pdf 366 # A-217.pdf 367 # A-218.pdf 368 # A-219.pdf 369 # A-220.pdf 370 # A-78.pdf 371 # C-001A.pdf 372 # C-108A.pdf 373 # C-110.pdf 374 # C-110A.pdf 375 # C-111.pdf 376 # C-112.pdf 377 # C-113.pdf 378 # C-114.pdf 379 # C-115.pdf 380 # C-117.pdf 381 # C-118.pdf 382 # C-119.pdf 383 # C-121.pdf 384 # C-122.pdf 385 # C-124.pdf 386 # C-126.pdf 387 # C-127.pdf 388 # C-130.pdf 389 # Total number of files remaining in the original directory after move: 76 390 # 391 # -------------------------------------------------------------------------------- 392 # Files found in new directory after move C:\ArcGIS_local_projects\SantaCruzSanitation\NewSanitationMaps 393 # -------------------------------------------------------------------------------- 394 # A-017.pdf 395 # A-018.pdf 396 # A-018a.pdf 397 # A-019.pdf 398 # A-020.pdf 399 # A-021.pdf 400 # A-022.pdf 401 # A-027.pdf 402 # A-029.pdf 403 # A-030.pdf 404 # A-031.pdf 405 # A-032.pdf 406 # A-077.pdf 407 # A-082_COMPLETE.pdf 408 # A-149.pdf 409 # A-191.pdf 410 # A-196.pdf 411 # A-200.pdf 412 # A-216.pdf 413 # C-008.pdf 414 # C-021.pdf 415 # C-050.pdf 416 # C-055.pdf 417 # C-109.pdf 418 # C-116.pdf 419 # C-120.pdf 420 # C-123.pdf 421 # C-125.pdf 422 # C-128.pdf 423 # C-129.pdf 424 # Total number of files found in new directory after move: 30 425 # 426 # Process finished with exit code 0 427 # ---------------------------------------------------------------------------------------------------------------------- 428