Moving files on a network using scripting

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)

You have to set up mapped drives in ignition.conf in order for the Gateway to be able to access them: Mapping a Network Drive | Ignition User Manual

1 Like

We do have those set up as well.

Pretty sure the location should be the UNS share name/path, not some kind of recursive reference to the driver letter you've assigned to it.

So use the Servername/folder?

If the file server requires a username and password, you need to include that as well. Even if it's already mapped in Windows.

I did not include that in the SS but that is right below the add shared drive lines

1 Like

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.

2 Likes

Got it, i changed them and stop then started the service.

this is the error i get in the webgui log

Caused by: org.python.core.PyException: OSError: [Errno 20] Not a directory: 'servername\Public\Nuking_Eddy_Test_files\HT2'

In your script, did you double up all of your backslashes in your string constants? (Stupid Windows folder separator.)

i did not. it does that in the errors for some reason

def onScheduledEvent():
	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"servername\Public\Nuking_Eddy_Test_files\HT2"
	local_destination = r"servername\Public\NUKING Quality\EDDY Test Results & REJECT LOG\HT2"
	
	copy_newest_file(network_path, local_destination)

So, a proper UNC path has double backslashes before the server name. (No need to additionally double backslashes when using the r'' syntax.)

2 Likes

Trying that now

After adding the two slashes i no longer get an error but the files do not move.

Look for more errors in the logs if running from gateway scope. FWIW, I would always use java's File class in place of Jython's stdlib.

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.