我目前有一個PowerShell腳本,用於定期將某些文件上傳到FTP服務器。該腳本將每個文件轉儲到一個主文件夾中。 我想要的是一個以(根)/年/月/日結構上傳文件的腳本。Powershell FTP上傳文件的年月日目錄結構
我已經檢查了很多的某些腳本的地方我可以用於此目的,但無法讓他們工作。
有什麼建議嗎?我一定試過谷歌的前100個結果。
'轉儲所有到一個文件夾' 上傳腳本:
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object
System.Net.NetworkCredential($user,$pass)
foreach($item in ($checkitems)){
Write-host "Uploading $item..." -ForegroundColor Green
$dt = Get-Date
Add-Content $log3 "$dt : Uploading $item..."
$name=$item.Name
$name=[System.Uri]::EscapeDataString($name)
$uri = New-Object System.Uri($ftp+$name+"")
$webclient.UploadFile($uri, $item.FullName)
if($error -ne $null)
{
Write-Host "Items will not be moved" -ForegroundColor Red
}
else
{
Write-Host "Moving $item to processed" -ForegroundColor green
Move-Item $item.Fullname $processed
$dt = Get-Date
Add-Content $log3 "$dt : Moving $item to processed"
}
}
一些我已經選中該人士透露,爲了完整起見:
https://superuser.com/questions/516232/creating-a-folder-named-after-the-current-date-and-time(可沒有得到這與FTP工作?)
Powrshell script to move files into year/month folders based on creation timestamp
https://community.spiceworks.com/topic/486290-help-with-powershell-script-ftp-upload
我需要添加一個檢查每個上傳的文件,以檢查目錄是否已經存在? 當另一個文件重新創建該文件夾時,如果該文件夾(已經包含文件)被「重新創建」會發生什麼?會拋出一個錯誤? – Alex
是的,它會拋出一個錯誤。如果您選擇首先檢查並僅創建它(如果它不存在),則取決於您。或者只是試着總是忽略錯誤。 –
我得到關於uri值不是字符串的錯誤。我試圖將其轉換爲這樣的一個: 編輯:標記很難。鏈接:https://i.imgur.com/f3Dx7md.png – Alex