Move files and directories to another location on the same server
To move remote files and directories from one remote directory to another remote directory, use the Move methods. These methods require the path to the remote directory containing files and directories to move and the path of the destination remote directory. There are many advanced options such as moving files and directories recursively, custom search conditions, and custom file comparison.
Use the Move method to move remote files and directories to another location on the same server only. using System;
using ComponentPro;
using ComponentPro.Net;
using ComponentPro.IO;
...
static void Main()
{
// Create a new class instance.
Ftp client = new Ftp();
// Connect to the SFTP server.
client.Connect("myserver");
// Authenticate.
client.Authenticate("userName", "password");
// ...
// Move an entire directory with the standard rename command.
client.Rename("/my old folder", "/my new folder/new one");
TransferOptions opt = new TransferOptions(
true, // Build directory structure.
true, // Recursive.
OptionValue.Yes, // Create empty directories.
(SearchCondition)null, //
FileOverwriteMode.Overwrite,
SymlinksResolveAction.Skip);
opt.DeleteEmptyDirectories = true; // Remove empty directories.
// Move all .cs and .vb files to a new folder.
client.Move("/myfolder2/*.cs,*.vb", "/my new folder", opt);
// Move the selected files in '/my folder' to 'my new folder'.
string[] files = { "my file1.dat", "file2.txt", "my dir" };
client.Move("/my folder", files, "my new folder", opt);
// Handle file existence event
client.TransferConfirm += client_TransferConfirm;
opt.FileOverwriteMode = FileOverwriteMode.Confirm;
// Move all *.dat and *.txt files to 'my dest' folder.
// If a .txt file already exists, skip it; otherwise overwrite. See the event handler client_TransferConfirm.
client.Move("my source/*.dat,*.txt", "my dest", opt);
// With advanced search conditions.
opt.SearchCondition =
SearchCondition.SizeInRange(10, 10240) + // Search for files with size from 10 -> 10240 bytes
SearchCondition.ModifiedAfter(new DateTime(2010, 05, 01)); // and modifed after 2010/05/01.
// Move XLS files.
client.Move("my data/*.xls", "my dest", opt);
// Move files simultaneously using 3 threads.
client.Move("/my root/*.txt", "/new folder", 3, true);
// ...
// Disconnect.
client.Disconnect();
}
static void client_TransferConfirm(object sender, TransferConfirmEventArgs e)
{
if (e.ConfirmReason == TransferConfirmReason.FileAlreadyExists)
{
if (e.SourceFileInfo.Name.EndsWith(".txt"))
e.NextAction = TransferConfirmNextActions.Skip; // Skip 'txt' file if it already exists.
else
e.NextAction = TransferConfirmNextActions.Overwrite; // otherwise overwrite it.
}
}
Imports ComponentPro
Imports ComponentPro.Net
Imports ComponentPro.IO
...
Shared Sub Main()
' Create a new class instance.
Dim client As New Ftp()
' Connect to the SFTP server.
client.Connect("myserver")
' Authenticate.
client.Authenticate("userName", "password")
' ...
' Move an entire directory with the standard rename command.
client.Rename("/my old folder", "/my new folder/new one")
Dim opt As New TransferOptions(True, True, OptionValue.Yes, CType(Nothing, SearchCondition), FileOverwriteMode.Overwrite, SymlinksResolveAction.Skip) ' Create empty directories. - Recursive. - Build directory structure.
opt.DeleteEmptyDirectories = True ' Remove empty directories.
' Move all .cs and .vb files to a new folder.
client.Move("/myfolder2/*.cs,*.vb", "/my new folder", opt)
' Move the selected files in '/my folder' to 'my new folder'.
Dim files() As String = { "my file1.dat", "file2.txt", "my dir" }
client.Move("/my folder", files, "my new folder", opt)
' Handle file existence event
AddHandler client.TransferConfirm, AddressOf client_TransferConfirm
opt.FileOverwriteMode = FileOverwriteMode.Confirm
' Move all *.dat and *.txt files to 'my dest' folder.
' If a .txt file already exists, skip it; otherwise overwrite. See the event handler client_TransferConfirm.
client.Move("my source/*.dat,*.txt", "my dest", opt)
' With advanced search conditions.
opt.SearchCondition = SearchCondition.SizeInRange(10, 10240) + SearchCondition.ModifiedAfter(New Date(2010, 05, 01)) ' and modifed after 2010/05/01. - Search for files with size from 10 -> 10240 bytes
' Move XLS files.
client.Move("my data/*.xls", "my dest", opt)
' Move files simultaneously using 3 threads.
client.Move("/my root/*.txt", "/new folder", 3, True)
' ...
' Disconnect.
client.Disconnect()
End Sub
Private Shared Sub client_TransferConfirm(ByVal sender As Object, ByVal e As TransferConfirmEventArgs)
If e.ConfirmReason = TransferConfirmReason.FileAlreadyExists Then
If e.SourceFileInfo.Name.EndsWith(".txt") Then
e.NextAction = TransferConfirmNextActions.Skip ' Skip 'txt' file if it already exists.
Else
e.NextAction = TransferConfirmNextActions.Overwrite ' otherwise overwrite it.
End If
End If
End Sub
To understand more about wildcard masks and search criterion, see this topic.
Move files and directories to another file system or vice versa
If you want to move files from local disk or another file system to the server or vice versa, use the
Upload or
Download method with the
MoveFiles property of a
TransferOptions object set to
true. We take advantage of those methods with the following examples for the corresponding cases.
Move files and directories from a local disk to the remote server
using (Ftp client = new Ftp())
{
client.Connect("myserver");
client.Authenticate("userName", "password");
TransferOptions opt = new TransferOptions();
opt.MoveFiles = true;
// Move files from "c:\data" to "/data"
client.Upload(@"c:\data", "/data", opt);
// Move .dat files from "c:\data2" to "/data"
client.Upload(@"c:\data2\*.dat", "/data", opt);
}
Using client As New Ftp()
client.Connect("myserver")
client.Authenticate("userName", "password")
Dim opt As New TransferOptions()
opt.MoveFiles = True
' Move files from "c:\data" to "/data"
client.Upload("c:\data", "/data", opt)
' Move .dat files from "c:\data2" to "/data"
client.Upload("c:\data2\*.dat", "/data", opt)
End Using
Move remote files and directories to a local disk
using (Ftp client = new Ftp())
{
client.Connect("myserver");
client.Authenticate("userName", "password");
TransferOptions opt = new TransferOptions();
opt.MoveFiles = true;
// Move files from "/data" to "c:\data"
client.Download("/data", @"c:\data", opt);
// Move .dat files from "/data2" to "c:\data"
client.Upload(@"/data2/*.dat", @"c:\data", opt);
}
Using client As New Ftp()
client.Connect("myserver")
client.Authenticate("userName", "password")
Dim opt As New TransferOptions()
opt.MoveFiles = True
' Move files from "/data" to "c:\data"
client.Download("/data", "c:\data", opt)
' Move .dat files from "/data2" to "c:\data"
client.Upload("/data2/*.dat", "c:\data", opt)
End Using
Move files and directories from another file system
using (Ftp client = new Ftp())
{
client.Connect("myserver");
client.Authenticate("userName", "password");
TransferOptions opt = new TransferOptions();
opt.MoveFiles = true;
// Open a ZIP archive
Zip zip = new Zip();
zip.Open("myZip.zip");
// Move files from "/data" in the ZIP archive to "/data"
client.Upload(zip, "/data", false, null, "/data", opt);
// Move .dat files from "/data2" in the ZIP archive to "/data"
client.Upload(zip, "/data2/*.dat", false, null, "/data", opt);
zip.Close();
}
Using client As New Ftp()
client.Connect("myserver")
client.Authenticate("userName", "password")
Dim opt As New TransferOptions()
opt.MoveFiles = True
' Open a ZIP archive
Dim zip As New Zip()
zip.Open("myZip.zip")
' Move files from "/data" in the ZIP archive to "/data"
client.Upload(zip, "/data", False, Nothing, "/data", opt)
' Move .dat files from "/data2" in the ZIP archive to "/data"
client.Upload(zip, "/data2/*.dat", False, Nothing, "/data", opt)
zip.Close()
End Using
Move remote files and directories to another file system
using (Ftp client = new Ftp())
{
client.Connect("myserver");
client.Authenticate("userName", "password");
TransferOptions opt = new TransferOptions();
opt.MoveFiles = true;
// Open a ZIP archive
Zip zip = new Zip();
zip.Open("myZip.zip");
// Move files from "/data" in the ZIP archive to "/data"
client.Upload(zip, "/data", false, null, "/data", opt);
// Move .dat files from "/data2" in the ZIP archive to "/data"
client.Upload(zip, "/data2/*.dat", false, null, "/data", opt);
zip.Close();
}
Using client As New Ftp()
client.Connect("myserver")
client.Authenticate("userName", "password")
Dim opt As New TransferOptions()
opt.MoveFiles = True
' Open a ZIP archive
Dim zip As New Zip()
zip.Open("myZip.zip")
' Move files from "/data" in the ZIP archive to "/data"
client.Upload(zip, "/data", False, Nothing, "/data", opt)
' Move .dat files from "/data2" in the ZIP archive to "/data"
client.Upload(zip, "/data2/*.dat", False, Nothing, "/data", opt)
zip.Close()
End Using