我正在嘗試編寫一個PowerShell腳本,該腳本可以自動刪除我們的FTP服務器上的空目錄。我沒有任何直接訪問目錄所在的服務器的權限 - 我只能通過FTP訪問它們。由於這個原因,我寫了一個PowerShell腳本,它使用WinSCP .NET程序集來嘗試刪除空目錄。但是,我遇到了問題,因爲服務器上的許多目錄在目錄名稱中都有方括號。無法使用WinSCP和PowerShell刪除名稱中帶有方括號的目錄
例如,目錄名可能被稱爲:[a]153432
有很多這樣的目錄,因此我寫了一個腳本來刪除這些願望。發生這種情況是因爲它們是由使用數字創建所需目錄的程序創建的。爲了本工作方案,我創建了一個空目錄
/latest/[b]test
我的計劃是這樣的:
# program to test deletion of directories with square-brackets in the name.
$HOSTNAME="myftpservername"
$USERNAME="myftpusername"
$PASSWORD="myftppassword"
$DLL_LOCATION="C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$LOCAL_DIRECTORY="C:\testdir\extended\baseroot"
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom($DLL_LOCATION) | Out-Null
# Session.FileTransferred event handler
try
{
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName = $HOSTNAME
$sessionOptions.UserName = $USERNAME
$sessionOptions.Password = $PASSWORD
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$remoteFileWithUnixPath = "/latest/[b]test"
$removalResult = $session.RemoveFiles($remoteFileWithUnixPath)
if ($removalResult.IsSuccess)
{
Write-Host ("Removal of remote file {0} succeeded" -f $remoteFileWithUnixPath)
}
else
{
Write-Host ("Removal of remote file {0} failed" -f $remoteFileWithUnixPath)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
當我運行它,它會顯示以下信息:
PS C:\Users\dbuddrige\Documents\dps> .\delete-squarebracket-dir.ps1
Removal of remote file /latest/[b]test succeeded
但是,該目錄不會被刪除。
我也曾嘗試指定與分隔符,如目錄名稱:
$remoteFileWithUnixPath = "/latest/\[b\]test"
但是這也失敗(但至少它是這麼說的):
PS C:\Users\dbuddrige\Documents\dps> .\delete-squarebracket-dir.ps1
Removal of remote file /latest/\[b\]test failed
但是,如果我改變目錄名[和變量$remoteFileWithUnixPath
]類似於
/latest/foo
然後重新運行程序,它刪除目錄/latest/foo
就好了。
有沒有人有任何想法,我需要做的才能讓這個工作?
回答這個問題是由馬丁Prikryl指出[!謝謝]
全工作的代碼如下:
# program to test deletion of directories with square-brackets in the name.
$HOSTNAME="myftpservername"
$USERNAME="myftpusername"
$PASSWORD="myftppassword"
$DLL_LOCATION="C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
$LOCAL_DIRECTORY="C:\testdir\extended\baseroot"
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom($DLL_LOCATION) | Out-Null
# Session.FileTransferred event handler
try
{
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName = $HOSTNAME
$sessionOptions.UserName = $USERNAME
$sessionOptions.Password = $PASSWORD
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$remoteFileWithUnixPath = "/latest/[b]test"
$removalResult =
$session.RemoveFiles($session.EscapeFileMask($remoteFileWithUnixPath))
if ($removalResult.IsSuccess)
{
Write-Host ("Removal of remote file {0} succeeded" -f $remoteFileWithUnixPath)
}
else
{
Write-Host ("Removal of remote file {0} failed" -f $remoteFileWithUnixPath)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
感謝 - 工作!我會發布更新的代碼,但主要變化是這樣的: $ removalResult = $ session.RemoveFiles($ session.EscapeFileMask(「/ latest/[b] test」)) – user3354235