PowerShell 如何删除带有 FTP 的目录?
PowerShell How can I delete a directory with FTP?
来自 this list of Web Request Method Fields, I've successfully deleted files from my server via FTP, by basically filling in the guide on this page 稍作修改:
$url = "ftp://server.net/path/to/place/FILE.txt"
$userName = "username"
$password = "p@ssw0rd!"
$ftpreq = [System.Net.FtpWebRequest]::create($url)
$ftpreq.Credentials = New-Object System.Net.NetworkCredential($userName, $password)
$ftpreq.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftpreq.GetResponse()
文件名为 FILE.txt
,文件夹名为 FOLDER
。
当我尝试做类似的事情时,但对于文件夹,我收到 PowerShell 错误和 550 响应。
我基本上尝试了两种文件删除方法。
尝试 1:更改 URL 以匹配文件夹名称,保持方法
请注意,我要删除的文件夹位于我已成功删除的文件旁边。
$url = "ftp://server.net/path/to/place/FOLDER"
$userName = "username"
$password = "p@ssw0rd!"
$ftpreq = [System.Net.FtpWebRequest]::create($url)
$ftpreq.Credentials = New-Object System.Net.NetworkCredential($userName, $password)
$ftpreq.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftpreq.GetResponse()
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable
(e.g., file not found, no access).
尝试 2:更改方法
$url = "ftp://server.net/path/to/place/FOLDER"
$userName = "username"
$password = "p@ssw0rd!"
$ftpreq = [System.Net.FtpWebRequest]::create($url)
$ftpreq.Credentials = New-Object System.Net.NetworkCredential($userName, $password)
$ftpreq.Method = [System.Net.WebRequestMethods+Ftp]::RemoveDirectory
$ftpreq.GetResponse()
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable
(e.g., file not found, no access)."
已实施解决方案
function DeleteFtpFolder($url, $credentials)
{
$listRequest = [Net.WebRequest]::Create($url)
$listRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
$listRequest.Credentials = $credentials
$lines = New-Object System.Collections.ArrayList
$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
$line = $listReader.ReadLine()
$lines.Add($line) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()
foreach ($line in $lines)
{
$tokens = $line.Split(" ", 5, [System.StringSplitOptions]::RemoveEmptyEntries)
$type = $tokens[2]
$name = $tokens[3]
$fileUrl = ($url + "/" + $name)
if ($type -eq "<DIR>")
{
Write-Host "Found folder: $name"
DeleteFtpFolder $fileUrl $credentials
Write-Host "Deleting folder: $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+FTP]::RemoveDirectory
$deleteRequest.GetResponse() | Out-Null
}
else
{
$fileUrl = ($url + "/" + $name)
Write-Host "Deleting file: $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+FTP]::DeleteFile
$deleteRequest.GetResponse() | Out-Null
}
}
}
$credentials = New-Object System.Net.NetworkCredential($AzureFtpUsername, $AzureFtpPassword)
$url = $AzureFtpUrl
DeleteFtpFolder $url $credentials
该设置是 Octopus Deploy 内流程步骤中的嵌入式 PowerShell 脚本。这就是我在底部调用函数的原因。
此解决方案与 几乎相同,只是在我放置递归调用的位置以及如何解析从服务器返回的数据方面做了一些小改动。他的解决方案看起来更像是 ls
输出,有更多的列,而我的解决方案看起来更像 dir
输出,有更少的列。但即便如此,它看起来也不像我本地 windows 机器上的 dir
输出,所以我不太确定到底发生了什么。但它有效,所以这就足够了。
如果目录不为空,RMD
FTP 命令(RemoveDirectory
方法)失败。
通常它会失败并出现如下错误:
550 Directory not empty.
不幸的是 FtpWebRequest
有一个坏习惯 "translating" 将 FTP 错误代码添加到自己的消息中。在这种情况下,它 "translates" 550 到:
File unavailable (e.g., file not found, no access).
什么隐藏了真正的问题。
无论如何,FtpWebRequest
class(或 .NET 框架中的任何其他 FTP 实现)不支持递归操作。你必须自己实现递归:
- 列出远程目录
- 迭代条目、删除文件并递归到子目录(再次列出它们等)
棘手的部分是从子目录中识别文件。 FtpWebRequest
无法以可移植的方式做到这一点。不幸的是,FtpWebRequest
不支持 MLSD
命令,这是在 FTP 协议中检索具有文件属性的目录列表的唯一可移植方式。另见 .
您的选择是:
- 对文件名执行操作,该操作肯定对文件失败而对目录成功(反之亦然)。 IE。你可以试试下载"name"。如果成功,它是一个文件,如果失败,它是一个目录。但是,当您有大量条目时,这可能会成为性能问题。
- 你可能很幸运,在你的具体情况下,你可以通过文件名从目录中区分文件(即你的所有文件都有扩展名,而子目录没有)
- 您使用长目录列表(
LIST
命令= ListDirectoryDetails
方法)并尝试解析特定于服务器的列表。许多 FTP 服务器使用 *nix 样式列表,您可以在条目的最开头通过 d
识别目录。但是许多服务器使用不同的格式。以下示例使用此方法(假设为 *nix 格式)。
- 在这种特定情况下,您可以尝试将条目作为文件删除。如果删除失败,请尝试将条目列为目录。如果列表成功,您假设它是一个文件夹并相应地进行。不幸的是,当您尝试列出文件时,某些服务器不会出错。他们只会 return 列出一个文件条目。
function DeleteFtpFolder($url, $credentials)
{
$listRequest = [Net.WebRequest]::Create($url)
$listRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$listRequest.Credentials = $credentials
$lines = New-Object System.Collections.ArrayList
$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
$line = $listReader.ReadLine()
$lines.Add($line) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()
foreach ($line in $lines)
{
$tokens = $line.Split(" ", 9, [StringSplitOptions]::RemoveEmptyEntries)
$name = $tokens[8]
$permissions = $tokens[0]
$fileUrl = ($url + $name)
if ($permissions[0] -eq 'd')
{
DeleteFtpFolder ($fileUrl + "/") $credentials
}
else
{
Write-Host "Deleting file $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$deleteRequest.GetResponse() | Out-Null
}
}
Write-Host "Deleting folder"
$deleteRequest = [Net.WebRequest]::Create($url)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::RemoveDirectory
$deleteRequest.GetResponse() | Out-Null
}
使用如下函数:
$url = "ftp://ftp.example.com/path/to/folder/";
$credentials = New-Object System.Net.NetworkCredential("username", "password")
DeleteFtpFolder $url $credentials
或者使用支持递归操作的第三方库。
例如 WinSCP .NET assembly you can delete whole directory with a single call to Session.RemoveFiles
:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "username"
Password = "password"
}
$session = New-Object WinSCP.Session
# Connect
$session.Open($sessionOptions)
# Remove folder
$session.RemoveFiles("/path/to/folder").Check()
# Disconnect, clean up
$session.Dispose()
如果服务器支持,WinSCP 在内部使用 MLSD
命令。如果没有,它使用 LIST
命令并支持数十种不同的列表格式。
(我是WinSCP的作者)
Martin Prikryl 对 Azure 的回答几乎没有变化 FTP
这将适用于以下格式
"01-12-22 07:38AM <DIR> assets"
"12-16-21 12:22PM 959 African Union.png"
function DeleteFtpFolder( $url, $credentials)
{
$listRequest = [Net.WebRequest]::Create($url)
$listRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$listRequest.Credentials = $credentials
$lines = New-Object System.Collections.ArrayList
$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
$line = $listReader.ReadLine()
$lines.Add($line) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()
foreach ($line in $lines)
{
$tokens = $line.split(" ", 4, StringSplitOptions]::RemoveEmptyEntries)
$name = $tokens[3]
$permissions = $tokens[2]
$fileUrl = ($url + $name)
if ($permissions[0] -eq '<dir>')
{
DeleteFtpFolder ($fileUrl + "/") $credentials
}
else
{
Write-Host "Deleting file $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$deleteRequest.GetResponse() | Out-Null
}
}
Write-Host "Deleting folder"
$deleteRequest = [Net.WebRequest]::Create($url)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::RemoveDirectory
$deleteRequest.GetResponse() | Out-Null
}
来自 this list of Web Request Method Fields, I've successfully deleted files from my server via FTP, by basically filling in the guide on this page 稍作修改:
$url = "ftp://server.net/path/to/place/FILE.txt"
$userName = "username"
$password = "p@ssw0rd!"
$ftpreq = [System.Net.FtpWebRequest]::create($url)
$ftpreq.Credentials = New-Object System.Net.NetworkCredential($userName, $password)
$ftpreq.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftpreq.GetResponse()
文件名为 FILE.txt
,文件夹名为 FOLDER
。
当我尝试做类似的事情时,但对于文件夹,我收到 PowerShell 错误和 550 响应。
我基本上尝试了两种文件删除方法。
尝试 1:更改 URL 以匹配文件夹名称,保持方法
请注意,我要删除的文件夹位于我已成功删除的文件旁边。
$url = "ftp://server.net/path/to/place/FOLDER"
$userName = "username"
$password = "p@ssw0rd!"
$ftpreq = [System.Net.FtpWebRequest]::create($url)
$ftpreq.Credentials = New-Object System.Net.NetworkCredential($userName, $password)
$ftpreq.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$ftpreq.GetResponse()
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
尝试 2:更改方法
$url = "ftp://server.net/path/to/place/FOLDER"
$userName = "username"
$password = "p@ssw0rd!"
$ftpreq = [System.Net.FtpWebRequest]::create($url)
$ftpreq.Credentials = New-Object System.Net.NetworkCredential($userName, $password)
$ftpreq.Method = [System.Net.WebRequestMethods+Ftp]::RemoveDirectory
$ftpreq.GetResponse()
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
已实施解决方案
function DeleteFtpFolder($url, $credentials)
{
$listRequest = [Net.WebRequest]::Create($url)
$listRequest.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
$listRequest.Credentials = $credentials
$lines = New-Object System.Collections.ArrayList
$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
$line = $listReader.ReadLine()
$lines.Add($line) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()
foreach ($line in $lines)
{
$tokens = $line.Split(" ", 5, [System.StringSplitOptions]::RemoveEmptyEntries)
$type = $tokens[2]
$name = $tokens[3]
$fileUrl = ($url + "/" + $name)
if ($type -eq "<DIR>")
{
Write-Host "Found folder: $name"
DeleteFtpFolder $fileUrl $credentials
Write-Host "Deleting folder: $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+FTP]::RemoveDirectory
$deleteRequest.GetResponse() | Out-Null
}
else
{
$fileUrl = ($url + "/" + $name)
Write-Host "Deleting file: $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+FTP]::DeleteFile
$deleteRequest.GetResponse() | Out-Null
}
}
}
$credentials = New-Object System.Net.NetworkCredential($AzureFtpUsername, $AzureFtpPassword)
$url = $AzureFtpUrl
DeleteFtpFolder $url $credentials
该设置是 Octopus Deploy 内流程步骤中的嵌入式 PowerShell 脚本。这就是我在底部调用函数的原因。
此解决方案与 ls
输出,有更多的列,而我的解决方案看起来更像 dir
输出,有更少的列。但即便如此,它看起来也不像我本地 windows 机器上的 dir
输出,所以我不太确定到底发生了什么。但它有效,所以这就足够了。
RMD
FTP 命令(RemoveDirectory
方法)失败。
通常它会失败并出现如下错误:
550 Directory not empty.
不幸的是 FtpWebRequest
有一个坏习惯 "translating" 将 FTP 错误代码添加到自己的消息中。在这种情况下,它 "translates" 550 到:
File unavailable (e.g., file not found, no access).
什么隐藏了真正的问题。
无论如何,FtpWebRequest
class(或 .NET 框架中的任何其他 FTP 实现)不支持递归操作。你必须自己实现递归:
- 列出远程目录
- 迭代条目、删除文件并递归到子目录(再次列出它们等)
棘手的部分是从子目录中识别文件。 FtpWebRequest
无法以可移植的方式做到这一点。不幸的是,FtpWebRequest
不支持 MLSD
命令,这是在 FTP 协议中检索具有文件属性的目录列表的唯一可移植方式。另见
您的选择是:
- 对文件名执行操作,该操作肯定对文件失败而对目录成功(反之亦然)。 IE。你可以试试下载"name"。如果成功,它是一个文件,如果失败,它是一个目录。但是,当您有大量条目时,这可能会成为性能问题。
- 你可能很幸运,在你的具体情况下,你可以通过文件名从目录中区分文件(即你的所有文件都有扩展名,而子目录没有)
- 您使用长目录列表(
LIST
命令=ListDirectoryDetails
方法)并尝试解析特定于服务器的列表。许多 FTP 服务器使用 *nix 样式列表,您可以在条目的最开头通过d
识别目录。但是许多服务器使用不同的格式。以下示例使用此方法(假设为 *nix 格式)。 - 在这种特定情况下,您可以尝试将条目作为文件删除。如果删除失败,请尝试将条目列为目录。如果列表成功,您假设它是一个文件夹并相应地进行。不幸的是,当您尝试列出文件时,某些服务器不会出错。他们只会 return 列出一个文件条目。
function DeleteFtpFolder($url, $credentials)
{
$listRequest = [Net.WebRequest]::Create($url)
$listRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$listRequest.Credentials = $credentials
$lines = New-Object System.Collections.ArrayList
$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
$line = $listReader.ReadLine()
$lines.Add($line) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()
foreach ($line in $lines)
{
$tokens = $line.Split(" ", 9, [StringSplitOptions]::RemoveEmptyEntries)
$name = $tokens[8]
$permissions = $tokens[0]
$fileUrl = ($url + $name)
if ($permissions[0] -eq 'd')
{
DeleteFtpFolder ($fileUrl + "/") $credentials
}
else
{
Write-Host "Deleting file $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$deleteRequest.GetResponse() | Out-Null
}
}
Write-Host "Deleting folder"
$deleteRequest = [Net.WebRequest]::Create($url)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::RemoveDirectory
$deleteRequest.GetResponse() | Out-Null
}
使用如下函数:
$url = "ftp://ftp.example.com/path/to/folder/";
$credentials = New-Object System.Net.NetworkCredential("username", "password")
DeleteFtpFolder $url $credentials
或者使用支持递归操作的第三方库。
例如 WinSCP .NET assembly you can delete whole directory with a single call to Session.RemoveFiles
:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp.example.com"
UserName = "username"
Password = "password"
}
$session = New-Object WinSCP.Session
# Connect
$session.Open($sessionOptions)
# Remove folder
$session.RemoveFiles("/path/to/folder").Check()
# Disconnect, clean up
$session.Dispose()
如果服务器支持,WinSCP 在内部使用 MLSD
命令。如果没有,它使用 LIST
命令并支持数十种不同的列表格式。
(我是WinSCP的作者)
Martin Prikryl 对 Azure 的回答几乎没有变化 FTP
这将适用于以下格式
"01-12-22 07:38AM <DIR> assets"
"12-16-21 12:22PM 959 African Union.png"
function DeleteFtpFolder( $url, $credentials)
{
$listRequest = [Net.WebRequest]::Create($url)
$listRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
$listRequest.Credentials = $credentials
$lines = New-Object System.Collections.ArrayList
$listResponse = $listRequest.GetResponse()
$listStream = $listResponse.GetResponseStream()
$listReader = New-Object System.IO.StreamReader($listStream)
while (!$listReader.EndOfStream)
{
$line = $listReader.ReadLine()
$lines.Add($line) | Out-Null
}
$listReader.Dispose()
$listStream.Dispose()
$listResponse.Dispose()
foreach ($line in $lines)
{
$tokens = $line.split(" ", 4, StringSplitOptions]::RemoveEmptyEntries)
$name = $tokens[3]
$permissions = $tokens[2]
$fileUrl = ($url + $name)
if ($permissions[0] -eq '<dir>')
{
DeleteFtpFolder ($fileUrl + "/") $credentials
}
else
{
Write-Host "Deleting file $name"
$deleteRequest = [Net.WebRequest]::Create($fileUrl)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
$deleteRequest.GetResponse() | Out-Null
}
}
Write-Host "Deleting folder"
$deleteRequest = [Net.WebRequest]::Create($url)
$deleteRequest.Credentials = $credentials
$deleteRequest.Method = [System.Net.WebRequestMethods+Ftp]::RemoveDirectory
$deleteRequest.GetResponse() | Out-Null
}