我目前有一个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