UDF to support SFTP protocol using PSFTP


SUBMITTED BY: Guest

DATE: May 29, 2013, 7:06 a.m.

FORMAT: Text only

SIZE: 55.9 kB

HITS: 2272

  1. ; !!! VERSION 1.0 BETA 9 - NOT COMPLETE !!!
  2. #include-once
  3. #include <Constants.au3>
  4. #include <Date.au3>
  5. #include <String.au3>
  6. ; #INDEX# =======================================================================================================================
  7. ; Title .........: SFTP
  8. ; AutoIt Version : 3.3.8.0++
  9. ; Language ......: English
  10. ; Description ...: Functions that assist with SFTP using psftp from PuTTY package.
  11. ; Author(s) .....: Lupo73
  12. ; Notes .........: Function names and parameters inspired by FTPEx.au3
  13. ; Exe(s) ........: psftp.exe
  14. ; ===============================================================================================================================
  15. ; #VARIABLES# ===================================================================================================================
  16. Global $__gsLocalDir_SFTP, $__gsRemoteDir_SFTP
  17. ; ===============================================================================================================================
  18. ; #CURRENT# =====================================================================================================================
  19. ;_SFTP_Close
  20. ;_SFTP_Connect
  21. ;_SFTP_DirCreate
  22. ;_SFTP_DirDelete
  23. ;_SFTP_DirGetContents
  24. ;_SFTP_DirGetCurrent
  25. ;_SFTP_DirGetCurrentLocal
  26. ;_SFTP_DirPutContents
  27. ;_SFTP_DirSetCurrent
  28. ;_SFTP_DirSetCurrentLocal
  29. ;_SFTP_FileDelete
  30. ;_SFTP_FileExists
  31. ;_SFTP_FileGet
  32. ;_SFTP_FileGetInfo
  33. ;_SFTP_FileGetSize
  34. ;_SFTP_FileMove
  35. ;_SFTP_FilePut
  36. ;_SFTP_ListToArray
  37. ;_SFTP_ListToArrayEx ; <<<<<<<<<<< time not updated with timezone offset
  38. ;_SFTP_Open
  39. ;_SFTP_ProgressDownload ; <<<<<<<<<<< not complete
  40. ;_SFTP_ProgressUpload
  41. ; ===============================================================================================================================
  42. ; #INTERNAL_USE_ONLY#============================================================================================================
  43. ;__MonthToNumber
  44. ;__WinAPI_GetFullPathName
  45. ; ===============================================================================================================================
  46. ; #FUNCTION# ====================================================================================================================
  47. ; Name...........: _SFTP_Close
  48. ; Description ...: Closes the _SFTP_Open session.
  49. ; Syntax.........: _SFTP_Close ( $hSession )
  50. ; Parameters ....: $hSession - as returned by _SFTP_Open().
  51. ; Return values .: Success - 1
  52. ; Failure - 0, sets @error
  53. ; |1 - Fails to close the session
  54. ; Author ........: Lupo73, IgneusJotunn
  55. ; Modified.......:
  56. ; Remarks .......:
  57. ; Related .......: _SFTP_Open
  58. ; Link ..........:
  59. ; Example .......: No
  60. ; ===============================================================================================================================
  61. Func _SFTP_Close($hSession)
  62. If ProcessExists($hSession) = 0 Then
  63. Return 1
  64. EndIf
  65. StdinWrite($hSession, "bye")
  66. ProcessClose($hSession)
  67. If @error Then
  68. Return SetError(1, 0, 0)
  69. EndIf
  70. Return 1
  71. EndFunc ; ==>_SFTP_Close
  72. ; #FUNCTION# ====================================================================================================================
  73. ; Name...........: _SFTP_Connect
  74. ; Description ...: Connects to a SFTP server.
  75. ; Syntax.........: _SFTP_Connect ( $hSession, $sServerName [, $sUsername = "" [, $sPassword = "" [, $iServerPort = 0]]] )
  76. ; Parameters ....: $hSession - as returned by _SFTP_Open().
  77. ; $sServerName - Server name/ip.
  78. ; $sUsername - Optional, Username.
  79. ; $sPassword - Optional, Password.
  80. ; $iServerPort - Optional, Server port ( 0 is default (22) )
  81. ; Return values .: Success - Returns an identifier
  82. ; Failure - 0, sets @error
  83. ; |1 - The session is closed
  84. ; |2 - Access denied
  85. ; |3 - Other error
  86. ; Author ........: Lupo73, trainer
  87. ; Modified.......:
  88. ; Remarks .......:
  89. ; Related .......: _SFTP_Open
  90. ; Link ..........:
  91. ; Example .......: No
  92. ; ===============================================================================================================================
  93. Func _SFTP_Connect($hSession, $sServerName, $sUsername = "", $sPassword = "", $iServerPort = 0)
  94. If ProcessExists($hSession) = 0 Then
  95. Return SetError(1, 0, 0)
  96. EndIf
  97. If $iServerPort = 0 Then
  98. $iServerPort = ""
  99. EndIf
  100. Local $sLine, $sStringSplit, $iWaitKeySaving = 0, $bSaveKey = True
  101. StdinWrite($hSession, 'open ' & $sServerName & ' ' & $iServerPort & @CRLF)
  102. While 1
  103. $iWaitKeySaving += 1
  104. If $iWaitKeySaving >= 500 And $bSaveKey Then
  105. StdinWrite($hSession, 'y' & @CRLF)
  106. $bSaveKey = False
  107. EndIf
  108. $sLine = StdoutRead($hSession)
  109. If ProcessExists($hSession) = 0 Then
  110. Return SetError(1, 0, 0)
  111. ElseIf StringInStr($sLine, "psftp>") Then
  112. ExitLoop
  113. ElseIf StringInStr($sLine, "login as:") Then
  114. StdinWrite($hSession, $sUsername & @CRLF)
  115. While 1
  116. $sLine = StdoutRead($hSession)
  117. If ProcessExists($hSession) = 0 Then
  118. Return SetError(1, 0, 0)
  119. ElseIf StringInStr($sLine, "psftp>") Then
  120. ExitLoop 2
  121. ElseIf StringInStr($sLine, "password:") Then
  122. StdinWrite($hSession, $sPassword & @CRLF)
  123. ExitLoop 2
  124. EndIf
  125. Sleep(10)
  126. WEnd
  127. ElseIf $sLine <> "" Then
  128. Return SetError(3, 0, 0)
  129. EndIf
  130. Sleep(10)
  131. WEnd
  132. If $sLine <> "psftp>" Then ; Connection With User And Password.
  133. While 1
  134. $sLine = StdoutRead($hSession)
  135. If ProcessExists($hSession) = 0 Then
  136. Return SetError(1, 0, 0)
  137. ElseIf StringInStr($sLine, "psftp>") Then
  138. ExitLoop
  139. ElseIf StringInStr($sLine, "Access denied") Then
  140. Return SetError(2, 0, 0) ; The Password Is Required Again.
  141. EndIf
  142. Sleep(10)
  143. WEnd
  144. EndIf
  145. If StringInStr($sLine, "Remote working directory is") Then
  146. $sStringSplit = StringSplit($sLine, @CRLF)
  147. $__gsRemoteDir_SFTP = StringTrimLeft($sStringSplit[1], 28)
  148. EndIf
  149. If $__gsRemoteDir_SFTP = 0 Then
  150. $__gsRemoteDir_SFTP = _SFTP_DirGetCurrent($hSession)
  151. EndIf
  152. Return $hSession
  153. EndFunc ; ==>_SFTP_Connect
  154. ; #FUNCTION# ====================================================================================================================
  155. ; Name...........: _SFTP_DirCreate
  156. ; Description ...: Makes a Directory on a SFTP server.
  157. ; Syntax.........: _SFTP_DirCreate ( $hConnection, $sRemoteDir )
  158. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  159. ; $sRemoteDir - The remote Directory to create.
  160. ; Return values .: Success - 1
  161. ; Failure - 0, sets @error
  162. ; |1 - The connection is closed
  163. ; |2 - Creation probably failed because the Directory already exists
  164. ; |3 - Other error
  165. ; Author ........: Lupo73
  166. ; Modified.......:
  167. ; Remarks .......:
  168. ; Related .......: _SFTP_Connect
  169. ; Link ..........:
  170. ; Example .......: No
  171. ; ===============================================================================================================================
  172. Func _SFTP_DirCreate($hConnection, $sRemoteDir)
  173. If ProcessExists($hConnection) = 0 Then
  174. Return SetError(1, 0, 0)
  175. EndIf
  176. Local $sLine
  177. StdinWrite($hConnection, 'mkdir "' & $sRemoteDir & '"' & @CRLF)
  178. While 1
  179. $sLine = StdoutRead($hConnection)
  180. If ProcessExists($hConnection) = 0 Then
  181. Return SetError(1, 0, 0)
  182. ElseIf StringInStr($sLine, $sRemoteDir & ": OK") Then
  183. ExitLoop
  184. ElseIf StringInStr($sLine, $sRemoteDir & ": failure") Then
  185. Return SetError(2, 0, 0)
  186. ElseIf $sLine <> "" Then
  187. Return SetError(3, 0, 0)
  188. EndIf
  189. Sleep(10)
  190. WEnd
  191. Return 1
  192. EndFunc ; ==>_SFTP_DirCreate
  193. ; #FUNCTION# ====================================================================================================================
  194. ; Name...........: _SFTP_DirDelete
  195. ; Description ...: Deletes a Directory on a SFTP server.
  196. ; Syntax.........: _SFTP_DirDelete ( $hConnection, $sRemoteDir )
  197. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  198. ; $sRemoteDir - The remote Directory to be deleted.
  199. ; Return values .: Success - 1
  200. ; Failure - 0, sets @error
  201. ; |1 - The connection is closed
  202. ; |2 - Directory not found
  203. ; |3 - Directory probably contains not removable files
  204. ; |4 - Failed listing Directory
  205. ; |5 - Other error
  206. ; Author ........: Lupo73
  207. ; Modified.......:
  208. ; Remarks .......:
  209. ; Related .......: _SFTP_Connect, _SFTP_FileDelete, _SFTP_ListToArrayEx
  210. ; Link ..........:
  211. ; Example .......: No
  212. ; ===============================================================================================================================
  213. Func _SFTP_DirDelete($hConnection, $sRemoteDir)
  214. If ProcessExists($hConnection) = 0 Then
  215. Return SetError(1, 0, 0)
  216. EndIf
  217. Local $aFileList = _SFTP_ListToArrayEx($hConnection, $sRemoteDir)
  218. If @error Then
  219. Return SetError(4, 0, 0)
  220. EndIf
  221. If $aFileList[0][0] > 0 Then
  222. For $A = 1 To $aFileList[0][0]
  223. If StringLeft($aFileList[$A][2], 1) <> "d" Then
  224. _SFTP_FileDelete($hConnection, $sRemoteDir & "/" & $aFileList[$A][0])
  225. Else
  226. _SFTP_DirDelete($hConnection, $sRemoteDir & "/" & $aFileList[$A][0])
  227. EndIf
  228. If @error Then
  229. Return SetError(5, 0, 0)
  230. EndIf
  231. Next
  232. EndIf
  233. Local $sLine
  234. StdinWrite($hConnection, 'rmdir "' & $sRemoteDir & '"' & @CRLF)
  235. While 1
  236. $sLine = StdoutRead($hConnection)
  237. If ProcessExists($hConnection) = 0 Then
  238. Return SetError(1, 0, 0)
  239. ElseIf StringInStr($sLine, $sRemoteDir & ": OK") Then
  240. ExitLoop
  241. ElseIf StringInStr($sLine, "no such file or directory") Then
  242. Return SetError(2, 0, 0)
  243. ElseIf StringInStr($sLine, $sRemoteDir & ": failure") Then
  244. Return SetError(3, 0, 0)
  245. ElseIf $sLine <> "" Then
  246. Return SetError(5, 0, 0)
  247. EndIf
  248. Sleep(10)
  249. WEnd
  250. Return 1
  251. EndFunc ; ==>_SFTP_DirDelete
  252. ; #FUNCTION# ====================================================================================================================
  253. ; Name...........: _SFTP_DirGetContents
  254. ; Description ...: Get a folder from a SFTP server.
  255. ; Syntax.........: _SFTP_DirGetContents ( $hConnection, $sRemoteFolder [, $sLocalFolder = "" [, $fFailIfExists = False [, $fRecursiveGet = 1]]] )
  256. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  257. ; $sRemoteFolder - The remote folder i.e. "/website/home".
  258. ; $sLocalFolder - Optional, The local folder (or use the source name if not defined) i.e. "c:\temp".
  259. ; $fFailIfExists - Optional, True: do not overwrite existing (default = False)
  260. ; $fRecursiveGet - Optional, Recurse through sub-dirs: 0 = Non recursive, 1 = Recursive (default)
  261. ; Return values .: Success - 1
  262. ; Failure - 0, sets @error
  263. ; |1 - The connection is closed
  264. ; |2 - Local folder exists and $fFailIfExists = True
  265. ; |3 - Remote folder not found
  266. ; |4 - Other error
  267. ; Author ........: Lupo73
  268. ; Modified.......:
  269. ; Remarks .......:
  270. ; Related .......: _SFTP_Connect
  271. ; Link ..........:
  272. ; Example .......: No
  273. ; ===============================================================================================================================
  274. Func _SFTP_DirGetContents($hConnection, $sRemoteFolder, $sLocalFolder = "", $fFailIfExists = False, $fRecursiveGet = 1)
  275. If ProcessExists($hConnection) = 0 Then
  276. Return SetError(1, 0, 0)
  277. EndIf
  278. If $sLocalFolder <> "" Then
  279. $sLocalFolder = __WinAPI_GetFullPathName($sLocalFolder)
  280. If FileExists($sLocalFolder) Then
  281. If $fFailIfExists Then
  282. Return SetError(2, 0, 0)
  283. EndIf
  284. EndIf
  285. $sLocalFolder = ' "' & $sLocalFolder & '"'
  286. EndIf
  287. Local $sLine
  288. If $fRecursiveGet Then
  289. $sLine = '-r '
  290. EndIf
  291. StdinWrite($hConnection, 'get ' & $sLine & '-- "' & $sRemoteFolder & '"' & $sLocalFolder & @CRLF)
  292. While 1
  293. $sLine = StdoutRead($hConnection)
  294. If ProcessExists($hConnection) = 0 Then
  295. Return SetError(1, 0, 0)
  296. ElseIf StringInStr($sLine, "psftp>") Then
  297. ExitLoop
  298. ElseIf StringInStr($sLine, "=> local:") Then
  299. ContinueLoop
  300. ElseIf StringInStr($sLine, "no such file or directory") Then
  301. Return SetError(3, 0, 0)
  302. ElseIf $sLine <> "" Then
  303. Return SetError(4, 0, 0)
  304. EndIf
  305. Sleep(10)
  306. WEnd
  307. Return 1
  308. EndFunc ; ==>_SFTP_DirGetContents
  309. ; #FUNCTION# ====================================================================================================================
  310. ; Name...........: _SFTP_DirGetCurrent
  311. ; Description ...: Get current remote Directory of a SFTP connection.
  312. ; Syntax.........: _SFTP_DirGetCurrent ( $hConnection )
  313. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  314. ; Return values .: Success - Remote Directory
  315. ; Failure - 0, sets @error
  316. ; |1 - The connection is closed
  317. ; |2 - Other error
  318. ; Author ........: Lupo73
  319. ; Modified.......:
  320. ; Remarks .......:
  321. ; Related .......: _SFTP_Connect
  322. ; Link ..........:
  323. ; Example .......: No
  324. ; ===============================================================================================================================
  325. Func _SFTP_DirGetCurrent($hConnection)
  326. If ProcessExists($hConnection) = 0 Then
  327. Return SetError(1, 0, 0)
  328. EndIf
  329. If $__gsRemoteDir_SFTP <> 0 Then
  330. Return $__gsRemoteDir_SFTP
  331. EndIf
  332. Local $sLine
  333. StdinWrite($hConnection, 'pwd' & @CRLF)
  334. While 1
  335. $sLine = StdoutRead($hConnection)
  336. If ProcessExists($hConnection) = 0 Then
  337. Return SetError(1, 0, 0)
  338. ElseIf StringInStr($sLine, "Remote directory is") Then
  339. ExitLoop
  340. ElseIf $sLine <> "" Then
  341. Return SetError(2, 0, 0)
  342. EndIf
  343. Sleep(10)
  344. WEnd
  345. Return StringTrimLeft($sLine, 20)
  346. EndFunc ; ==>_SFTP_DirGetCurrent
  347. ; #FUNCTION# ====================================================================================================================
  348. ; Name...........: _SFTP_DirGetCurrentLocal
  349. ; Description ...: Get current local Directory of a SFTP connection.
  350. ; Syntax.........: _SFTP_DirGetCurrentLocal ( $hConnection )
  351. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  352. ; Return values .: Success - Local Directory
  353. ; Failure - 0, sets @error
  354. ; |1 - The connection is closed
  355. ; |2 - Other error
  356. ; Author ........: Lupo73
  357. ; Modified.......:
  358. ; Remarks .......:
  359. ; Related .......: _SFTP_Connect
  360. ; Link ..........:
  361. ; Example .......: No
  362. ; ===============================================================================================================================
  363. Func _SFTP_DirGetCurrentLocal($hConnection)
  364. If ProcessExists($hConnection) = 0 Then
  365. Return SetError(1, 0, 0)
  366. EndIf
  367. If $__gsLocalDir_SFTP <> 0 Then
  368. Return $__gsLocalDir_SFTP
  369. EndIf
  370. Local $sLine
  371. StdinWrite($hConnection, 'lpwd' & @CRLF)
  372. While 1
  373. $sLine = StdoutRead($hConnection)
  374. If ProcessExists($hConnection) = 0 Then
  375. Return SetError(1, 0, 0)
  376. ElseIf StringInStr($sLine, "Current local directory is") Then
  377. ExitLoop
  378. ElseIf $sLine <> "" Then
  379. Return SetError(2, 0, 0)
  380. EndIf
  381. Sleep(10)
  382. WEnd
  383. Return StringTrimLeft($sLine, 27)
  384. EndFunc ; ==>_SFTP_DirGetCurrentLocal
  385. ; #FUNCTION# ====================================================================================================================
  386. ; Name...........: _SFTP_DirPutContents
  387. ; Description ...: Puts a folder on a SFTP server. Recursively if selected.
  388. ; Syntax.........: _SFTP_DirPutContents ( $hConnection, $sLocalFolder [, $sRemoteFolder = "" [, $fRecursivePut = 1]]] )
  389. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  390. ; $sLocalFolder - The local folder i.e. "c:\temp".
  391. ; $sRemoteFolder - Optional, The remote folder (or use the source name if not defined) i.e. "/website/home".
  392. ; $fRecursivePut - Optional, Recurse through sub-dirs: 0 = Non recursive, 1 = Recursive (default)
  393. ; Return values .: Success - 1
  394. ; Failure - 0, sets @error
  395. ; |1 - The connection is closed
  396. ; |2 - Local folder does not exists
  397. ; |3 - Remote folder cannot be created
  398. ; |4 - Other error
  399. ; Author ........: Lupo73
  400. ; Modified.......:
  401. ; Remarks .......:
  402. ; Related .......: _SFTP_Connect
  403. ; Link ..........:
  404. ; Example .......: No
  405. ; ===============================================================================================================================
  406. Func _SFTP_DirPutContents($hConnection, $sLocalFolder, $sRemoteFolder = "", $fRecursivePut = 1)
  407. If ProcessExists($hConnection) = 0 Then
  408. Return SetError(1, 0, 0)
  409. EndIf
  410. If $sRemoteFolder <> "" Then
  411. $sRemoteFolder = ' "' & $sRemoteFolder & '"'
  412. EndIf
  413. Local $sLine
  414. If $fRecursivePut Then
  415. $sLine = '-r '
  416. EndIf
  417. StdinWrite($hConnection, 'put ' & $sLine & '-- "' & $sLocalFolder & '"' & $sRemoteFolder & @CRLF)
  418. While 1
  419. $sLine = StdoutRead($hConnection)
  420. If ProcessExists($hConnection) = 0 Then
  421. Return SetError(1, 0, 0)
  422. ElseIf StringInStr($sLine, "psftp>") Then
  423. ExitLoop
  424. ElseIf StringInStr($sLine, "=> remote:") Then
  425. ContinueLoop
  426. ElseIf StringInStr($sLine, "unable to open") Then
  427. Return SetError(2, 0, 0)
  428. ElseIf StringInStr($sLine, "Cannot create directory") Then
  429. Return SetError(3, 0, 0)
  430. ElseIf $sLine <> "" Then
  431. Return SetError(4, 0, 0)
  432. EndIf
  433. Sleep(10)
  434. WEnd
  435. Return 1
  436. EndFunc ; ==>_SFTP_DirPutContents
  437. ; #FUNCTION# ====================================================================================================================
  438. ; Name...........: _SFTP_DirSetCurrent
  439. ; Description ...: Set current remote Directory of a SFTP connection.
  440. ; Syntax.........: _SFTP_DirSetCurrent ( $hConnection [, $sRemoteDir = ""] )
  441. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  442. ; $sRemoteDir - Optional, The remote Directory (or use the original Directory if not defined) i.e. "/website/home".
  443. ; Return values .: Success - Remote Directory
  444. ; Failure - 0, sets @error
  445. ; |1 - The connection is closed
  446. ; |2 - Directory not found
  447. ; |3 - Other error
  448. ; Author ........: Lupo73
  449. ; Modified.......:
  450. ; Remarks .......:
  451. ; Related .......: _SFTP_Connect
  452. ; Link ..........:
  453. ; Example .......: No
  454. ; ===============================================================================================================================
  455. Func _SFTP_DirSetCurrent($hConnection, $sRemoteDir = "")
  456. If ProcessExists($hConnection) = 0 Then
  457. Return SetError(1, 0, 0)
  458. EndIf
  459. Local $sLine
  460. StdinWrite($hConnection, 'cd "' & $sRemoteDir & '"' & @CRLF)
  461. While 1
  462. $sLine = StdoutRead($hConnection)
  463. If ProcessExists($hConnection) = 0 Then
  464. Return SetError(1, 0, 0)
  465. ElseIf StringInStr($sLine, "Remote directory is now") Then
  466. ExitLoop
  467. ElseIf StringInStr($sLine, "no such file or directory") Then
  468. Return SetError(2, 0, 0)
  469. ElseIf $sLine <> "" Then
  470. Return SetError(3, 0, 0)
  471. EndIf
  472. Sleep(10)
  473. WEnd
  474. $__gsRemoteDir_SFTP = StringTrimLeft($sLine, 24)
  475. Return $__gsRemoteDir_SFTP
  476. EndFunc ; ==>_SFTP_DirSetCurrent
  477. ; #FUNCTION# ====================================================================================================================
  478. ; Name...........: _SFTP_DirSetCurrentLocal
  479. ; Description ...: Set current local Directory of a SFTP connection.
  480. ; Syntax.........: _SFTP_DirSetCurrentLocal ( $hConnection, $sLocalDir )
  481. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  482. ; $sLocalDir - The local Directory i.e. "c:\temp".
  483. ; Return values .: Success - Local Directory
  484. ; Failure - 0, sets @error
  485. ; |1 - The connection is closed
  486. ; |2 - Directory not found
  487. ; |3 - Other error
  488. ; Author ........: Lupo73
  489. ; Modified.......:
  490. ; Remarks .......:
  491. ; Related .......: _SFTP_Connect
  492. ; Link ..........:
  493. ; Example .......: No
  494. ; ===============================================================================================================================
  495. Func _SFTP_DirSetCurrentLocal($hConnection, $sLocalDir)
  496. If ProcessExists($hConnection) = 0 Then
  497. Return SetError(1, 0, 0)
  498. EndIf
  499. Local $sLine
  500. StdinWrite($hConnection, 'lcd "' & $sLocalDir & '"' & @CRLF)
  501. While 1
  502. $sLine = StdoutRead($hConnection)
  503. If ProcessExists($hConnection) = 0 Then
  504. Return SetError(1, 0, 0)
  505. ElseIf StringInStr($sLine, "New local directory is") Then
  506. ExitLoop
  507. ElseIf StringInStr($sLine, "unable to") Then
  508. Return SetError(2, 0, 0)
  509. ElseIf $sLine <> "" Then
  510. Return SetError(3, 0, 0)
  511. EndIf
  512. Sleep(10)
  513. WEnd
  514. $__gsLocalDir_SFTP = StringTrimLeft($sLine, 23)
  515. Return $__gsLocalDir_SFTP
  516. EndFunc ; ==>_SFTP_DirSetCurrentLocal
  517. ; #FUNCTION# ====================================================================================================================
  518. ; Name...........: _SFTP_FileDelete
  519. ; Description ...: Deletes a file on a SFTP server.
  520. ; Syntax.........: _SFTP_FileDelete ( $hConnection, $sRemoteFile )
  521. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  522. ; $sRemoteFile - The remote file to be deleted.
  523. ; Return values .: Success - 1
  524. ; Failure - 0, sets @error
  525. ; |1 - The connection is closed
  526. ; |2 - File not found
  527. ; |3 - Other error
  528. ; Author ........: Lupo73
  529. ; Modified.......:
  530. ; Remarks .......:
  531. ; Related .......: _SFTP_Connect, _SFTP_DirDelete
  532. ; Link ..........:
  533. ; Example .......: No
  534. ; ===============================================================================================================================
  535. Func _SFTP_FileDelete($hConnection, $sRemoteFile)
  536. If ProcessExists($hConnection) = 0 Then
  537. Return SetError(1, 0, 0)
  538. EndIf
  539. Local $sLine
  540. StdinWrite($hConnection, 'del "' & $sRemoteFile & '"' & @CRLF)
  541. While 1
  542. $sLine = StdoutRead($hConnection)
  543. If ProcessExists($hConnection) = 0 Then
  544. Return SetError(1, 0, 0)
  545. ElseIf StringInStr($sLine, $sRemoteFile & ": OK") Then
  546. ExitLoop
  547. ElseIf StringInStr($sLine, "no such file or directory") Then
  548. Return SetError(2, 0, 0)
  549. ElseIf $sLine <> "" Then
  550. Return SetError(3, 0, 0)
  551. EndIf
  552. Sleep(10)
  553. WEnd
  554. Return 1
  555. EndFunc ; ==>_SFTP_FileDelete
  556. ; #FUNCTION# ====================================================================================================================
  557. ; Name...........: _SFTP_FileExists
  558. ; Description ...: Checks if a file or folder exists on a SFTP server.
  559. ; Syntax.........: _SFTP_FileExists ( $hConnection, $sRemoteFile )
  560. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  561. ; $sRemoteFile - The remote file.
  562. ; Return values .: Success - 1
  563. ; Failure - 0, sets @error
  564. ; |1 - The connection is closed
  565. ; |2 - Directory not found
  566. ; |3 - Directory is empty
  567. ; Author ........: Lupo73
  568. ; Modified.......:
  569. ; Remarks .......:
  570. ; Related .......: _SFTP_Connect
  571. ; Link ..........:
  572. ; Example .......: No
  573. ; ===============================================================================================================================
  574. Func _SFTP_FileExists($hConnection, $sRemoteFile)
  575. If ProcessExists($hConnection) = 0 Then
  576. Return SetError(1, 0, 0)
  577. EndIf
  578. Local $aStringSplit, $aFileList, $iExists = 0, $sRemoteDir = ""
  579. $aStringSplit = StringSplit($sRemoteFile, "/")
  580. If $aStringSplit[0] > 1 Then
  581. $sRemoteDir = StringTrimRight($sRemoteFile, StringLen($aStringSplit[$aStringSplit[0]]) + 1)
  582. $sRemoteFile = $aStringSplit[$aStringSplit[0]]
  583. EndIf
  584. $aFileList = _SFTP_ListToArray($hConnection, $sRemoteDir)
  585. If @error Then
  586. Return SetError(2, 0, 0)
  587. EndIf
  588. If $aFileList[0] = 0 Then
  589. Return SetError(3, 0, 0)
  590. EndIf
  591. For $A = 1 To $aFileList[0]
  592. If $sRemoteFile = $aFileList[$A] Then
  593. $iExists = 1
  594. ExitLoop
  595. EndIf
  596. Next
  597. Return $iExists
  598. EndFunc ; ==>_SFTP_FileExists
  599. ; #FUNCTION# ====================================================================================================================
  600. ; Name...........: _SFTP_FileGet
  601. ; Description ...: Get a file from a SFTP server.
  602. ; Syntax.........: _SFTP_FileGet ( $hConnection, $sRemoteFile [, $sLocalFile = "" [, $fFailIfExists = False]] )
  603. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  604. ; $sRemoteFile - The remote file.
  605. ; $sLocalFile - Optional, The local file (or use the source name if not defined).
  606. ; $fFailIfExists - Optional, True: do not overwrite existing (default = False)
  607. ; Return values .: Success - 1
  608. ; Failure - 0, sets @error
  609. ; |1 - The connection is closed
  610. ; |2 - Local file exists and $fFailIfExists = True
  611. ; |3 - Remote file not found
  612. ; |4 - Other error
  613. ; Author ........: Lupo73
  614. ; Modified.......:
  615. ; Remarks .......:
  616. ; Related .......: _SFTP_Connect
  617. ; Link ..........:
  618. ; Example .......: No
  619. ; ===============================================================================================================================
  620. Func _SFTP_FileGet($hConnection, $sRemoteFile, $sLocalFile = "", $fFailIfExists = False)
  621. If ProcessExists($hConnection) = 0 Then
  622. Return SetError(1, 0, 0)
  623. EndIf
  624. If $sLocalFile <> "" Then
  625. $sLocalFile = __WinAPI_GetFullPathName($sLocalFile)
  626. If FileExists($sLocalFile) Then
  627. If $fFailIfExists Then
  628. Return SetError(2, 0, 0)
  629. EndIf
  630. EndIf
  631. $sLocalFile = ' "' & $sLocalFile & '"'
  632. EndIf
  633. Local $sLine
  634. StdinWrite($hConnection, 'get -- "' & $sRemoteFile & '"' & $sLocalFile & @CRLF)
  635. While 1
  636. $sLine = StdoutRead($hConnection)
  637. If ProcessExists($hConnection) = 0 Then
  638. Return SetError(1, 0, 0)
  639. ElseIf StringInStr($sLine, "psftp>") Then
  640. ExitLoop
  641. ElseIf StringInStr($sLine, "=> local:") Then
  642. ContinueLoop
  643. ElseIf StringInStr($sLine, "no such file or directory") Then
  644. Return SetError(3, 0, 0)
  645. ElseIf $sLine <> "" Then
  646. Return SetError(4, 0, 0)
  647. EndIf
  648. Sleep(10)
  649. WEnd
  650. Return 1
  651. EndFunc ; ==>_SFTP_FileGet
  652. ; #FUNCTION# ====================================================================================================================
  653. ; Name...........: _SFTP_FileGetInfo
  654. ; Description ...: Get info of a file or folder from a SFTP server.
  655. ; Syntax.........: _SFTP_FileGetInfo ( $hConnection, $sRemoteFile )
  656. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  657. ; $sRemoteFile - The remote file.
  658. ; Return values .: Success - returns an Array containing file info.
  659. ; Failure - 0, sets @error
  660. ; |1 - The connection is closed
  661. ; |2 - Directory not found
  662. ; |3 - Directory is empty
  663. ; |4 - File not found
  664. ; Author ........: Lupo73
  665. ; Modified.......:
  666. ; Remarks .......: Array[0] Filename
  667. ; Array[1] Filesize
  668. ; Array[2] Permissions
  669. ; Array[3] File Modification datetime
  670. ; Array[4] Owner/Group
  671. ; Related .......: _SFTP_Connect, _SFTP_ListToArrayEx
  672. ; Link ..........:
  673. ; Example .......: No
  674. ; ===============================================================================================================================
  675. Func _SFTP_FileGetInfo($hConnection, $sRemoteFile)
  676. If ProcessExists($hConnection) = 0 Then
  677. Return SetError(1, 0, 0)
  678. EndIf
  679. Local $aArray[5], $aStringSplit, $aFileList, $sRemoteDir = ""
  680. $aStringSplit = StringSplit($sRemoteFile, "/")
  681. If $aStringSplit[0] > 1 Then
  682. $sRemoteDir = StringTrimRight($sRemoteFile, StringLen($aStringSplit[$aStringSplit[0]]) + 1)
  683. $sRemoteFile = $aStringSplit[$aStringSplit[0]]
  684. EndIf
  685. $aFileList = _SFTP_ListToArrayEx($hConnection, $sRemoteDir)
  686. If @error Then
  687. Return SetError(2, 0, 0)
  688. EndIf
  689. If $aFileList[0][0] = 0 Then
  690. Return SetError(3, 0, 0)
  691. EndIf
  692. For $A = 1 To $aFileList[0][0]
  693. If $sRemoteFile = $aFileList[$A][0] Then
  694. For $B = 0 To 4
  695. $aArray[$B] = $aFileList[$A][$B]
  696. Next
  697. Return $aArray
  698. EndIf
  699. Next
  700. Return SetError(4, 0, 0)
  701. EndFunc ; ==>_SFTP_FileGetInfo
  702. ; #FUNCTION# ====================================================================================================================
  703. ; Name...........: _SFTP_FileGetSize
  704. ; Description ...: Get the filesize of a file from a SFTP server.
  705. ; Syntax.........: _SFTP_FileGetSize ( $hConnection, $sRemoteFile )
  706. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  707. ; $sRemoteFile - The remote file.
  708. ; Return values .: Success - Filesize
  709. ; Failure - 0, sets @error
  710. ; |1 - The connection is closed
  711. ; |2 - Remote directory not found
  712. ; |3 - Remote directory is empty
  713. ; Author ........: Lupo73
  714. ; Modified.......:
  715. ; Remarks .......:
  716. ; Related .......: _SFTP_Connect, _SFTP_ListToArrayEx
  717. ; Link ..........:
  718. ; Example .......: No
  719. ; ===============================================================================================================================
  720. Func _SFTP_FileGetSize($hConnection, $sRemoteFile)
  721. If ProcessExists($hConnection) = 0 Then
  722. Return SetError(1, 0, 0)
  723. EndIf
  724. Local $aStringSplit, $aFileList, $iSize = 0, $sRemoteDir = ""
  725. $aStringSplit = StringSplit($sRemoteFile, "/")
  726. If $aStringSplit[0] > 1 Then
  727. $sRemoteDir = StringTrimRight($sRemoteFile, StringLen($aStringSplit[$aStringSplit[0]]) + 1)
  728. $sRemoteFile = $aStringSplit[$aStringSplit[0]]
  729. EndIf
  730. $aFileList = _SFTP_ListToArrayEx($hConnection, $sRemoteDir, 2)
  731. If @error Then
  732. Return SetError(2, 0, 0)
  733. EndIf
  734. If $aFileList[0][0] = 0 Then
  735. Return SetError(3, 0, 0)
  736. EndIf
  737. For $A = 1 To $aFileList[0][0]
  738. If $sRemoteFile = $aFileList[$A][0] Then
  739. $iSize = $aFileList[$A][1]
  740. ExitLoop
  741. EndIf
  742. Next
  743. Return $iSize
  744. EndFunc ; ==>_SFTP_FileGetSize
  745. ; #FUNCTION# ====================================================================================================================
  746. ; Name...........: _SFTP_FileMove
  747. ; Description ...: Move/Rename a file on a SFTP server.
  748. ; Syntax.........: _SFTP_FileMove ( $hConnection, $sSourceFile, $sDestinationFile )
  749. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  750. ; $sSourceFile - The source file to move/rename.
  751. ; $sDestinationFile - The destination file to move/rename.
  752. ; Return values .: Success - 1
  753. ; Failure - 0, sets @error
  754. ; |1 - The connection is closed
  755. ; |2 - File not found
  756. ; |3 - Other error
  757. ; Author ........: Lupo73
  758. ; Modified.......:
  759. ; Remarks .......:
  760. ; Related .......: _SFTP_Connect
  761. ; Link ..........:
  762. ; Example .......: No
  763. ; ===============================================================================================================================
  764. Func _SFTP_FileMove($hConnection, $sSourceFile, $sDestinationFile)
  765. If ProcessExists($hConnection) = 0 Then
  766. Return SetError(1, 0, 0)
  767. EndIf
  768. Local $sLine
  769. StdinWrite($hConnection, 'mv "' & $sSourceFile & '" "' & $sDestinationFile & '"' & @CRLF)
  770. While 1
  771. $sLine = StdoutRead($hConnection)
  772. If ProcessExists($hConnection) = 0 Then
  773. Return SetError(1, 0, 0)
  774. ElseIf StringInStr($sLine, "psftp>") Then
  775. ExitLoop
  776. ElseIf StringInStr($sLine, $sSourceFile & " -> ") Then
  777. ContinueLoop
  778. ElseIf StringInStr($sLine, "no such file or directory") Then
  779. Return SetError(2, 0, 0)
  780. ElseIf $sLine <> "" Then
  781. Return SetError(3, 0, 0)
  782. EndIf
  783. Sleep(10)
  784. WEnd
  785. Return 1
  786. EndFunc ; ==>_SFTP_FileMove
  787. ; #FUNCTION# ====================================================================================================================
  788. ; Name...........: _SFTP_FilePut
  789. ; Description ...: Puts a file on a SFTP server.
  790. ; Syntax.........: _SFTP_FilePut ( $hConnection, $sLocalFile [, $sRemoteFile = ""] )
  791. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  792. ; $sLocalFile - The local file i.e. "c:\temp".
  793. ; $sRemoteFile - Optional, The remote file (or use the source name if not defined) i.e. "/website/home".
  794. ; $fRecursivePut - Optional, Recurse through sub-dirs: 0 = Non recursive, 1 = Recursive (default)
  795. ; Return values .: Success - 1
  796. ; Failure - 0, sets @error
  797. ; |1 - The connection is closed
  798. ; |2 - Local file does not exists
  799. ; |3 - Other error
  800. ; Author ........: Lupo73
  801. ; Modified.......:
  802. ; Remarks .......:
  803. ; Related .......: _SFTP_Connect
  804. ; Link ..........:
  805. ; Example .......: No
  806. ; ===============================================================================================================================
  807. Func _SFTP_FilePut($hConnection, $sLocalFile, $sRemoteFile = "")
  808. If ProcessExists($hConnection) = 0 Then
  809. Return SetError(1, 0, 0)
  810. EndIf
  811. Local $sLine
  812. If $sRemoteFile <> "" Then
  813. $sRemoteFile = ' "' & $sRemoteFile & '"'
  814. EndIf
  815. StdinWrite($hConnection, 'put -- "' & $sLocalFile & '"' & $sRemoteFile & @CRLF)
  816. While 1
  817. $sLine = StdoutRead($hConnection)
  818. If ProcessExists($hConnection) = 0 Then
  819. Return SetError(1, 0, 0)
  820. ElseIf StringInStr($sLine, "psftp>") Then
  821. ExitLoop
  822. ElseIf StringInStr($sLine, "=> remote:") Then
  823. ContinueLoop
  824. ElseIf StringInStr($sLine, "unable to open") Then
  825. Return SetError(2, 0, 0)
  826. ElseIf $sLine <> "" Then
  827. Return SetError(3, 0, 0)
  828. EndIf
  829. Sleep(10)
  830. WEnd
  831. Return 1
  832. EndFunc ; ==>_SFTP_FilePut
  833. ; #FUNCTION# ====================================================================================================================
  834. ; Name...........: _SFTP_ListToArray
  835. ; Description ...: Get names of files/folders into defined remote directory.
  836. ; Syntax.........: _SFTP_ListToArray ( $hConnection [, $sRemoteDir = "" [, $ReturnType = 0]] )
  837. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  838. ; $sRemoteDir - Optional, The remote Directory (or use current remote Directory if not defined).
  839. ; $ReturnType - Optional, 0 = Both Files and Folders (default), 1 = Folders, 2 = Files.
  840. ; Return values .: Success - returns an Array containing the names. Array[0] contain the number of found entries.
  841. ; Failure - Array[0] = 0, sets @error
  842. ; |1 - The connection is closed
  843. ; |2 - Remote directory not found
  844. ; |3 - Error splitting the list of files
  845. ; |4 - Wrong ReturnType value
  846. ; |5 - Other error
  847. ; Author ........: Lupo73, TCR
  848. ; Modified.......:
  849. ; Remarks .......:
  850. ; Related .......: _SFTP_Connect
  851. ; Link ..........:
  852. ; Example .......: No
  853. ; ===============================================================================================================================
  854. Func _SFTP_ListToArray($hConnection, $sRemoteDir = "", $ReturnType = 0)
  855. Local $aArray[1] = [0]
  856. If ProcessExists($hConnection) = 0 Then
  857. Return SetError(1, 0, $aArray)
  858. EndIf
  859. If $ReturnType < 0 Or $ReturnType > 2 Then
  860. Return SetError(4, 0, $aArray)
  861. EndIf
  862. Local $sLine, $aStringSplit, $aSubStringSplit
  863. If $sRemoteDir <> "" Then
  864. $sRemoteDir = ' "' & $sRemoteDir & '"'
  865. EndIf
  866. StdinWrite($hConnection, 'ls' & $sRemoteDir & @CRLF)
  867. While 1
  868. $sLine &= StdoutRead($hConnection)
  869. If ProcessExists($hConnection) = 0 Then
  870. Return SetError(1, 0, $aArray)
  871. ElseIf StringInStr($sLine, "Unable to open") Then
  872. Return SetError(2, 0, $aArray)
  873. ElseIf StringInStr($sLine, "Multiple-level wildcards") Or StringInStr($sLine, ": canonify: ") Then
  874. Return SetError(5, 0, $aArray)
  875. ElseIf StringInStr($sLine, "psftp>") Then
  876. ExitLoop
  877. EndIf
  878. Sleep(10)
  879. WEnd
  880. $aStringSplit = StringSplit(StringStripCR($sLine), @LF)
  881. If IsArray($aStringSplit) = 0 Then
  882. Return SetError(3, 0, $aArray)
  883. EndIf
  884. ReDim $aArray[$aStringSplit[0] + 1]
  885. For $A = 1 To $aStringSplit[0]
  886. If ($ReturnType = 1 And StringLeft($aStringSplit[$A], 1) <> "d") Or ($ReturnType = 2 And StringLeft($aStringSplit[$A], 1) <> "-") Then
  887. ContinueLoop
  888. EndIf
  889. $aSubStringSplit = _StringExplode(StringStripWS($aStringSplit[$A], 7), " ", 8)
  890. If UBound($aSubStringSplit) < 9 Then
  891. ContinueLoop
  892. EndIf
  893. $aArray[0] += 1
  894. $aArray[$aArray[0]] = $aSubStringSplit[8]
  895. Next
  896. ReDim $aArray[$aArray[0] + 1]
  897. Return $aArray
  898. EndFunc ; ==>_SFTP_ListToArray
  899. ; #FUNCTION# ====================================================================================================================
  900. ; Name...........: _SFTP_ListToArrayEx
  901. ; Description ...: Get names, sizes, attributes and times of files/folders into defined remote directory.
  902. ; Syntax.........: _SFTP_ListToArrayEx ( $hConnection [, $sRemoteDir = "" [, $ReturnType = 0 [, $fTimeFormat = 1]]] )
  903. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  904. ; $sRemoteDir - Optional, The remote Directory (or use current remote Directory if not defined).
  905. ; $ReturnType - Optional, 0 = Both Files and Folders (default), 1 = Folders, 2 = Files.
  906. ; $fTimeFormat - Optional, type on the date strings: 1 = YYYY/MM/DD[ HH:MM] (default), 0 = MM/DD/YYYY[ HH:MM].
  907. ; Return values .: Success - returns a 2D Array, see remarks.
  908. ; Failure - Array[0][0] = 0, sets @error
  909. ; |1 - The connection is closed
  910. ; |2 - Remote directory not found
  911. ; |3 - Error splitting the list of files
  912. ; |4 - Wrong ReturnType value
  913. ; |5 - Other error
  914. ; Author ........: Lupo73, TCR
  915. ; Modified.......:
  916. ; Remarks .......: Array[0][0] = number of found entries
  917. ; Array[x][0] Filename
  918. ; Array[x][1] Filesize
  919. ; Array[x][2] Permissions
  920. ; Array[x][3] File Modification datetime
  921. ; Array[x][4] Owner/Group
  922. ; Related .......: _SFTP_Connect
  923. ; Link ..........:
  924. ; Example .......: No
  925. ; ===============================================================================================================================
  926. Func _SFTP_ListToArrayEx($hConnection, $sRemoteDir = "", $ReturnType = 0, $fTimeFormat = 1)
  927. Local $aArray[1][5]
  928. $aArray[0][0] = 0
  929. If ProcessExists($hConnection) = 0 Then
  930. Return SetError(1, 0, $aArray)
  931. EndIf
  932. If $ReturnType < 0 Or $ReturnType > 2 Then
  933. Return SetError(4, 0, $aArray)
  934. EndIf
  935. Local $sLine, $sTime, $aStringSplit, $aSubStringSplit
  936. If $sRemoteDir <> "" Then
  937. $sRemoteDir = ' "' & $sRemoteDir & '"'
  938. EndIf
  939. StdinWrite($hConnection, 'ls' & $sRemoteDir & @CRLF)
  940. While 1
  941. $sLine &= StdoutRead($hConnection)
  942. If ProcessExists($hConnection) = 0 Then
  943. Return SetError(1, 0, $aArray)
  944. ElseIf StringInStr($sLine, "Unable to open") Then
  945. Return SetError(2, 0, $aArray)
  946. ElseIf StringInStr($sLine, "Multiple-level wildcards") Or StringInStr($sLine, ": canonify:") Then
  947. Return SetError(5, 0, $aArray)
  948. ElseIf StringInStr($sLine, "psftp>") Then
  949. ExitLoop
  950. EndIf
  951. Sleep(10)
  952. WEnd
  953. $aStringSplit = StringSplit(StringStripCR($sLine), @LF)
  954. If IsArray($aStringSplit) = 0 Then
  955. Return SetError(3, 0, $aArray)
  956. EndIf
  957. ReDim $aArray[$aStringSplit[0] + 1][5]
  958. For $A = 1 To $aStringSplit[0]
  959. If ($ReturnType = 1 And StringLeft($aStringSplit[$A], 1) <> "d") Or ($ReturnType = 2 And StringLeft($aStringSplit[$A], 1) <> "-") Then
  960. ContinueLoop
  961. EndIf
  962. $aSubStringSplit = _StringExplode(StringStripWS($aStringSplit[$A], 7), " ", 8)
  963. If UBound($aSubStringSplit) < 9 Then
  964. ContinueLoop
  965. EndIf
  966. $aArray[0][0] += 1
  967. $aArray[$aArray[0][0]][0] = $aSubStringSplit[8]
  968. $aArray[$aArray[0][0]][1] = 0
  969. If StringLeft($aSubStringSplit[0], 1) <> "d" Then ; Is A File.
  970. $aArray[$aArray[0][0]][1] = $aSubStringSplit[4]
  971. EndIf
  972. $aArray[$aArray[0][0]][2] = $aSubStringSplit[0]
  973. $sTime = ""
  974. $aSubStringSplit[6] = StringRight("0" & $aSubStringSplit[6], 2)
  975. If StringInStr($aSubStringSplit[7], ":") Then ; Is A Time.
  976. $sTime = " " & $aSubStringSplit[7]
  977. $aSubStringSplit[7] = @YEAR
  978. If _DateDiff('n', $aSubStringSplit[7] & "/" & __MonthToNumber($aSubStringSplit[5]) & "/" & $aSubStringSplit[6] & $sTime, _NowCalc()) < 0 Then
  979. $aSubStringSplit[7] -= 1
  980. EndIf
  981. EndIf
  982. If $fTimeFormat = 1 Then
  983. $aArray[$aArray[0][0]][3] = $aSubStringSplit[7] & "/" & __MonthToNumber($aSubStringSplit[5]) & "/" & $aSubStringSplit[6] & $sTime
  984. Else
  985. $aArray[$aArray[0][0]][3] = __MonthToNumber($aSubStringSplit[5]) & "/" & $aSubStringSplit[6] & "/" & $aSubStringSplit[7] & $sTime
  986. EndIf
  987. ; <<<<<<<<<<<<<<<<<<<<<<<<<<< it may needs to update time based on timezone offset
  988. $aArray[$aArray[0][0]][4] = $aSubStringSplit[2] & " " & $aSubStringSplit[3]
  989. Next
  990. ReDim $aArray[$aArray[0][0] + 1][5]
  991. Return $aArray
  992. EndFunc ; ==>_SFTP_ListToArrayEx
  993. ; #FUNCTION# ====================================================================================================================
  994. ; Name...........: _SFTP_Open
  995. ; Description ...: Opens a SFTP session.
  996. ; Syntax.........: _SFTP_Open ( [$sPath = 'psftp.exe'] )
  997. ; Parameters ....: $sPath - Optional, The local path of the psftp executable i.e. "c:\temp\psftp.exe".
  998. ; Return values .: Success - Handle to internet session to be used in _SFTP_Connect().
  999. ; Failure - 0, sets @error
  1000. ; |1 - The connection is closed
  1001. ; |2 - Other error
  1002. ; Author ........: Lupo73, trainer
  1003. ; Modified.......:
  1004. ; Remarks .......:
  1005. ; Related .......: _SFTP_Connect, _SFTP_Close
  1006. ; Link ..........:
  1007. ; Example .......: No
  1008. ; ===============================================================================================================================
  1009. Func _SFTP_Open($sPath = 'psftp.exe')
  1010. Local $hSession = Run($sPath & " -load null", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
  1011. If @error Then
  1012. Return SetError(1, 0, 0)
  1013. EndIf
  1014. Local $sLine
  1015. While 1
  1016. $sLine = StdoutRead($hSession)
  1017. If ProcessExists($hSession) = 0 Then
  1018. Return SetError(1, 0, 0)
  1019. ElseIf StringInStr($sLine, "psftp>") Then
  1020. ExitLoop
  1021. ElseIf $sLine <> "" Then
  1022. Return SetError(2, 0, 0)
  1023. EndIf
  1024. Sleep(10)
  1025. WEnd
  1026. $__gsLocalDir_SFTP = _SFTP_DirGetCurrentLocal($hSession)
  1027. Return $hSession
  1028. EndFunc ; ==>_SFTP_Open
  1029. ; #FUNCTION# ====================================================================================================================
  1030. ; Name...........: _SFTP_ProgressDownload
  1031. ; Description ...: Downloads a file and shows a Progress window or by Calling a User defined Function.
  1032. ; Syntax.........: _SFTP_ProgressDownload ( $hConnection, $sRemoteFile [, $sLocalFile = "" [, $fFailIfExists = False [, $fRecursiveGet = 1 [, $FunctionToCall = ""]]]] )
  1033. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  1034. ; $sRemoteFile - The remote file.
  1035. ; $sLocalFile - Optional, The local file (or use the source name if not defined).
  1036. ; $fFailIfExists - Optional, True: do not overwrite existing (default = False)
  1037. ; $fRecursiveGet - Optional, Recurse through sub-dirs: 0 = Non recursive, 1 = Recursive (default)
  1038. ; $FunctionToCall - Optional, A function which can update a ProgressBar and react on UserInput like Click on Abort or Close App.
  1039. ; Return values .: Success - 1
  1040. ; Failure - 0, sets @error
  1041. ; |1 - The connection is closed
  1042. ; |2 - Local folder exists and $fFailIfExists = True
  1043. ; |3 - Remote folder not found
  1044. ; |4 - Download aborted by PercentageFunc and Return of Called Function
  1045. ; |5 - Other error
  1046. ; Author ........: Lupo73
  1047. ; Modified.......:
  1048. ; Remarks .......:
  1049. ; Related .......: _SFTP_Connect, _SFTP_ProgressUpload, _SFTP_FileGetSize
  1050. ; Link ..........:
  1051. ; Example .......: No
  1052. ; ===============================================================================================================================
  1053. Func _SFTP_ProgressDownload($hConnection, $sRemoteFile, $sLocalFile = "", $fFailIfExists = False, $fRecursiveGet = 1, $FunctionToCall = "")
  1054. If ProcessExists($hConnection) = 0 Then
  1055. Return SetError(1, 0, 0)
  1056. EndIf
  1057. If $sLocalFile <> "" Then
  1058. $sLocalFile = __WinAPI_GetFullPathName($sLocalFile)
  1059. If FileExists($sLocalFile) Then
  1060. If $fFailIfExists Then
  1061. Return SetError(2, 0, 0)
  1062. EndIf
  1063. EndIf
  1064. $sLocalFile = ' "' & $sLocalFile & '"'
  1065. EndIf
  1066. If $FunctionToCall = "" Then
  1067. ProgressOn("SFTP Download", "Downloading " & $sRemoteFile)
  1068. EndIf
  1069. Local $iSize = _SFTP_FileGetSize($hConnection, $sRemoteFile) ; <<<<<<<<<<<<<<<<<<<<<< 0 for folders
  1070. Local $sLine, $iInitialBytes, $iReadBytes, $iError
  1071. If $fRecursiveGet Then
  1072. $sLine = '-r '
  1073. EndIf
  1074. StdinWrite($hConnection, 'get ' & $sLine & '-- "' & $sRemoteFile & '"' & $sLocalFile & @CRLF)
  1075. $iReadBytes = ProcessGetStats($hConnection, 1) ; <<<<<<<<<<<<<<<<<<<<<<<< it may needs to load from another stat
  1076. $iInitialBytes = $iReadBytes[3]
  1077. While 1
  1078. $sLine = StdoutRead($hConnection)
  1079. If ProcessExists($hConnection) = 0 Then
  1080. $iError = 1
  1081. ExitLoop
  1082. ElseIf StringInStr($sLine, "psftp>") Then
  1083. ExitLoop
  1084. ElseIf StringInStr($sLine, "=> local:") Then
  1085. ContinueLoop
  1086. ElseIf StringInStr($sLine, "no such file or directory") Then
  1087. $iError = 3
  1088. ExitLoop
  1089. ElseIf $sLine <> "" Then
  1090. $iError = 5
  1091. ExitLoop
  1092. EndIf
  1093. $iReadBytes = ProcessGetStats($hConnection, 1)
  1094. If $FunctionToCall = "" Then
  1095. ProgressSet(($iReadBytes[3] - $iInitialBytes) / $iSize * 100)
  1096. Else
  1097. If Call($FunctionToCall, ($iReadBytes[3] - $iInitialBytes) / $iSize * 100) <= 0 Then
  1098. ProcessClose($hConnection)
  1099. $iError = 4
  1100. ExitLoop
  1101. EndIf
  1102. EndIf
  1103. Sleep(10)
  1104. WEnd
  1105. If $FunctionToCall = "" Then
  1106. ProgressOff()
  1107. EndIf
  1108. If $iError Then
  1109. Return SetError($iError, 0, 0)
  1110. EndIf
  1111. Return 1
  1112. EndFunc ; ==>_SFTP_ProgressDownload
  1113. ; #FUNCTION# ====================================================================================================================
  1114. ; Name...........: _SFTP_ProgressUpload
  1115. ; Description ...: Uploads a file and shows a Progress window or by Calling a User defined Function.
  1116. ; Syntax.........: _SFTP_ProgressUpload ( $hConnection, $sLocalFile [, $sRemoteFile = "" [, $fRecursivePut = 1 [, $FunctionToCall = ""]]] )
  1117. ; Parameters ....: $hConnection - as returned by _SFTP_Connect().
  1118. ; $sLocalFile - The local file.
  1119. ; $sRemoteFile - Optional, The remote file (or use the source name if not defined).
  1120. ; $fRecursivePut - Optional, Recurse through sub-dirs: 0 = Non recursive, 1 = Recursive (default)
  1121. ; $FunctionToCall - Optional, A function which can update a ProgressBar and react on UserInput like Click on Abort or Close App.
  1122. ; Return values .: Success - 1
  1123. ; Failure - 0, sets @error
  1124. ; |1 - The connection is closed
  1125. ; |2 - Remote folder not found
  1126. ; |3 - Cannot create directory
  1127. ; |4 - Download aborted by PercentageFunc and Return of Called Function
  1128. ; |5 - Other error
  1129. ; Author ........: Lupo73
  1130. ; Modified.......:
  1131. ; Remarks .......:
  1132. ; Related .......: _SFTP_Connect, _SFTP_ProgressDownload
  1133. ; Link ..........:
  1134. ; Example .......: No
  1135. ; ===============================================================================================================================
  1136. Func _SFTP_ProgressUpload($hConnection, $sLocalFile, $sRemoteFile = "", $fRecursivePut = 1, $FunctionToCall = "")
  1137. If ProcessExists($hConnection) = 0 Then
  1138. Return SetError(1, 0, 0)
  1139. EndIf
  1140. If $sRemoteFile <> "" Then
  1141. $sRemoteFile = ' "' & $sRemoteFile & '"'
  1142. EndIf
  1143. If $FunctionToCall = "" Then
  1144. ProgressOn("SFTP Upload", "Uploading " & $sLocalFile)
  1145. EndIf
  1146. Local $iSize
  1147. If StringInStr(FileGetAttrib($sLocalFile), "D") Then
  1148. $iSize = DirGetSize($sLocalFile)
  1149. Else
  1150. $iSize = FileGetSize($sLocalFile)
  1151. EndIf
  1152. Local $sLine, $iInitialBytes, $iReadBytes, $iError
  1153. If $fRecursivePut Then
  1154. $sLine = '-r '
  1155. EndIf
  1156. StdinWrite($hConnection, 'put ' & $sLine & '-- "' & $sLocalFile & '"' & $sRemoteFile & @CRLF)
  1157. $iReadBytes = ProcessGetStats($hConnection, 1)
  1158. $iInitialBytes = $iReadBytes[3]
  1159. While 1
  1160. $sLine = StdoutRead($hConnection)
  1161. If ProcessExists($hConnection) = 0 Then
  1162. $iError = 1
  1163. ExitLoop
  1164. ElseIf StringInStr($sLine, "psftp>") Then
  1165. ExitLoop
  1166. ElseIf StringInStr($sLine, "=> remote:") Then
  1167. ContinueLoop
  1168. ElseIf StringInStr($sLine, "unable to open") Then
  1169. $iError = 2
  1170. ExitLoop
  1171. ElseIf StringInStr($sLine, "Cannot create directory") Then
  1172. $iError = 3
  1173. ExitLoop
  1174. ElseIf $sLine <> "" Then
  1175. $iError = 5
  1176. ExitLoop
  1177. EndIf
  1178. $iReadBytes = ProcessGetStats($hConnection, 1)
  1179. If $FunctionToCall = "" Then
  1180. ProgressSet(($iReadBytes[3] - $iInitialBytes) / $iSize * 100)
  1181. Else
  1182. If Call($FunctionToCall, ($iReadBytes[3] - $iInitialBytes) / $iSize * 100) <= 0 Then
  1183. ProcessClose($hConnection)
  1184. $iError = 4
  1185. ExitLoop
  1186. EndIf
  1187. EndIf
  1188. Sleep(10)
  1189. WEnd
  1190. If $FunctionToCall = "" Then
  1191. ProgressOff()
  1192. EndIf
  1193. If $iError Then
  1194. Return SetError($iError, 0, 0)
  1195. EndIf
  1196. Return 1
  1197. EndFunc ; ==>_SFTP_ProgressUpload
  1198. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  1199. ; Name...........: __MonthToNumber
  1200. ; Description ...: Converts the name of the Month into the month number.
  1201. ; Syntax.........: __MonthToNumber ( $sMonth )
  1202. ; Parameters ....:
  1203. ; Return values .:
  1204. ; Author ........:
  1205. ; Modified.......:
  1206. ; Remarks .......:
  1207. ; Related .......:
  1208. ; Link ..........:
  1209. ; Example .......:
  1210. ; ===============================================================================================================================
  1211. Func __MonthToNumber($sMonth)
  1212. Switch $sMonth
  1213. Case 'Jan'
  1214. $sMonth = '01'
  1215. Case 'Feb'
  1216. $sMonth = '02'
  1217. Case 'Mar'
  1218. $sMonth = '03'
  1219. Case 'Apr'
  1220. $sMonth = '04'
  1221. Case 'May'
  1222. $sMonth = '05'
  1223. Case 'Jun'
  1224. $sMonth = '06'
  1225. Case 'Jul'
  1226. $sMonth = '07'
  1227. Case 'Aug'
  1228. $sMonth = '08'
  1229. Case 'Sep'
  1230. $sMonth = '09'
  1231. Case 'Oct'
  1232. $sMonth = '10'
  1233. Case 'Nov'
  1234. $sMonth = '11'
  1235. Case 'Dec'
  1236. $sMonth = '12'
  1237. Case Else
  1238. Return SetError(1, 0, "00")
  1239. EndSwitch
  1240. Return $sMonth
  1241. EndFunc ;==>__MonthToNumber
  1242. ; #INTERNAL_USE_ONLY# ===========================================================================================================
  1243. ; Name...........: __WinAPI_GetFullPathName
  1244. ; Description....: Retrieves the full path and file name of the specified file.
  1245. ; Syntax.........: __WinAPI_GetFullPathName ( $sFile )
  1246. ; Parameters.....: $sFile - The name of the file.
  1247. ; Return values..: Success - The drive and path.
  1248. ; Failure - Empty string and sets the @error flag to non-zero.
  1249. ; Author.........: Yashied (WinAPIEx.au3)
  1250. ; Modified.......:
  1251. ; Remarks........: The _WinAPI_GetFullPathName() merges the name of the current drive and directory with a specified file name to
  1252. ; determine the full path and file name of a specified file. This function does not verify that the resulting
  1253. ; path and file name are valid, or that they see an existing file on the associated volume.
  1254. ; Related........:
  1255. ; Link...........: @@MsdnLink@@ GetFullPathName
  1256. ; Example........: Yes
  1257. ; ===============================================================================================================================
  1258. Func __WinAPI_GetFullPathName($sFile)
  1259. Local $Ret = DllCall('kernel32.dll', 'dword', 'GetFullPathNameW', 'wstr', $sFile, 'dword', 4096, 'wstr', '', 'ptr', 0)
  1260. If (@error) Or (Not $Ret[0]) Then
  1261. Return SetError(1, 0, '')
  1262. EndIf
  1263. Return $Ret[3]
  1264. EndFunc ;==>__WinAPI_GetFullPathName

comments powered by Disqus