[英]Year Month Day directory structure for FTP uploaded files with Powershell
I currently have a PowerShell script for periodically uploading certain files to an FTP server. 我目前有一个PowerShell脚本,用于定期将某些文件上传到FTP服务器。 This script dumps every file into one main folder. 此脚本将每个文件转储到一个主文件夹中。 What I would like to have is a script that uploads the files in a (root)/year/month/day structure. 我想要的是一个脚本,它以(根)/年/月/日结构上传文件。
I have checked a lot of places for some script I would be able to use for this purpose, but couldn't get any of them to work. 我已经检查了很多地方可以用于此目的的一些脚本,但是无法使它们中的任何一个工作。
Any suggestions? 有什么建议么? I must've tried the first 100 results on google. 我一定是在google上尝试了前100个结果。
'Dump it all into one folder' upload script: “将其全部转储到一个文件夹中”上传脚本:
$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"
}
}
Some of the sources I already checked, for completeness sake: 我已经检查了一些来源,为了完整起见:
Creating a directory on remote FTP using powershell 使用powershell在远程FTP上创建目录
https://superuser.com/questions/516232/creating-a-folder-named-after-the-current-date-and-time (can't get this to work with FTP?) 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 Powrshell脚本,用于根据创建时间戳将文件移动到年/月文件夹中
https://community.spiceworks.com/topic/486290-help-with-powershell-script-ftp-upload https://community.spiceworks.com/topic/486290-help-with-powershell-script-ftp-upload
This will upload the file to yy/mm/dd
folder: 这会将文件上传到yy/mm/dd
文件夹:
$uri = New-Object System.Uri($ftp + $(Get-Date -f "yyyy/MM/dd") + "/" + $name)
(assuming the $ftp
already ends with a slash) (假设$ftp
已经以斜线结尾)
But the folder has to exist already. 但该文件夹必须已经存在。
If the folder does not exist, you have to create it: 如果该文件夹不存在,则必须创建它:
Creating a directory on remote FTP using powershell 使用powershell在远程FTP上创建目录
You may need to create all the levels: 您可能需要创建所有级别:
$ftp + $(Get-Date -f "yyyy")
$ftp + $(Get-Date -f "yyyy/MM")
$ftp + $(Get-Date -f "yyyy/MM/dd")
Obviously storing the Get-Date
to a variable and reusing it, is a good idea. 显然,将Get-Date
存储到变量并重用它是一个好主意。 To protect your from problems, if a new day started in the middle of the execution. 为了保护您免受问题困扰,如果在执行过程中开始新的一天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.