I am trying to take a .csv file from a server location rename it to have the same name of the folder it came from and paste it into a different folder. I am able to use the script console and it moves but does not change the name. if i try to use the gateway schedule it says the path is invalid. I did change the logged on used to myself and i do have permissions on the server hosting ignition to be able to access the drives.
I have included the code that works in the script console below.
import os
import shutil
import glob
def copy_newest_file(local_path, local_destination, num_folders=20):
folder_list = [f for f in os.listdir(network_path) if os.path.isdir(os.path.join(network_path, f))]
folder_list = sorted(
[(os.path.join(network_path, folder), os.path.getmtime(os.path.join(network_path, folder))) for folder in folder_list[:num_folders]],
key=lambda x: x[1],
reverse=True
)
if len(folder_list) < 2:
return
second_newest_folder = folder_list[1][0]
file_to_copy = glob.glob(os.path.join(second_newest_folder, 'PieceLog.CSV'))
if file_to_copy:
destination_file = os.path.join(local_destination, os.path.basename(second_newest_folder) + '.CSV')
try:
shutil.copy2(file_to_copy[0], destination_file)
except Exception as e:
pass
network_path = r"P:\folder1"
local_destination = r"P:\folder2"
copy_newest_file(network_path, local_destination)
Even better is to not map a drive letter at all, but use UNC paths in your script. Then adjust the credentials the Ignition service uses so that it has access to your target locations.
This has the major advantage that it self-recovers if your network target goes off the air. And doesn't block gateway startup if that target is off the air when the gateway restarts.
I am running it through the gateway scheduled scripts. no errors in the logs. It acts like its running but doesn't seem to actually move anything. I may end up trying the java route, Im not super well versed in it but good a time as any to dig in more.