วิธีสร้างแบตช์ไฟล์เป็นไฟล์ ftp ในไดเรกทอรีที่มีชื่อเมื่อวานนี้? [ปิด]


-11

ฉันมีชุดของไดเรกทอรีที่เข้ารหัสในชื่อวันที่ ภายในไดเรกทอรีที่มีชื่อไฟล์
files.csv

ไดเรกทอรีมีชื่อมาตรฐานเป็นตัวอย่างเช่น

/ opt / OSS / เซิร์ฟเวอร์ / var / fileint / PM / pmexport_20150915

โดยที่ชื่อของไดเรกทอรีสุดท้ายเปลี่ยนไปพร้อมกับวันเช่น pmexport_20150915เป็นไดเรกทอรีของ 15 กันยายน 2558

ฉันต้องการสร้างไฟล์แบตช์เป็น ftp โดยอัตโนมัติในfiles.csvวันก่อนหน้าไปยังปลายทางที่แน่นอน
ตัวอย่างเช่นวันนี้คือวันที่ 16 กันยายน 2558: ฉันต้องการ ftp files.csvจากสารบบของเมื่อวานนี้, pmexport_20150915.


ยินดีต้อนรับสู่ Super User เราไม่ใช่บริการเขียนสคริปต์ เราคาดว่าผู้ใช้จะบอกเราถึงสิ่งที่พวกเขาได้ลองมาแล้ว (รวมถึงรหัสที่ใช้) และที่ติดขัดเพื่อให้เราสามารถช่วยแก้ไขปัญหาเฉพาะได้ คำถามที่ว่ามีเพียงถามรหัสกว้างเกินไปและมีแนวโน้มที่จะวางไว้หรือปิด โปรดอ่านฉันจะถามคำถามที่ดีได้อย่างไร .
DavidPostill

@DavidPostill ฉันดูเหมือนจะบ่อนทำลายความคิดเห็นของคุณขออภัย ... ฉันจะทิ้งคำตอบไว้เพราะฉันยังไม่ได้เขียนรหัสเพิ่งชี้ไปที่ตัวอย่างเกือบจะมีตัวอย่างของตัวชี้ของวิธีการที่จะทำให้มันสมบูรณ์
Joe Taylor

@JoeTaylor ไม่ใช่ปัญหา เราแค่อยากให้ทุกคนพยายาม;)
DavidPostill

@DavidPostill - ดูเหมือนว่าเขากำลังใช้เครื่องที่ไม่มีหน้าต่างอยู่โดย filepath PowerShell ไม่ต้องสงสัยเลยจะช่วยเลย ....
Joe Taylor

คำตอบ:


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 = "DoNotReplyftp@lab.com" # Change  
$email1 = "Vikas.Sukhija@lab.com"  # 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) .Addays (-1) .ToString ('yyyyMMDd')
จากนั้นคุณจะสามารถสร้างอาร์เรย์สตริงที่มีไฟล์ที่คุณต้องการอัปโหลด คุณควรจะสามารถทำได้โดยการแก้ไข get-childitem เพื่อรวมตัวกรองในสตริง
ฉันไม่สามารถรวบรวมได้อย่างเต็มที่และทดสอบให้คุณเนื่องจากฉันไม่มี FTP เพื่อตรวจสอบด้วยและ จำกัด จำนวนเล่ม แต่หวังว่าด้วยการปรับแต่งอย่างรวดเร็วคุณจะทำงานได้ดี


เรียนโปรดการสนับสนุนของคุณเพื่อสร้างไฟล์แบตช์อย่างง่ายเพื่อให้ได้รับและแสดงวันที่ดีเมื่อวานนี้
SAKILANI Mohamed

1

ลองสมมติว่าคุณต้องการที่จะใช้ไฟล์จากเครื่อง 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คุณสามารถรวมสิ่งข้างต้นกับคำตอบนี้


เรียนทั้งหมดโปรดการสนับสนุนของคุณเพื่อสร้างไฟล์แบตช์อย่างง่ายเพื่อให้ได้รับและแสดงวันที่ดีเมื่อวานนี้
SAKILANI Mohamed
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.