어제와 관련된 이름을 사용하여 디렉토리의 FTP 파일에 배치 파일을 만드는 방법은 무엇입니까?

어제와 관련된 이름을 사용하여 디렉토리의 FTP 파일에 배치 파일을 만드는 방법은 무엇입니까?

나는 그들의 이름에 날짜가 인코딩된 디렉토리 세트를 가지고 있습니다.
해당 디렉토리 안에는 files.csv.

디렉토리에는 다음과 같은 표준 이름이 있습니다.

/opt/oss/서버/var/fileint/pm/pmexport_20150915

여기서 마지막 디렉터리의 이름은 날짜에 따라 변경됩니다. 예를 들어 pmexport_201509152015년 9월 15일의 디렉터리입니다.

files.csv전날의 고정 대상으로 자동으로 ftp하는 배치 파일을 만들고 싶습니다 . 예를 들어 오늘은 2015년 9월 16일입니다. 어제 디렉토리에서
ftp로 접속하고 싶습니다 .files.csvpmexport_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 쉘이나 Powershell을 프로그래밍해야 하는 경우 수행해야 하는 단계는 동일하며 언어만 다릅니다. 언어에 필요한 단어(명령)가 없고 다른 단어를 사용해야 하는 경우는 거의 없습니다.

나는 당신에게 완전한 작동 솔루션을 제공하지 않을 것이며 안전한 복사본을 만드는 방법을 사용할 것입니다 scp(원격 파일 복사 프로그램입니다). 힌트와 유용하다고 생각되는 몇 가지 명령으로 활용하세요. (do man datefor the help of 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위의 내용을 병합할 수 있습니다.이 답변.

관련 정보