Im using WinSCP with PowerShell-script to download a couple of folders with files in it.
/remotepath/folder1/file1.xml
/remotepath/folder1/file1.pdf
/remotepath/folder2/file2.xml
/remotepath/folder2/file2.pdf
... and so on
Im running the command twice. First run to download the complete folders including the files. The second run with filemask and "true" parameter, to only delete the files in the remote folders. And don't know any better way but I think it should work...
The second run is the one not working fine, as it tries to delete the folders (what's not allowed per accessrights) and then the script breaks after the first folder.
The filter itself is working fine, it deltes xml or pdfs and ignores other files
# Load WinSCP .NET assembly
Add-Type -Path "D:\Script\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "server"
UserName = "user"
Password = "pass"
SshHostKeyFingerprint = "ssh-rsa 2048 lalalala"
}
$session = New-Object WinSCP.Session
$session2 = New-Object WinSCP.Session
$session2.SessionLogPath = "D:\Script\log\sessionlog.txt"
# Connect
$session.Open($sessionOptions)
$session2.Open($sessionOptions)
#Transfer Options
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
# Download files
$session.GetFiles("/remotePath/*", "D:\localPath\", $False, $transferOptions)
#Transfer Options2
$transferOptions2 = New-Object WinSCP.TransferOptions
$transferOptions2.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions2.FileMask = "*.xml,*.pdf"
# Delete *.xml
$session2.GetFiles("/remotePath/*", "D:\localPath\", $True, $transferOptions2)
> 2022-06-23 11:48:26.811 Script: get -delete -nopermissions -preservetime -transfer="binary" -filemask="*.xml,*.pdf" -- "/RemotePath/*" "C:\LocalPath\"
. 2022-06-23 11:48:26.811 Listing directory "/RemotePath".
> 2022-06-23 11:48:26.811 Type: SSH_FXP_OPENDIR, Size: 42, Number: 267
< 2022-06-23 11:48:26.826 Type: SSH_FXP_HANDLE, Size: 41, Number: 267
> 2022-06-23 11:48:26.826 Type: SSH_FXP_READDIR, Size: 41, Number: 524
< 2022-06-23 11:48:26.826 Type: SSH_FXP_NAME, Size: 283, Number: 524
> 2022-06-23 11:48:26.842 Type: SSH_FXP_READDIR, Size: 41, Number: 780
. 2022-06-23 11:48:26.842 Reading symlink ".".
> 2022-06-23 11:48:26.842 Type: SSH_FXP_READLINK, Size: 44, Number: 1043
> 2022-06-23 11:48:26.842 Type: SSH_FXP_STAT, Size: 48, Number: 1297
< 2022-06-23 11:48:26.842 Type: SSH_FXP_STATUS, Size: 42, Number: 780
. 2022-06-23 11:48:26.842 Storing reserved response
< 2022-06-23 11:48:26.842 Type: SSH_FXP_NAME, Size: 82, Number: 1043
. 2022-06-23 11:48:26.842 Link resolved to "/../../applications/.../remotepath".
< 2022-06-23 11:48:26.857 Type: SSH_FXP_ATTRS, Size: 59, Number: 1297
> 2022-06-23 11:48:26.857 Type: SSH_FXP_CLOSE, Size: 41, Number: 1540
. 2022-06-23 11:48:26.857 .;L;0;2022-04-26T14:02:18.000Z;3;"lalala" [0];"lala" [0];rwxrwxrwx;1
. 2022-06-23 11:48:26.857 ..;D;0;2022-05-26T22:57:16.000Z;3;"lalala" [0];"lala" [0];rwxr-xr-x;0
. 2022-06-23 11:48:26.857 Folder1;D;0;2022-06-23T09:33:32.000Z;3;"lalala" [0];"lala" [0];rwxr-xr-x;0
. 2022-06-23 11:48:26.857 20220620-012887;D;0;2022-06-23T08:34:09.000Z;3;"lalala" [0];"lala" [0];rwxr-xr-x;0
. 2022-06-23 11:48:26.857 Copying 2 files/directories to local directory "C:\LocalPath\"
. 2022-06-23 11:48:26.857 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask:
. 2022-06-23 11:48:26.857 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: *.xml,*.pdf; ResumeL: 0
. 2022-06-23 11:48:26.857 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2022-06-23 11:48:26.857 File: '/RemotePath/Folder1' [2022-06-23T09:33:32.000Z] [0]
. 2022-06-23 11:48:26.857 Listing directory "/RemotePath/Folder1".
> 2022-06-23 11:48:26.857 Type: SSH_FXP_OPENDIR, Size: 58, Number: 1803
< 2022-06-23 11:48:26.873 Type: SSH_FXP_STATUS, Size: 17, Number: 1540
. 2022-06-23 11:48:26.873 Discarding reserved response
< 2022-06-23 11:48:26.873 Type: SSH_FXP_HANDLE, Size: 41, Number: 1803
> 2022-06-23 11:48:26.873 Type: SSH_FXP_READDIR, Size: 41, Number: 2060
< 2022-06-23 11:48:26.904 Type: SSH_FXP_NAME, Size: 293, Number: 2060
> 2022-06-23 11:48:26.904 Type: SSH_FXP_READDIR, Size: 41, Number: 2316
. 2022-06-23 11:48:26.904 Reading symlink "..".
> 2022-06-23 11:48:26.904 Type: SSH_FXP_READLINK, Size: 43, Number: 2579
> 2022-06-23 11:48:26.904 Type: SSH_FXP_STAT, Size: 47, Number: 2833
< 2022-06-23 11:48:26.920 Type: SSH_FXP_STATUS, Size: 42, Number: 2316
. 2022-06-23 11:48:26.920 Storing reserved response
< 2022-06-23 11:48:26.920 Type: SSH_FXP_NAME, Size: 82, Number: 2579
. 2022-06-23 11:48:26.920 Link resolved to "/../../applications/.../remotepath".
< 2022-06-23 11:48:26.920 Type: SSH_FXP_ATTRS, Size: 59, Number: 2833
> 2022-06-23 11:48:26.920 Type: SSH_FXP_CLOSE, Size: 41, Number: 3076
. 2022-06-23 11:48:26.920 .;D;0;2022-06-23T09:33:32.000Z;3;"lalala" [0];"lala" [0];rwxr-xr-x;0
. 2022-06-23 11:48:26.920 ..;L;0;2022-04-26T14:02:18.000Z;3;"lalala" [0];"lala" [0];rwxrwxrwx;1
. 2022-06-23 11:48:26.920 test.pdf;-;10784;2022-06-23T09:33:32.000Z;3;"lalala" [0];"lala" [0];rw-r--r--;0
. 2022-06-23 11:48:26.920 File123.xml;-;10784;2022-06-23T09:33:32.000Z;3;"lalala" [0];"lala" [0];rw-r--r--;0
. 2022-06-23 11:48:26.920 File: '/RemotePath/Folder1/test.pdf' [2022-06-23T09:33:32.000Z] [10784]
. 2022-06-23 11:48:26.920 Copying "/RemotePath/Folder1/test.pdf" to local directory started.
. 2022-06-23 11:48:26.920 Binary transfer mode selected.
. 2022-06-23 11:48:26.920 Opening remote file.
> 2022-06-23 11:48:26.920 Type: SSH_FXP_OPEN, Size: 80, Number: 3331
< 2022-06-23 11:48:26.936 Type: SSH_FXP_STATUS, Size: 17, Number: 3076
. 2022-06-23 11:48:26.936 Discarding reserved response
< 2022-06-23 11:48:26.967 Type: SSH_FXP_HANDLE, Size: 41, Number: 3331
> 2022-06-23 11:48:26.967 Type: SSH_FXP_FSTAT, Size: 45, Number: 3592
< 2022-06-23 11:48:26.982 Type: SSH_FXP_ATTRS, Size: 67, Number: 3592
. 2022-06-23 11:48:26.982 Confirming overwriting of file.
> 2022-06-23 11:48:26.982 Type: SSH_FXP_READ, Size: 53, Number: 3845
. 2022-06-23 11:48:27.014 Storing reserved response
< 2022-06-23 11:48:27.014 Status code: 1
. 2022-06-23 11:48:27.014 5 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2022-06-23 11:48:27.014 Type: SSH_FXP_CLOSE, Size: 41, Number: 4612
. 2022-06-23 11:48:27.014 1 requests to fill 1 data gaps were issued.
. 2022-06-23 11:48:27.014 Preserving timestamp [2022-06-23T09:33:32.000Z]
. 2022-06-23 11:48:27.014 Transfer done: '/RemotePath/Folder1/test.pdf' => 'C:\LocalPath\Folder1\test.pdf' [10514]
. 2022-06-23 11:48:27.014 Deleting file "/RemotePath/Folder1/test.pdf".
> 2022-06-23 11:48:27.014 Type: SSH_FXP_REMOVE, Size: 67, Number: 4877
< 2022-06-23 11:48:27.029 Type: SSH_FXP_STATUS, Size: 17, Number: 4612
. 2022-06-23 11:48:27.029 Discarding reserved response
< 2022-06-23 11:48:27.029 Type: SSH_FXP_STATUS, Size: 17, Number: 4877
< 2022-06-23 11:48:27.029 Status code: 0
. 2022-06-23 11:48:27.029 File: '/RemotePath/Folder1/File123.xml' [2022-06-23T09:33:32.000Z] [10784]
. 2022-06-23 11:48:27.029 Copying "/RemotePath/Folder1/File123.xml" to local directory started.
. 2022-06-23 11:48:27.029 Binary transfer mode selected.
. 2022-06-23 11:48:27.029 Opening remote file.
> 2022-06-23 11:48:27.029 Type: SSH_FXP_OPEN, Size: 88, Number: 5123
< 2022-06-23 11:48:27.076 Type: SSH_FXP_HANDLE, Size: 41, Number: 5123
> 2022-06-23 11:48:27.076 Type: SSH_FXP_FSTAT, Size: 45, Number: 5384
< 2022-06-23 11:48:27.092 Type: SSH_FXP_ATTRS, Size: 67, Number: 5384
. 2022-06-23 11:48:27.092 Confirming overwriting of file.
> 2022-06-23 11:48:27.092 Type: SSH_FXP_READ, Size: 53, Number: 5637
. 2022-06-23 11:48:27.107 Storing reserved response
< 2022-06-23 11:48:27.107 Status code: 1
. 2022-06-23 11:48:27.107 5 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2022-06-23 11:48:27.107 Type: SSH_FXP_CLOSE, Size: 41, Number: 6404
. 2022-06-23 11:48:27.107 1 requests to fill 1 data gaps were issued.
. 2022-06-23 11:48:27.107 Preserving timestamp [2022-06-23T09:33:32.000Z]
. 2022-06-23 11:48:27.107 Transfer done: '/RemotePath/Folder1/File123.xml' => 'C:\LocalPath\Folder1\File123.xml' [10514]
. 2022-06-23 11:48:27.107 Deleting file "/RemotePath/Folder1/File123.xml".
> 2022-06-23 11:48:27.107 Type: SSH_FXP_REMOVE, Size: 75, Number: 6669
< 2022-06-23 11:48:27.139 Type: SSH_FXP_STATUS, Size: 17, Number: 6404
. 2022-06-23 11:48:27.139 Discarding reserved response
< 2022-06-23 11:48:27.139 Type: SSH_FXP_STATUS, Size: 17, Number: 6669
< 2022-06-23 11:48:27.139 Status code: 0
. 2022-06-23 11:48:27.139 Deleting file "/RemotePath/Folder1".
> 2022-06-23 11:48:27.139 Type: SSH_FXP_RMDIR, Size: 58, Number: 6927
< 2022-06-23 11:48:27.154 Type: SSH_FXP_STATUS, Size: 34, Number: 6927
< 2022-06-23 11:48:27.154 Status code: 3, Message: 6927, Server: Permission denied, Language:
. 2022-06-23 11:48:27.154 Asking user:
. 2022-06-23 11:48:27.154 Error deleting file '/RemotePath/Folder1'. ("Permission denied.
. 2022-06-23 11:48:27.154 Error code: 3
. 2022-06-23 11:48:27.154 Error message from server: Permission denied")
< 2022-06-23 11:48:27.154 Script: Error deleting file '/RemotePath/Folder1'.
< 2022-06-23 11:48:27.154 Script: Permission denied.
< 2022-06-23 11:48:27.154 Error code: 3
< 2022-06-23 11:48:27.154 Error message from server: Permission denied
. 2022-06-23 11:48:27.154 Answer: Abort
. 2022-06-23 11:48:27.154 Script: Failed
I would be fine with two solutions, either the filemask starts working fine and ignores the folders or the script just continues on errors.