如何创建一个批处理文件来将目录中的文件以与昨天相关的名称传输到 ftp?

如何创建一个批处理文件来将目录中的文件以与昨天相关的名称传输到 ftp?

我有一组目录,其名称中编码了日期。
这些目录中有名为的文件files.csv

这些目录有一个标准名称,例如,

/opt/oss/服务器/var/fileint/pm/pmexport_20150915

最后一个目录的名称随日期变化,例如 pmexport_201509152015 年 9 月 15 日的目录。

我想创建一个批处理文件,自动将files.csv前一天的内容通过 ftp 传输到固定目的地。
例如,今天是 2015 年 9 月 16 日:我想files.csv从昨天的目录中通过ftp 传输pmexport_20150915

答案1

你见过吗有关使用 Powershell 上传文件的帖子?

    ##################################################################################### 
##           Script will Upload files to FTP    
##           Author:  Vikas Sukhija                                                            
##                                                                     
##           Date: 02-24-2013 
##           Modified Date:- 02-26-2013 (included loging & monitoring) 
##################################################################################### 
#############################Define Log Files######################################## 

$date = get-date -format d 
$date = $date.ToString().Replace(“/”, “-”) 
$time = get-date -format t 
$month = get-date  
$month1 = $month.month 
$year1 = $month.year 
$time = $time.ToString().Replace(":", "-") 
$time = $time.ToString().Replace(" ", "") 

$log1 = ".\Logs" + "\" + "FTP_" + $date + "_.log" 
$log2 = ".\Logs" + "\" + "FTP_" + $month1 +"_" + $year1 +"_.log" 
$log3 = ".\Logs" + "\" + "FTP_" + $date + $time + "_.log" 

$logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt" 

#Start-Transcript -Path $logs  
$dt = Get-Date 
Add-Content $log3 "$dt : Script Started" 

###########################Variables###################################### 

$smtpServer = "smtp.lab.com" # Change  
$fromadd = "[email protected]" # Change  
$email1 = "[email protected]"  # Change  
$ftp = "ftp://127.0.0.1/" # Change 
$user = "vikas"  # Change 
$pass = "password" # Change 
$uploadfpath = "C:\Uploadftp\ftpfiles" # Define the Folder from where files will be uploaded  

########################################################################### 

$checkitems = Get-ChildItem $uploadfpath 
$countitems = $checkitems.count 
if ($countitems -eq 0) 
{ 
Write-Host "No items to process" -ForegroundColor Green 
$dt = Get-Date 
Add-Content $log3 "$dt : No items to process, script will exit" 
exit 
} 
$dt = Get-Date 
Add-Content $log3 "$dt : Total number of items to process $countitems" 

$processed = ".\processed\$date" + "-" + $time 
if((test-path $processed) -like $false) 
{ 
New-Item -Path "$processed" -type directory 
} 

##################################################################################### 

if ($error -ne $null) 
      { 
#SMTP Relay address 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 

#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1) 
$msg.Subject = "FTP Script error" 
$msg.Body = $error 
$smtp.Send($msg) 
$dt = Get-Date 
Add-Content $log3 "$dt : Script Terminated because of error: $error" 
$error.clear() 
exit 

       } 
  else 

      { 
    Write-host "no errors till now" 
      } 

$webclient = New-Object System.Net.WebClient  

$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)   

#Upload each file in upload directory... 

foreach($item in (dir $uploadfpath "*.*")){  
    Write-host  "Uploading $item..."  -ForegroundColor Green 
    $dt = Get-Date 
    Add-Content $log3 "$dt : Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$item.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 "$uploadfpath\$item" $processed 
    $dt = Get-Date 
    Add-Content $log3 "$dt : Moving $item to processed" 
        } 
 }  

if ($error -ne $null) 
      { 
#SMTP Relay address 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 

#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1) 
$msg.Subject = "FTP Script error" 
$msg.Body = $error 
$smtp.Send($msg) 
$dt = Get-Date 
Add-Content $log3 "$dt : Script encountered error: $error" 
$error.clear() 
       } 
  else 

      { 
    Write-host "no errors till now" 
      } 
$dt = Get-Date 
Add-Content $log3 "$dt : Script Processing finished" 

#Stop-Transcript 
##################################################################

您需要创建的只是一个包含要更新的文件的字符串。如果您以这种方式使用 Get-Date 函数:
(Get-Date).AddDays(-1).ToString('yyyyMMdd'),
那么您将能够创建一个包含要上传的文件的字符串数组。您应该能够通过修改 get-childitem 以在字符串上包含过滤器来做到这一点。
我无法完全编译并为您测试它,因为我没有 FTP 来检查它,而且时间有限,但希望通过快速调整,您可以让它正常工作。

答案2

假设你想获取文件从 Linux 机器。如果您必须编写 bash shell 或 powershell,那么您必须执行的步骤是相同的​​,只有语言不同......一种语言中很少没有您需要的单词(命令),而您必须使用其他单词。

我不会给你一个完整的解决方案,我会使用scp它来制作一个安全副本(这是一个远程文件复制程序)。把它当作提示和一些你会发现有用的命令(man date在帮助下执行date...)。

#!/bin/bash
DestinationPath="/tmp"    # Here you have to put where you want to copy the file
FileToTake='files.csv'    # Here the file you want to take
PathFrom=$(date -d yesterday "+/opt/oss/server/var/fileint/pm/pmexport_%Y%m%d")
FullFileNameToTake="${PathFrom}/${FileToTake}"
scp -p user@host:"$FullFileNameToTake" "$DestinationPath"

如果你想使用,ftp你可以将上面的内容与这个答案

相关内容