Script for copying files from FTP to USB

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
Cluel3ss
Posts: 1
Joined: Wed Jan 22, 2020 7:52 am

Script for copying files from FTP to USB

Post by Cluel3ss »

I'm using a x2 Extreme HP SC where i save log files to the FTP area of the panel.
I also want to be able to copy these log files to a USB-stick connected to the panel, using a script.

I am not familiar with C# coding and have had limited success in this, but have found an example program that works well in Visual Studio, but not in the IX Developer software.

I would appreciate any help, and hope it will be useful for others too!

""
using System;
using System.IO;

namespace DirectoryCopyToUSB
{
class DirectoryCopyToUSB
{
public static void Copy()
{
// Copy from the current directory, include subdirectories.
DirectoryCopy(@"Project Files", @"Hard Disk\LogFiles", true);
}

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);

if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}

DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}

// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}

// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
}
""

User avatar
Russ C.
Posts: 213
Joined: Thu Nov 16, 2017 3:32 pm
Contact:

Re: Script for copying files from FTP to USB

Post by Russ C. »

One reason it might not be working in iX Developer is, iX is restricted to the 3.5 Compact Framework, whereas VS has access to all frameworks.

Here is a snippet I've used in the past in iX that worked, but it looks pretty similar to your's.

Code: Select all

using System.IO;
using System.Text;

void Button_Click(System.Object sender, System.EventArgs e)
{
	DirectoryCopy("<SourceName>", "<DestinationName>", true);
}

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
	DirectoryInfo dir = new DirectoryInfo(sourceDirName);
	DirectoryInfo[] dirs = dir.GetDirectories();

	// If the source directory does not exist, throw an exception. 
	if (!dir.Exists)
	{
		throw new DirectoryNotFoundException(
			"Source directory does not exist or could not be found: "
			+ sourceDirName);
	}

	// If the destination directory exists, delete it. 				
	if (Directory.Exists(destDirName))
	{
		Directory.Delete(destDirName, true);
	}
		
	Directory.CreateDirectory(destDirName);


	// Get the file contents of the directory to copy.
	FileInfo[] files = dir.GetFiles();

	foreach (FileInfo file in files)
	{
		try
		{
			// Create the path to the new copy of the file. 
			string temppath = Path.Combine(destDirName, file.Name);
			file.CopyTo(temppath, false);
		}
		catch (Exception) { }
	}

	// If copySubDirs is true, copy the subdirectories. 
	if (copySubDirs)
	{

		foreach (DirectoryInfo subdir in dirs)
		{
			// Create the subdirectory. 
			string temppath = Path.Combine(destDirName, subdir.Name);

			// Copy the subdirectories.
			DirectoryCopy(subdir.FullName, temppath, copySubDirs);
		}
	}
}
Best regards,

Russ
(801) 708-6690
Technical Support
Contact Us

Beijer Electronics AB
http://www.beijerelectronics.us

Post Reply