简体   繁体   English

使用Powershell进行FTP上载文件的年月日目录结构

[英]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: 我已经检查了一些来源,为了完整起见:

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.

相关问题 根据文件名(日期)更改文件夹结构年>月-powershell - Change folder structure Year >Month based on Filename(DATE) -powershell Powershell:根据月份和年份创建文件夹结构并相应地复制文件 - Powershell: creating folder structure based on month and year and copying file accordingly 在 PowerShell 中使用 WinSCP .NET 程序集监控 FTP 服务器上上传的文件 - Monitoring uploaded files on FTP server using WinSCP .NET assembly in PowerShell 使用Powershell仅复制文件而不复制目录结构 - copy files only not directory structure using powershell Powershell脚本,用于根据创建时间戳将文件移动到年/月文件夹中 - Powershell script to move files into year/month folders based on creation timestamp 根据文件名时间戳Powershell将文件移动到年/月文件夹 - Move files into year/month folders based on file name timestamp powershell 如何在powershell中删除30天以前的文件并保留每个月的最后一天文件 - How to delete 30 day older files and keep last day files for each month in powershell PowerShell 脚本将所有旧文件归档到基于月份的目录中 - PowerShell script to archive all old files in a directory based of month Powershell获得本月的第n天 - Powershell Getting the nth day of the month 在powershell上获取一个月的最后一天 - Get the last day of a month on powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM