Berikut ini saya berikan contoh skenario jika Anda ingin membuat sebuah program untuk Download atau Remote file dari SAP Directories (Tcode AL11) ke local directories memakai bahasa pemrograman C#.
Jadi, Pertama Anda buat dulu program untuk generate file yang akan Anda simpan di SAP Directories, Ketika sudah di generate dan di Folder SAP Directories Anda sudah ada file tersebut maka langkah selanjutnya adalah remote akses ke SFTP SAP Directories Anda, disini saya menggunakan bahasa pemrograman C# dengan library Senchi SFTP.
public static void DownloadAll2() { int counterdata = 0; string host = VERSI01.Properties.Settings.Default.HOST; int port = 22; string username = VERSI01.Properties.Settings.Default.USERNAME; string password = VERSI01.Properties.Settings.Default.PASSWORD; string remoteDirectory = VERSI01.Properties.Settings.Default.REMOTEDIR; string localDirectory = VERSI01.Properties.Settings.Default.LOCALDIR; KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(username); PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(username, password); kauth.AuthenticationPrompt += new EventHandler<renci.sshnet.common.authenticationprompteventargs>(HandleKeyEvent); ConnectionInfo connectionInfo = new ConnectionInfo(host, port, username, pauth, kauth); string cs = @"server=localhost;userid=root;password=;database=interface"; using (var sftp = new SftpClient(connectionInfo)) { WriteConsole("Connecting to " + host + " as " + username); sftp.Connect(); WriteConsole("Connected!"); var files = sftp.ListDirectory(remoteDirectory); counterdata = 0; foreach (var file in files) { string remoteFileName = file.Name; if(remoteFileName.Contains(".SAPBR")) { counterdata = counterdata + 1; WriteConsole(remoteFileName); using (Stream file1 = File.OpenWrite(string.Format("{0}{1}", localDirectory, remoteFileName))) { WriteConsole("Download " + remoteDirectory + remoteFileName); sftp.DownloadFile(remoteDirectory + remoteFileName, file1); if (VERSI01.Properties.Settings.Default.DELETEAFTERDOWNLOAD == "X") sftp.DeleteFile(remoteDirectory + remoteFileName); } // Read file using StreamReader. Reads file line by line WriteConsole(string.Format("ReadFile {0}{1}", localDirectory, remoteFileName)); using (StreamReader fileRead = new StreamReader(string.Format("{0}{1}", localDirectory, remoteFileName))) { int counter = 0; string ln; while ((ln = fileRead.ReadLine()) != null) { using (MySqlConnection koneksi = new MySqlConnection(cs)) { koneksi.Open(); MySqlCommand cmd = new MySqlCommand("INSERT INTO `data` (REQ) VALUES ('"+ln+"') ", koneksi); cmd.ExecuteNonQuery(); koneksi.Close(); } Console.WriteLine(ln); counter++; } fileRead.Close(); } } } WriteConsole("Done!"); } } </renci.sshnet.common.authenticationprompteventargs> |
Pada contok kode diatas berguna untuk remote SFTP SAP Directories & Download file ke local dengan Extension file SAPBR, setelah download di local event selanjutnya akan menyimpan isi dari File tersebut ke MySQL.
Jangan lupa Anda buat setting dulu di Visual Studionya :
Semoga bermanfaat.