# Moving files on a network using scripting

**URL:** <https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007>\
**Category:** Ignition\
**Created:** [January 16, 2025, 8:35pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007 "2025-01-16T20:35:10Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 8:35pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/1 "2025-01-16T20:35:10Z")

</div>

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.

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 16, 2025, 8:38pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/2 "2025-01-16T20:38:45Z")

</div>

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](https://docs.inductiveautomation.com/docs/8.1/tutorials/tutorials-and-helpful-tricks/mapping-a-network-drive)

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 8:43pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/3 "2025-01-16T20:43:48Z")

</div>

We do have those set up as well.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/d/7d260e9e5de6c63ae60c186e9d3f1e9b0b35a2d0.png)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 16, 2025, 8:48pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/4 "2025-01-16T20:48:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:00pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/5 "2025-01-16T21:00:29Z")

</div>

So use the Servername/folder?

---

<div class="post-metadata">

**Author:** ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://forum.inductiveautomation.com/u/Troy)\
**Post date:** [January 16, 2025, 9:04pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/6 "2025-01-16T21:04:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:05pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/7 "2025-01-16T21:05:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 16, 2025, 9:07pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/8 "2025-01-16T21:07:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:08pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/9 "2025-01-16T21:08:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:10pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/10 "2025-01-16T21:10:04Z")

</div>

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'

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 16, 2025, 9:12pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/11 "2025-01-16T21:12:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:12pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/12 "2025-01-16T21:12:56Z")

</div>

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

```python
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)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 16, 2025, 9:15pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/13 "2025-01-16T21:15:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:16pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/14 "2025-01-16T21:16:30Z")

</div>

Trying that now

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:18pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/15 "2025-01-16T21:18:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 16, 2025, 9:20pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/16 "2025-01-16T21:20:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mike\_Lamothe](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@Mike\_Lamothe](https://forum.inductiveautomation.com/u/Mike_Lamothe)\
**Post date:** [January 16, 2025, 9:26pm UTC](https://forum.inductiveautomation.com/t/moving-files-on-a-network-using-scripting/101007/17 "2025-01-16T21:26:43Z")

</div>

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.
