Download and Delete Script Issue

Advertisement

midnite8
Joined:
Posts:
2

Download and Delete Script Issue

I have success in downloading the files from source to destination but when I add in the -delete switch, I get a permissions error 3.
This script connects to a SFTP folder that is not connected to the domain so I can't add additional permissions to the folder for the user account that is executing the script.
Is there another way I can delete the files once downloaded?
Below is my script and error
@echo off
 
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\Temp\WinSCP Logs\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://user:password@secureftp.domain.com/ -hostkey=""ssh-rsa 2048 key""" ^
    "cd /" ^
    "lcd H:\folder" ^
    "get -delete ""*.*""" ^
    "exit"
 
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
) else (
  echo Error
)
 
exit /b %WINSCP_RESULT%
. 2023-03-30 11:35:21.510 Copying 45 files/directories to local directory "H:\Folder" - total size: 972,451
. 2023-03-30 11:35:21.510   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: 
. 2023-03-30 11:35:21.510   TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: ; ResumeL: 0
. 2023-03-30 11:35:21.510   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2023-03-30 11:35:21.510 File: '/File.csv' [2023-03-23T16:25:10.000Z] [6200]
. 2023-03-30 11:35:21.510 Copying "/file.csv" to local directory started.
. 2023-03-30 11:35:21.510 Binary transfer mode selected.
. 2023-03-30 11:35:21.510 Opening remote file.
> 2023-03-30 11:35:21.510 Type: SSH_FXP_OPEN, Size: 48, Number: 2051
< 2023-03-30 11:35:21.511 Type: SSH_FXP_STATUS, Size: 31, Number: 1548
. 2023-03-30 11:35:21.511 Discarding reserved response
< 2023-03-30 11:35:21.511 Type: SSH_FXP_STATUS, Size: 27, Number: 1796
. 2023-03-30 11:35:21.511 Discarding reserved response
< 2023-03-30 11:35:21.525 Type: SSH_FXP_HANDLE, Size: 17, Number: 2051
> 2023-03-30 11:35:21.525 Type: SSH_FXP_FSTAT, Size: 21, Number: 2312
< 2023-03-30 11:35:21.527 Type: SSH_FXP_ATTRS, Size: 18, Number: 2312
. 2023-03-30 11:35:21.528 Confirming overwriting of file.
> 2023-03-30 11:35:21.529 Type: SSH_FXP_READ, Size: 29, Number: 2565
< 2023-03-30 11:35:21.531 Status code: 1
. 2023-03-30 11:35:21.531 3 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2023-03-30 11:35:21.531 Type: SSH_FXP_CLOSE, Size: 17, Number: 3076
. 2023-03-30 11:35:21.531 Preserving timestamp [2023-03-23T16:25:10.000Z]
. 2023-03-30 11:35:21.531 Transfer done: '/File.csv' => 'H:\Folder\File.csv' [6200]
. 2023-03-30 11:35:21.531 Deleting file "/File.csv".
> 2023-03-30 11:35:21.531 Type: SSH_FXP_REMOVE, Size: 35, Number: 3341
< 2023-03-30 11:35:21.534 Type: SSH_FXP_STATUS, Size: 37, Number: 3341
< 2023-03-30 11:35:21.534 Status code: 3, Message: 3341, Server: Permission denied., Language: en 
. 2023-03-30 11:35:21.534 Asking user:
. 2023-03-30 11:35:21.534 Error deleting file '/File.csv'. ("Permission denied.
. 2023-03-30 11:35:21.534 Error code: 3
. 2023-03-30 11:35:21.534 Error message from server (en): Permission denied.")
< 2023-03-30 11:35:21.534 Script: Error deleting file '/File.csv'.
< 2023-03-30 11:35:21.534 Script: Permission denied.
< 2023-03-30 11:35:21.534 Error code: 3
< 2023-03-30 11:35:21.534 Error message from server (en): Permission denied.
. 2023-03-30 11:35:21.535 Answer: Abort
. 2023-03-30 11:35:21.535 Script: Failed
. 2023-03-30 11:35:21.535 Script: Exit code: 1
. 2023-03-30 11:35:21.535 Closing connection.
. 2023-03-30 11:35:21.535 Sending special code: 1
. 2023-03-30 11:35:21.545 Main session channel closed
. 2023-03-30 11:35:21.545 All channels closed

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,984
Location:
Prague, Czechia

Re: Download and Delete Script Issue

Well, it looks like you do not have permissions on the server to delete the file in that folder. If you do not, nothing in the script will help you. You need to get the permissions first. Or can you delete the files in WinSCP GUI? Or in any other SFTP client?

On common Linux systems, you need write permission to the folder to be able to delete files in the folder.

Reply with quote

Advertisement

You can post new topics in this forum