Script for copying files from FTP to USB
Posted: Thu Jan 23, 2020 6:28 am
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);
}
}
}
}
}
""
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);
}
}
}
}
}
""