Search code examples
pythonwinscppywinauto

Can't access files in list control


I'm planning to execute this script on a remote desktop to automatically download a file every day using WinSCP.

So far this script can log in and get to an explorer window, but I cannot access the files in the window. I'd like to be able to iterate through the list of files and open a specific one.

The files are labeled like 'YYYY-MM-DD-0001', 'YYYY-MM-DD-0002', etc and I want to be able to open the most recent file for the respective date.

import pywinauto
from pywinauto.application import Application
from pywinauto.keyboard import send_keys
from pywinauto import keyboard as kb
import time

HOST_NAME = ''
USER_NAME = ''
PASSWORD = ''
PORT_NUMBER = ''
DIRECTORY = ''

main_app = Application(backend='uia').start(cmd_line=r"C:\Program Files (x86)\WinSCP\WinSCP.exe").connect(title='WinSCP', timeout=10)
main_dlg = main_app.window(title='WinSCP')
main_dlg.set_focus()

Login_app = Application().connect(title=u'Login', class_name='TLoginDialog', timeout=10)
Login_dlg = Login_app.window(title=u'Login', class_name='TLoginDialog')
Login_dlg.set_focus()
Login_dlg.wait('ready', timeout=10)

Login_dlg['Edit1'].set_text(PORT_NUMBER) # Port Number
Login_dlg['Edit2'].set_text(PASSWORD) # Password
Login_dlg['Edit3'].set_text(USER_NAME) # User name
Login_dlg['Edit4'].set_text(HOST_NAME) # Host Name

Login_dlg['Button5'].click() # Login


appExplorer = Application().connect(title_re='.*' + HOST_NAME + ' - WinSCP', class_name="TScpExplorerForm", timeout=10)

winExplorer = appExplorer.window(title_re='*- carestreamhealth@' + HOST_NAME + ' - WinSCP', class_name='TScpExplorerForm')
winExplorer.wait('visible',timeout=10)
winExplorer.set_focus()

winExplorer.TTBXToolbar4.click_input() # click into Address bar
winExplorer.TTBXToolbar4.TTBXEdit.set_text(DIRECTORY)
send_keys('{ENTER}')
winExplorer.wait('ready', timeout=10)

Solution

  • Why are you automating WinSCP GUI to download files?

    Use WinSCP scripting:
    https://winscp.net/eng/docs/guide_automation

    See also:
    From Python run WinSCP commands in console

    Another option is to use native Python FTP/SFTP modules.