Reputation: 79
I want to move all files located in 'Directory A' to 'Directory B' on the same FTP Server.
I have searched this site and found a similar question but the solution did not work for me and I am unable to comment as my reputation is less than 50.
The below code shows what I have tried so far;
$Source = "ftp://ftp3.example.com/Jaz/In/"
$user = 'username'
$pass = 'password'
$credentials = new-object System.Net.NetworkCredential($user, $pass)
$ftprequest = [System.Net.FtpWebRequest]::create($Source)
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::Rename
$ftprequest.RenameTo = "ftp://ftp3.example.com/Jaz/Backup"
Note:
- 'Directory A' = ftp://ftp3.example.com/Jaz/In
- 'Directory B' = ftp://ftp3.example.com/Jaz/Backup
- All files to be moved are .txt format
- Full permissions have been granted to all files & folders on FTP
Can someone please help with a solution, your knowledge is much appreciated.
Upvotes: 2
Views: 5498
Reputation: 202168
There's no support for batch or recursive FTP operations (like moving all files from directory to another) in PowerShell or .NET.
If you want to use a pure PowerShell, you have to move the files one by one.
$source = "ftp://ftp.example.com/source/path/"
$dest = "/dest/path/"
$credentials = new-object System.Net.NetworkCredential("username", "password")
Write-Host "Listing files..."
$listRequest = [System.Net.FtpWebRequest]::Create($source)
$listRequest.Credentials = $credentials
$listRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$files = New-Object System.Collections.ArrayList
$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
$file = $listReader.ReadLine()
$files.Add($file) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()
foreach ($file in $files)
{
Write-Host "Renaming $file..."
$renameRequest = [System.Net.FtpWebRequest]::Create($source + $file)
$renameRequest.Credentials = $credentials
$renameRequest.Method = [System.Net.WebRequestMethods+Ftp]::Rename
$renameRequest.RenameTo = $dest + $file
$renameRequest.GetResponse().Dispose()
}
The above code will move all entries in the directory (files or subfolders).
Other problems in your code (fixed in mine):
$credentials
RenameTo
is a path only, not an URL.The code would be much simpler with a better FTP client.
For example with WinSCP .NET assembly, it's as easy as:
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "username"
Password = "password"
}
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$source = "/source/path"
$dest = "/dest/path"
$files = $session.EnumerateRemoteFiles(
$source, $Null, [WinSCP.EnumerationOptions]::MatchDirectories)
foreach ($file in $files)
{
Write-Host "Renaming $file..."
$session.MoveFile(
$file.FullName, [WinSCP.RemotePath]::Combine($dest, $file.Name))
}
(I'm the author of WinSCP)
Upvotes: 3