Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Error trying to download file

$directoryInfo = $session.ListDirectory("/EXPORT/")


$files = $directoryInfo.Files | ?{$_.name -match "LYLI_$date"}

foreach ($Fileinfo in $files) {
   
   $session.GetFiles($Fileinfo, $LocalPath).Check()
   Write-Host ($fileinfo.Name)
}

In your code above, the $Fileinfo is an object of type RemoteFileInfo, while Session.GetFiles expects a string. PowerShell implicitly converts the object to a script by calling .ToString() method that is implemented by RemoteFileInfo to return Name. But Name is file name only, without a path. That's why .GetFiles fails, because it's not able to find the file without knowing its path.
Guest

This is resolved. I used a different method.

$session.Open($sessionOptions)

$directoryInfo = $session.ListDirectory("/EXPORT/")
$date = Get-Date -Format yyyyMMdd
$Files = $directoryInfo.Files | ?{$_.name -match "LYLI_$date"} | select -ExpandProperty name
$RemotePath = "/EXPORT/$Files"
$LocalPath = "C:\Users\samc\Desktop\FTP\$Files"

Try{
if ($session.FileExists($RemotePath)) {

Write-Host ("File {0} exists" -f $RemotePath)
$session.GetFiles($RemotePath, $LocalPath).Check()
}

else { Write-Host "File not availiable" }
}

catch [Exception]{
Write-Host $_.Exception.Message
}
samc

Error trying to download file

LOG for doing it with GUI. There are a lot more files. Didnt want to clog up the forum.

. 2014-07-11 14:16:24.527 --------------------------------------------------------------------------
. 2014-07-11 14:16:24.527 WinSCP Version 5.5.4 (Build 4433) (OS 6.1.7601 Service Pack 1 - Windows 7 Professional)
. 2014-07-11 14:16:24.527 Configuration: HKEY_CURRENT_USER\Software\Martin Prikryl\WinSCP 2\
. 2014-07-11 14:16:24.527 Local account: PRIMROSE\samc
. 2014-07-11 14:16:24.527 Working directory: C:\Program Files (x86)\WinSCP
. 2014-07-11 14:16:24.527 Process ID: 3376
. 2014-07-11 14:16:24.527 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe"
. 2014-07-11 14:16:24.527 Time zone: Current: GMT+1, Standard: GMT+0 (GMT Standard Time), DST: GMT+1 (GMT Daylight Time), DST Start: 30/03/2014, DST End: 26/10/2014
. 2014-07-11 14:16:24.528 Login time: 11 July 2014 14:16:24
. 2014-07-11 14:16:24.528 --------------------------------------------------------------------------
. 2014-07-11 14:16:24.528 Session name: FTP (Site)
. 2014-07-11 14:16:24.528 Host name: 79.171.178.50 (Port: 21)
. 2014-07-11 14:16:24.528 User name: PRIMROSE (Password: No, Key file: No)
. 2014-07-11 14:16:24.528 Tunnel: No
. 2014-07-11 14:16:24.528 Transfer Protocol: FTP
. 2014-07-11 14:16:24.528 Ping type: C, Ping interval: 30 sec; Timeout: 15 sec
. 2014-07-11 14:16:24.528 Proxy: none
. 2014-07-11 14:16:24.528 Send buffer: 262144
. 2014-07-11 14:16:24.528 UTF: 2
. 2014-07-11 14:16:24.528 FTP: FTPS: None; Passive: Yes [Force IP: A]; MLSD: A [List all: A]
. 2014-07-11 14:16:24.528 Local directory: C:\Program Files (x86)\WinSCP, Remote directory: /, Update: Yes, Cache: Yes
. 2014-07-11 14:16:24.528 Cache directory changes: Yes, Permanent: Yes
. 2014-07-11 14:16:24.528 DST mode: 1; Timezone offset: 0h 0m
. 2014-07-11 14:16:24.528 --------------------------------------------------------------------------
. 2014-07-11 14:16:24.588 Connecting to ********* ...
. 2014-07-11 14:16:24.690 Connected with *********. Waiting for welcome message...
< 2014-07-11 14:16:24.717 220 edi FTP server (IS FTP-Service, version 5.7.31) ready.
> 2014-07-11 14:16:24.717 USER PRIMROSE
< 2014-07-11 14:16:24.753 331 Password required for PRIMROSE (f78f875b735e37c7).
> 2014-07-11 14:16:35.598 PASS ************
< 2014-07-11 14:16:35.740 230 User PRIMROSE logged in.
> 2014-07-11 14:16:35.740 SYST
< 2014-07-11 14:16:35.950 215 UNIX Type: L8 Version: IS 5.7.31
> 2014-07-11 14:16:35.950 FEAT
< 2014-07-11 14:16:36.248 211-Extensions supported:
< 2014-07-11 14:16:36.248 CLNT
< 2014-07-11 14:16:36.248 AUTH TLS
< 2014-07-11 14:16:36.248 PBSZ
< 2014-07-11 14:16:36.248 PROT
< 2014-07-11 14:16:36.248 MDTM
< 2014-07-11 14:16:36.248 MFMT
< 2014-07-11 14:16:36.248 PASV
< 2014-07-11 14:16:36.248 REST STREAM
< 2014-07-11 14:16:36.249 SIZE
< 2014-07-11 14:16:36.249 XCRC "Filename" SP EP
< 2014-07-11 14:16:36.249 XMD5 "Filename" SP EP
< 2014-07-11 14:16:36.249 XSH1 "Filename" SP EP
< 2014-07-11 14:16:36.249 211 End.
. 2014-07-11 14:16:36.299 Connected
. 2014-07-11 14:16:36.299 --------------------------------------------------------------------------
. 2014-07-11 14:16:36.299 Using FTP protocol.
. 2014-07-11 14:16:36.299 Doing startup conversation with host.
> 2014-07-11 14:16:36.350 PWD
< 2014-07-11 14:16:36.721 257 "/" is current directory.
. 2014-07-11 14:16:36.751 Changing directory to "/".
> 2014-07-11 14:16:36.751 CWD /
< 2014-07-11 14:16:37.173 250 CWD command successful.
. 2014-07-11 14:16:37.173 Getting current directory name.
> 2014-07-11 14:16:37.173 PWD
< 2014-07-11 14:16:37.517 257 "/" is current directory.
. 2014-07-11 14:16:37.702 Retrieving directory listing...
> 2014-07-11 14:16:37.702 TYPE A
< 2014-07-11 14:16:38.001 200 Type set to A.
> 2014-07-11 14:16:38.002 PASV
< 2014-07-11 14:16:38.470 227 Entering Passive Mode (79,171,178,50,224,246)
> 2014-07-11 14:16:38.470 LIST -a
< 2014-07-11 14:16:38.914 150 ASCII data connection for / (212.58.48.107,57590).
. 2014-07-11 14:16:38.914 drw-rw-rw- 3 generic 4096 Jul 11 15:15 IMPORT
. 2014-07-11 14:16:38.914 drw-rw-rw- 3 generic 49152 Jul 11 15:06 EXPORT
< 2014-07-11 14:16:39.151 226 ASCII transfer complete.
. 2014-07-11 14:16:39.153 Directory listing successful
. 2014-07-11 14:16:39.153 ..;D;0;1899-12-30T00:00:00.000Z;"" [0];"" [0];---------;0
. 2014-07-11 14:16:39.153 IMPORT;D;4096;2014-07-11T14:15:00.000Z;"generic" [0];"" [0];rw-rw-rw-;1
. 2014-07-11 14:16:39.153 EXPORT;D;49152;2014-07-11T14:06:00.000Z;"generic" [0];"" [0];rw-rw-rw-;1
. 2014-07-11 14:16:39.253 Startup conversation with host finished.
. 2014-07-11 14:17:03.085 Changing directory to "IMPORT".
> 2014-07-11 14:17:03.085 CWD IMPORT
< 2014-07-11 14:17:03.117 250 CWD command successful.
. 2014-07-11 14:17:03.117 Getting current directory name.
> 2014-07-11 14:17:03.117 PWD
< 2014-07-11 14:17:03.148 257 "/IMPORT" is current directory.
. 2014-07-11 14:17:03.148 Retrieving directory listing...
> 2014-07-11 14:17:03.148 TYPE A
< 2014-07-11 14:17:03.179 200 Type set to A.
> 2014-07-11 14:17:03.180 PASV
< 2014-07-11 14:17:03.234 227 Entering Passive Mode (79,171,178,50,146,163)
> 2014-07-11 14:17:03.234 LIST -a
. 2014-07-11 14:17:03.293 <Empty directory listing>
< 2014-07-11 14:17:03.295 150 ASCII data connection for /IMPORT (212.58.48.107,37539).
< 2014-07-11 14:17:03.525 226 ASCII transfer complete.
. 2014-07-11 14:17:03.525 Directory listing successful
. 2014-07-11 14:17:03.525 ..;D;0;1899-12-30T00:00:00.000Z;"" [0];"" [0];---------;0
. 2014-07-11 14:17:06.009 Cached directory change via ".." to "/".
. 2014-07-11 14:17:06.009 Getting current directory name.
. 2014-07-11 14:17:06.011 Directory content loaded from cache.
. 2014-07-11 14:17:06.733 Changing directory to "EXPORT".
. 2014-07-11 14:17:06.733 Synchronizing current directory "/".
> 2014-07-11 14:17:06.733 CWD /
< 2014-07-11 14:17:06.764 250 CWD command successful.
> 2014-07-11 14:17:06.765 CWD EXPORT
< 2014-07-11 14:17:06.796 250 CWD command successful.
. 2014-07-11 14:17:06.796 Getting current directory name.
> 2014-07-11 14:17:06.796 PWD
< 2014-07-11 14:17:06.827 257 "/EXPORT" is current directory.
. 2014-07-11 14:17:06.827 Retrieving directory listing...
> 2014-07-11 14:17:06.827 TYPE A
< 2014-07-11 14:17:06.859 200 Type set to A.
> 2014-07-11 14:17:06.859 PASV
< 2014-07-11 14:17:06.911 227 Entering Passive Mode (79,171,178,50,189,221)
> 2014-07-11 14:17:06.911 LIST -a
< 2014-07-11 14:17:06.991 150 ASCII data connection for /EXPORT (212.58.48.107,48605).
< 2014-07-11 14:17:07.389 226 ASCII transfer complete.
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 47 Apr 08 11:06 LYBU_20140408_110539.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 15722 Jun 12 17:06 LYPA_20140612_170537.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 660 Apr 08 11:06 LYPA_20140408_110549.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 68955 Apr 30 09:05 LYPA_20140430_090501.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 110398 Apr 29 09:06 LYPA_20140429_090520.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 5519 May 19 09:06 LYPA_20140519_090553.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 40493 May 26 13:06 LYPA_20140526_130520.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 3224 Apr 14 17:05 LYPA_20140414_170501.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 11410 Jun 19 06:01 LYBV_20140619_060036.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 5318 May 01 12:06 LYPA_20140501_120559.TXT
. 2014-07-11 14:30:07.446 -rw-rw-rw- 1 generic 70340 May 08 09:06 LYPA_20140508_090551.TXT
. 2014-07-11 14:30:07.446 Directory listing successful
> 2014-07-11 14:30:37.757 TYPE A
< 2014-07-11 14:30:37.757 200 Type set to A.
. 2014-07-11 14:30:59.113 File: '/EXPORT/LYLI_20140708_180540.TXT' [2014-07-08T17:06:00.000Z] [620]
. 2014-07-11 14:30:59.129 Copying "/EXPORT/LYLI_20140708_180540.TXT" to local directory started.
. 2014-07-11 14:30:59.129 Binary transfer mode selected.
. 2014-07-11 14:30:59.129 Starting download of /EXPORT/LYLI_20140708_180540.TXT
> 2014-07-11 14:30:59.129 TYPE I
< 2014-07-11 14:30:59.176 200 Type set to I.
> 2014-07-11 14:30:59.176 PASV
< 2014-07-11 14:30:59.223 227 Entering Passive Mode (79,171,178,50,230,12)
> 2014-07-11 14:30:59.223 RETR LYLI_20140708_180540.TXT
< 2014-07-11 14:30:59.316 150 Binary data connection for /EXPORT/LYLI_20140708_180540.TXT (212.58.48.107,58892) (620 bytes).
< 2014-07-11 14:30:59.550 226 Binary transfer complete.
. 2014-07-11 14:30:59.566 Download successful
samc

Error trying to download file

I receive an error while trying to download a file. I have no logs and I don't know how to log. If someone could show me how to log, then I will provide that.

Error:
Exception calling "Check" with "0" argument(s): "Can't get attributes of file 

'LYLI_20140708_180540.TXT'."
At line:11 char:4
+    $session.GetFiles($Fileinfo, $LocalPath).Check()
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException


My code:

# Load WinSCP .NET assembly

Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::ftp
$sessionOptions.HostName = "server"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "pass"
$session = New-Object WinSCP.Session
 
$session.Open($sessionOptions)
$RemotePath = "/EXPORT/LYKA_$($date)_*/"
$LocalPath = "C:\Users\samc\Desktop\FTP\$Fileinfo"


$date = Get-Date -Year 2014 -Month 07 -Day 08 -Format yyyyMMdd
$wildcard = "LYLI*.txt"

$directoryInfo = $session.ListDirectory("/EXPORT/")

$files = $directoryInfo.Files | ?{$_.name -match "LYLI_$date"}

foreach ($Fileinfo in $files) {
   
   $session.GetFiles($Fileinfo, $LocalPath).Check()
   Write-Host ($fileinfo.Name)
}