copy multiple files using ps script

Advertisement

popotzo
Joined:
Posts:
4

copy multiple files using ps script

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

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
27,253
Location:
Prague, Czechia

Re: copy multiple files using ps script

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.

Reply with quote

popotzo
Joined:
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.

https://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
}

Reply with quote

dotps1
Contributor
Joined:
Posts:
18
Location:
United States

Try a foreach loop for the .GetFiles method

I got it to work by adding a Foreach Loop to the list of files:

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:
$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 https://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

Reply with quote

dotps1
Contributor
Joined:
Posts:
18
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().

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

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

thanks.

Reply with quote

Advertisement

You can post new topics in this forum