Topic "copy multiple files using ps script"

Author Message
popotzo
[View user's profile]

Joined: 2015-05-14
Posts: 4
Hello,

I am trying to use the PS script from your site to copy multiple files from a remove server to a local folder. The original script is copying only 1 file but i need to copy 12 files. I 've changed from 1 to 12 but im receiving Can't get attributes of file. What am i doing wrong?


# Select the most recent file
$latest =
$directoryInfo.Files |
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 12
Thanks
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Your code will resolve to a collection. So you have to iterate the collection and call Session.GetFiles for each file in the collection. Are you doing this? Show us your complete code. A session log file would be useful too.
popotzo
[View user's profile]

Joined: 2015-05-14
Posts: 4
Thank you for the reply. It's the code from your website but instead of copying just 1 file I need to copy multiple files with the latest date stamp.

http://winscp.net/eng/docs/script_download_most_recent_file

param (
$localPath = "c:\downloaded\",
$remotePath = "/home/user/"
)

try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)

# Select the most recent file
$latest =
$directoryInfo.Files |
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1

# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}

# Download the selected file
$session.GetFiles($session.EscapeFileMask($remotePath + $latest.Name), $localPath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
dotps1
[View user's profile]
Contributor
Joined: 2015-03-23
Posts: 15
Location: United States
I got it to work by adding a Foreach Loop to the list of files:

Code:

Param ( $localPath = 'c:\downloaded\', $remotePath = '/home/user/' )

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path 'WinSCPnet.dll'

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = 'example.com'
    $sessionOptions.UserName = 'user'
    $sessionOptions.Password = 'mypassword'
    $sessionOptions.SshHostKeyFingerprint = 'ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx'

    $session = New-Object -TypeName WinSCP.Session

    try
    {
        # Connect
        $session.Open($sessionOptions)

        # Get list of files in the directory
        $directoryInfo = $session.ListDirectory($remotePath)

        # Select the most recent file
        $latest = $directoryInfo.Files | Where-Object { -Not $_.IsDirectory } | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 12

        # Any file at all?
        if ($latest -eq $null)
        {
            Write-Output -InputObject "No file found"
            exit 1
        }

        # Download the selected file
        # $session.GetFiles($session.EscapeFileMask($remotePath + $latest.Name), $localPath).Check()

        # Download list of files in foreach loop
        foreach ($file in $latest)
        {
            $session.GetFiles("$remotePath/$file", $localPath)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch [Exception]
{
    Write-Error $_.Exception.Message
    exit 1
}


because what is happing is your GetFiles command looks like this:
Code:

$session.GetFiles($session.EscapeFileMask(/home/user/file1.txt file2.txt file3.text file4.txt), $localPath).Check()

and im sure that is not what you where going for.

Check out http://winscp.net/eng/docs/library_session_getfiles, the RemotePath Property takes an object of Type [String] not [String[]].

Last edited by dotps1 on 2015-05-21 18:32; edited 1 time in total
popotzo
[View user's profile]

Joined: 2015-05-14
Posts: 4
Thanks dotps1! It's working now but the script is adding %5C in front of each file name.

Any idea or suggestion?
dotps1
[View user's profile]
Contributor
Joined: 2015-03-23
Posts: 15
Location: United States
popotzo wrote:
Thanks dotps1! It's working now but the script is adding %5C in front of each file name.

Any idea or suggestion?


Sorry, i forgot that $directoryInfo.Files already only returns the file name, so, you should beable to remove the .Name from $($file.Name) in the GetFiles().

Code:

         foreach ($file in $latest)
         {
             $session.GetFiles("$remotePath/$file", $localPath)
         }


i edited the original post as well.
see if that fixes it.

thanks.
popotzo
[View user's profile]

Joined: 2015-05-14
Posts: 4
Perfect! Thanks a lot dotps1!
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License