S031 sftp - Download a file throught SFTP

#################################;
# Download a file throught sFTP #;
#################################;

#Create the script to download;
script create post "demo.file.sftp.download" false 1 
  (param
  	(var "[remoteDirectory]" {true} "The remote directory" is_null:false is_empty:false "/Users/jimmitry/Desktop")
  	(var "[filter]" {true} "The file filter from the remote directory" is_null:false is_empty:false "*png")
  	(var "[localDirectory]" {true} "The local directory" is_null:false is_empty:false "/Users/jimmitry/Desktop/ftp")
  ;) 
  "Download file through SFTP" 
{

	try {

		#Connect to the SFTP server;
		sftp connect "session1" {cm get "demo_cm_sftp";};

		#Move to the remote directory;
		sftp cd "session1" [remoteDirectory];

		#Get all remote files;
		json load "files" (sftp ls "session1" [filter]);

		#Get all files;
		-> "[nbFiles]" (json count "files" "/");
		for (-> "[i]" 0) (< [i] [nbFiles]) (++ "[i]") {

			-> "[filename]" (json select "files" (concat "/[" [i] "]/name"));
		
			sftp get "session1" [filename] (concat [localDirectory] "/" [filename]);
		
		};

		#Disconnect the session;
		sftp disconnect "session1";
	
	} {
	
		#Close objects;
		try {sftp disconnect "session1";} {} "[sub_err]";

		#Generate an error;
		exception (1) ([err]);
	
	} "[err]";
	
	
} "Return nothing";

#Execute the script;
execute "demo.file.sftp.download.post"
	"[remoteDirectory]" "/Users/jimmitry/Desktop"
	"[filter]" "*png"
	"[localDirectory]" "/Users/jimmitry/Desktop/ftp"
;


MentDB © 2012 - 2020 - Legal Notice