Sample code for 30+ languages & platforms
PowerBuilder

Set a Remote File's POSIX Permissions

See more SFTP Examples

Demonstrates the Chilkat SFtp.SetPermissions method, which sets the POSIX permission bits of a remote item. The arguments are the remote path (or handle), whether the first argument is a handle, and the permission bits. Pass the permission bits only, not the file-type bits.

Background: This is the SFTP equivalent of chmod. Common values, written as octal, are 0644 for a normal file, 0600 for a private file, and 0755 for an executable or a directory. In languages without octal literals the same values must be given in decimal or hexadecimal (for example 0755 is decimal 493). Setting an executable bit after uploading a script is the classic use.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
integer li_IsHandle
integer li_Permissions

li_Success = 0

//  Demonstrates the SFtp.SetPermissions method, which sets the POSIX permission bits of a remote
//  item.  The 1st argument is the remote path (or handle), the 2nd (isHandle) indicates which,
//  and the 3rd is the permission bits.

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Connect, authenticate, and initialize the SFTP subsystem.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Normally you would not hard-code the password in source.  You should instead obtain it
//  from an interactive prompt, environment variable, or a secrets vault.
ls_Password = "mySshPassword"

li_Success = loo_Sftp.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  The 1st argument is a path, so isHandle is 0.
li_IsHandle = 0

//  Pass the permission bits, not the file-type bits.  0755 gives the owner read/write/execute and
//  read/execute to group and others -- a common mode for an executable script.
li_Permissions = 493
li_Success = loo_Sftp.SetPermissions("subdir/script.sh",li_IsHandle,li_Permissions)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

Write-Debug "Permissions set."

loo_Sftp.Disconnect()


destroy loo_Sftp