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()
}
Yes.
It's a simple tree, there are no subfolders.
Hello, I would like to go a step further in the process in place by adding the same prefix to all downloaded directories (example: 'COPY _' + folderName)
=> is it possible? and how?
thanks in advance.
COPY_folder\COPY_subfolder\COPY_subsubfolder
?
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 System.Windows.Forms;
using WinSCP;
namespace ST_0211b76178e849199217ecc2d4ccaa07
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.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();
// Connect
session.Open(sessionOptions);
// Download files
TransferOptions TransferOptions = new TransferOptions();
TransferOptions.TransferMode = TransferMode.Binary;
//TransferOptions.FileMask = "*.zip";
TransferOperationResult transferResult =
//session.QueryReceived += (sender, e) =>
//{
// Console.WriteLine("Error: {0}", e);
// e.Continue();
//};
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)DTSE*xecResult.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)DTS*ExecResult.Failure;
}
}
}
}
// 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);