![]() ![]() Note that this does not include a fix for the $localPath, as I'm not sure, what the path C:\users\user\desktop\file actually mean. $session.MoveFile($transfer.FileName, $remotePath2) $transferResult = $session.GetFiles(($remotePath $fileName), $localPath)įoreach ($transfer in $transferResult.Transfers) ![]() The code should look like (for the first set of files only, i.e. This way you avoid race condition, where you download set of files, new files are added (which you didn't download) and you incorrectly move them to the "completed" folder. Actually the present implementation allows a use of the file mask, but this use is undocumented and may be deprecated in future versions.Īnyway, the best solution is to iterate the list of actually downloaded files, as returned by the GetFiles and move the files one by one. The MoveFile (note the singular, comparing to the GetFiles), moves only a single file. While I'm no PowerShell expert, I'd actually say you are omitting the second argument of the method completely this way.įurther, note that the MoveFile method does not return anything (contrary to the GetFiles). You have brackets incorrectly around both arguments, instead of around the first argument only. You are downloading all files (both *.txt and *.log) to the folder C:\users\user\desktop and save them all to the same name file, overwriting one another. Your current code fails, because you are renaming the file to a name of an already existing folder. For that use the target path /complete/ (or the /complete/* to make it more obvious). While you probably want to move the file to folder the /complete, and keep its name. So, if you use the target path /complete, you are trying to move the file to a root folder and rename it to the complete. The targetPath argument of the Session.MoveFile method is a path to move/rename the file to. $session.MoveFile(($remotePath $fileNamee, $remotePath2)).Check() $session.MoveFile(($remotePath $fileName, $remotePath2)).Check() $session.GetFiles(($remotePath $fileNamee), $localPath).Check() $session.GetFiles(($remotePath $fileName), $localPath).Check() $directoryInfo = $session.ListDirectory($remotePath2) $directoryInfo = $session.ListDirectory($remotePath) $localPath = "C:\users\user\desktop\file" $sessionOptions = New-Object WinSCP.SessionOptions tryĪdd-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" So far I just keep getting errors like "cannot move "file" to "/file". But I need to move them to another directory on the server after the download. Session.GetFiles("/*", false, t).I have this script that downloads all. So the correct code is: TransferOptions t = new TransferOptions You should check for errors, either by inspecting the returned TransferOperationResult or by calling the. When downloading multiple files, the filename in the path should be replaced with operation mask or omitted ( path ends with backslash). Or overwriting the files one with another, if there's not.ĭocumentation for the localPath parameter of the GetFiles:įull path to download the file to. Failing, if there's already a directory with that name. Otherwise WinSCP will try to save all files to C:\ with name bla. So you have to tell WinSCP to download the files to the directory by appending a backslash: C:\bla\. I assume that the C:\bla is a target directory. To download all files in a directory, use mask *. Quoting the documentation for the remotePath parameter of the GetFiles:įull path to remote directory followed by slash and wildcard to select files or subdirectories to download. You need to ask to download all files in the root directory: /*. You ask WinSCP to download the root directory, yet to exclude all directories. TransferOptions.FileMask = "/home/user/", false, transferOptions) ![]() See also WinSCP FAQ How do I transfer directory non-recursively? TransferOptions transferOptions = new TransferOptions() Your code to exclude subdirectories is correct. ![]()
0 Comments
Leave a Reply. |