Topic "Output from session.Putfiles eating 1 line from every dir!"

Author Message
Bix
[View user's profile]

Joined: 2014-11-03
Posts: 2
Location: Brussels, Belgium
Greetings,

First post here, I have searched the forums for this error but didn't find it. It concerns WinSCP with PowerShell so maybe it should be moved to the Scripting / Automation forum, but as I consider it a bug, I leave it here for the moment.

On a Win2008R2 SP1 server, I am using the WinSCP GUI to upload files to an sFTP server with a private key (PPK file) and it works perfectly.

Now I would like to script these uploads with PowerShell, and I am using the PowerShell Wrapper
On this page : http://winscp.net/eng/docs/library_powershell#module, I follow the link to download the wrapper. I include it successfully in PowerShell, and manage to use the code given in the "Example" chapter on that same page. It uploads the files just fine (I can check separately with the GUI) but the output (that I need to use for logging purposes) is bugging in this way:

The directory D:\TOUPLOAD contains the following folders & files:
Quote:
D:\TOUPLOAD\TESTDIR01
D:\TOUPLOAD\TESTDIR01\testfile01a.txt
D:\TOUPLOAD\TESTDIR01\testfile01b.txt
D:\TOUPLOAD\TESTDIR01\testfile01c.txt
D:\TOUPLOAD\TESTDIR02
D:\TOUPLOAD\TESTDIR02\testfile02a.txt
D:\TOUPLOAD\TESTDIR03\testfile02b.txt
D:\TOUPLOAD\TESTDIR03\testfile02c.txt
D:\TOUPLOAD\TESTDIR03
D:\TOUPLOAD\TESTDIR03\testfile03a.txt
D:\TOUPLOAD\TESTDIR03\testfile03b.txt
D:\TOUPLOAD\TESTDIR03\testfile03c.txt

I use the "D:\TOUPLOAD\*" parameter for the PutFiles method, just like in the Example.
All files are uploaded to the server, where the until-then-non-existing directories are created, with all files put accordingly, everything is actually working fine, I can confirm with the GUI!

BUT the output from the script is the following:
Quote:
Upload of D:\TOUPLOAD\TESTDIR01\testfile01a.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR01\testfile01b.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR02\testfile02a.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR02\testfile02b.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR03\testfile03a.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR03\testfile03b.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR03\testfile03c.txt succeeded

Look, the last file of all sub-directories but the last one is missing from the log.

I first thought it might be some PowerShell variable management issue but then I checked the content of the variable:
Code:
$transferResult.Transfers

And the same files are missing.

Good question is: what happens if there is only one file in the directories? Well it gets eaten too ! Only the file(s) from the last directory show up in the output.

I can reproduce the issue at will, with any number of directories or files. Always the last file from all directories but the last directory, is missing.

This is really embarrassing when I need to use that output for other reasons afterwards (like moving files) so can you indicate me how to correct this or at least acknowledge it's a bug, 'cause I'm becoming crazy here Smile

Summary :
  • Open-WinSCPSession outputs a Session object that we'll call $session.
  • The method $session.Putfiles (with correct parameters) uploads files to an sFTP server, and outputs a TransferOperationResult object (BaseType WinSCP.OperationResultBase). Let's call it $transfertResult
  • The property $transferResult.Transfers is a TransferEventArgsCollection , which has a FileName property.
  • I can find all uploaded files in there, except the last one of every directory, but the last directory (which has all its files).
Bix
[View user's profile]

Joined: 2014-11-03
Posts: 2
Location: Brussels, Belgium
OK I managed to build a workaround.

Knowing that my directory structure is fixed (One root dir, a big amount of sub-dirs containing files, but no level deeper), I added the following:

Code:
 # $uploadDir_target & $transferOptions are defined previously in the code
$rootCI = gci "d:\TOUPLOAD"   # will contain all subdirs

        foreach ($ci in $rootCI)
        {
         $transferResult = $session.PutFiles($ci.FullName, $uploadDir_target, $False, $transferOptions)
          $transferResult.Check()

         foreach ($transfer in $transferResult.Transfers)
         {
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
          }
        }


Which is basically the same as the original script except I browse directory per directory, and as there is no sub-dir in those but only files, it works.

Still, this is very unstable, not portable, and if our directory structure ever goes deeper, I'm still in a situation (or I can do more get-childitem but this it not clean).

So even if for now I get what I want, the problem persists! Mr. Green
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24555
Location: Prague, Czechia
Thanks for your post.

This bug has been added to the tracker:
http://winscp.net/tracker/show_bug.cgi?id=1238

I'm sending you an email with a dev version of WinSCP to address you have used to register on this forum.
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