Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $yesterday = [DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
- # OR I have to use ToString("MM/dd/yyyy") for months 10-12,
- # but I need both formats to work.
- #delete the temporary file
- del .FTPfiles.txt
- # Load WinSCP .NET assembly
- Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.dll"
- # Setup session options
- $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
- Protocol = [WinSCP.Protocol]::Ftp
- HostName = "server.com"
- UserName = "joe"
- Password = "smith"
- }
- $session = New-Object WinSCP.Session
- try
- {
- # Connect
- $session.Open($sessionOptions)
- $directory = $session.ListDirectory("/Folder")
- foreach ($fileInfo in $directory.Files)
- {
- Write-Output ("{1} {0}" -f
- $fileInfo.Name, $fileInfo.LastWriteTime) >> FTPfiles.txt
- }
- $fileList = get-content .FTPfiles.txt | findstr $yesterday
- $stripped = $fileList -creplace '^.*Z12', 'Z12'
- # Download files
- $remotePath = "/Folder/"
- $transferOptions = New-Object WinSCP.TransferOptions
- $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
- $lines = $stripped
- foreach ($line in $lines)
- {
- Write-Host ("Downloading {0} ..." -f $line)
- $session.GetFiles($remotePath+$line, "C:Downloads").Check()
- }
- }
- catch [Exception]
- {
- Write-Host ("Error: {0}" -f $_.Exception.Message)
- exit 1
- }
- finally
- {
- # Disconnect, clean up
- $session.Dispose()
- }
- FROM:
- 3/14/2017 2:04:00 AM Z1234_20170314050001_1.zip
- 3/14/2017 3:04:00 AM Z1234_20170315060002_1.zip
- 3/14/2017 4:04:00 AM Z1234_20170316070001_1.zip
- 3/14/2017 5:04:00 AM Z1234_20170317080001_1.zip
- TO:
- Z1234_20170314050001_1.zip
- Z1234_20170315060002_1.zip
- Z1234_20170316070001_1.zip
- Z1234_20170317080001_1.zip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement