如何建立一個批次檔來ftp目錄中與昨天相關的名稱的檔案?

如何建立一個批次檔來ftp目錄中與昨天相關的名稱的檔案?

我有一組目錄,其名稱中編碼有日期。
這些目錄內有名為files.csv.

這些目錄有一個標準名稱,例如,

/opt/oss/server/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你可以將上面的內容與這個答案

相關內容