Good. Sorry I've posted PowerShell code by mistake, forgetting that you use C#.
- martin
foreach (RemoteFileInfo fileInfo in fileInfos)
{
string destFileName = fileInfo.FullName.Insert(1, "COPY_");
string localFilePath =
RemotePath.TranslateRemotePathToLocal(
destFileName, remotePath, localPath);
...
}
$subfolders =
$session.EnumerateRemoteFiles(
$remotePath, $Null, [WinSCP.EnumerationOptions]::EnumerateDirectories)
foreach ($subfolder in $subfolders)
{
$dest = (Join-Path $localPath ("COPY_" + $subfolder.Name) "*")
$session.GetFiles($subfolder.FullName + "/*", $dest).Check()
}
COPY_folder\COPY_subfolder\COPY_subsubfolder
?
'COPY _' + folderName
)
This code lists local files and directories to upload on FTP and i want to do the opposite.
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Enumerate files and directories to DOWNload
IEnumerable<FileSystemInfo> fileInfos =
new DirectoryInfo(remotePath).EnumerateFileSystemInfos(
"*", SearchOption.AllDirectories);
foreach (FileSystemInfo fileInfo in fileInfos)
{
string localFilePath =
RemotePath.TranslateRemotePathToLocal(
fileInfo.FullName, remotePath, localPath);
if (fileInfo.Attributes.HasFlag(FileAttributes.Directory))
{
// Create remote subdirectory, if it does not exist yet
if (!session.FileExists(localFilePath))
{
session.CreateDirectory(localFilePath);
}
}
else
{
Console.WriteLine("Moving file {0}...", fileInfo.FullName);
// DOWNload file and remove original
session.GetFiles(fileInfo.FullName, localFilePath, true).Check();
}
}
}
I tried this code, it works for upload but for the download it becomes complicated: reverse paths does not work (that's my goal).
Am I to understand that the 2 other tracks evoked will not succeed? (1: filemask / 2: error handling).
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Tasks.ScriptTask;
using System.Windows.Forms;
using WinSCP;
namespace ST_0211b76178e849199217ecc2d4ccaa07
{
[SSISScriptTaskEntryPoint]
public partial class ScriptMain : VSTARTScriptObjectModelBase
{
public void Main()
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
FtpSecure = FtpSecure.Explicit,
HostName = Dts.Variables["$Project::PROP_FTP_HostName"].Value.ToString(),
UserName = Dts.Variables["$Project::PROP_FTP_UserName"].Value.ToString(),
Password = Dts.Variables["$Project::PROP_FTP_Password"].Value.ToString(),
TlsHostCertificateFingerprint = Dts.Variables["$Project::PROP_FTP_Fingerprint"].Value.ToString()
};
try
{
using (Session session = new Session())
{
// As WinSCP .NET assembly has to be stored in GAC to be used with SSIS,
// you need to set path to WinSCP.exe explicitly, if using non-default location.
session.ExecutablePath = @Dts.Variables["$Project::PROP_CheminWinSCP"].Value.ToString();
//session.QueryReceived += (sender, e) =>
//{
// Console.WriteLine("Error: {0}", e);
// e.Continue();
//};
// Connect
session.Open(sessionOptions);
// Download files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
//transferOptions.FileMask = "*.zip";
TransferOperationResult transferResult =
session.GetFiles(
"/*", @Dts.Variables["$Project::PROP_CheminFichiers"].Value.ToString(), true, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
bool fireAgain = false;
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Dts.Events.FireInformation(0, null,
string.Format("Upload of {0} succeeded", transfer.FileName),
null, 0, ref fireAgain);
}
}
Dts.TaskResult = (int)DTSExecResult.Success;
}
catch (Exception e)
{
Dts.Events.FireError(0, null,
string.Format("Error when using WinSCP to upload files: {0}", e),
null, 0);
Dts.TaskResult = (int)DTSExecResult.Failure;
}
}
}
}
session.GetFiles
with transferOptions.FileMask
but I can not get what I want with it.
// Download files
TransferOptions TransferOptions = new TransferOptions();
TransferOptions.TransferMode = TransferMode.Binary;
TransferOptions.FileMask = "*.zip";
TransferOperationResult transferResult = session.GetFiles(
"/*", @Dts.Variables["$Project::PROP_FilePath"].Value.ToString(), true, TransferOptions);
session.QueryReceived
to override errors (folders are protected and can not be deleted).