Posts: 121
Threads: 33
Joined: Jun 2007
Hi,
I need some general advice on the best way to approach programming a macro that does the following:
1. User selects a file from a fixed directory.
2. User selects a FTP Profile from a combo box (popluated by a list of text files, each containing different FTP site info. E.G., Profile1.txt, Profile2.txt etc.)
3. User repeats the above for another file+FTP Profile, and again as needed.
4. Each file is then uploaded using the appropriate FTP site profile, one after another.
---
I can do the programming for the above doing one file at a time, and waiting a long time for each file to upload before starting on the next. Just not sure how to be able to select and queue several files and THEN have them upload in a row/batch to the various selected FTP servers.
Perhaps create a new text file for each file+profile (containing the file name and complete FTP info transfered from the preset Profile) and store in a ToBeUploaded directory, then get each file's info and upload, repeat etc in a For loop?
Any suggestions would be greatly appreciated!
Thanks,
Steve
Posts: 12,097
Threads: 142
Joined: Dec 2002
Function dlg_ftp_queue
\Dialog_Editor
;Shows dialog where user can select files and ftp.
;Saves the list in "$my qm$\ftp queue.csv".
function# hDlg message wParam lParam
if(hDlg) goto messages
str controls = "3"
str qmg3x
if(!ShowDialog("dlg_ftp_queue" &dlg_ftp_queue &controls)) ret
qmg3x.setfile("$my qm$\ftp queue.csv")
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 223 135 "Dialog"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;3 QM_Grid 0x56031041 0x0 0 0 224 114 "0x0,0,0,0,0x0[]File,,16,[]FTP,,1,"
;END DIALOG
;DIALOG EDITOR: "" 0x2030300 "*" "" ""
ret
;messages
sel message
,case WM_INITDIALOG
,case WM_DESTROY
,case WM_COMMAND goto messages2
,case WM_NOTIFY goto messages3
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1
;messages3
NMHDR* nh=+lParam
sel nh.idFrom
,case 3
,GRID.QM_NMLVDATA* cd=+nh
,sel nh.code
,,case GRID.LVN_QG_BUTTONCLICK ;;when user clicks button
,,if cd.subitem=0
,,,if(OpenSaveDialog(0 _s)) _s.setwintext(cd.hctrl)
,,
,,case GRID.LVN_QG_COMBOFILL ;;when user clicks combo box arrow
,,if cd.subitem=1
,,,TO_CBFill cd.hcb "ftp.one.com[]ftp.two.com[]ftp.three.com"
Macro exec_ftp_queue
;Opens file created by dlg_ftp_queue, and uploads all.
str sFile="$my qm$\ftp queue.csv"
ICsv x=CreateCsv(1)
ICsv xe=CreateCsv(1) ;;for failed uploads
x.FromFile(sFile)
int i
for i 0 x.RowCount
,;get file and ftp from csv
,str s1 s2
,s1=x.Cell(i 0)
,s2=x.Cell(i 1)
,out "file: %s, ftp: %s" s1 s2
,
,;now upload
,int failed=0
,err-
,;...
,;...
,err+ failed=1
,if(failed) xe.AddRowSA(-1 2 &s1)
if xe.RowCount
,xe.ToFile(sFile) ;;save failed uploads. Then you can run this macro again to upload them.
else
,del- sFile
Posts: 121
Threads: 33
Joined: Jun 2007
Very cool, and fast as always. Many thanks.
Probably have a few more questions once I expand on your code...
Cheers,
S
Posts: 121
Threads: 33
Joined: Jun 2007
Actually, already have a question...where you say "now upload", do I put the actual upload FTP code between the err- and err+ lines?
(never used err code before...)
Posts: 12,097
Threads: 142
Joined: Dec 2002
Yes, between err- and err+.
Posts: 121
Threads: 33
Joined: Jun 2007
Works like a charm, thanks again!
Posts: 121
Threads: 33
Joined: Jun 2007
Hi Gintaras,
Having a little trouble with something - just starting to learn working with CSV in QM (thanks!) and could use some help.
In the end of the exec macro...
if xe.RowCount
xe.ToFile(sFile) ;;save failed uploads. Then you can run this macro again to upload them.
...what is the best way to display a notification of the failed file(s)? I can read and display them one at a time using a "for" loop, but I'd prefer something along the lines of this:
The following files were not uploaded and have been requeued:
file1.wav (profile1)
file2.mp3 (profile1)
or a read-only grid like the one used to select the file/ftp profile.
Posts: 12,097
Threads: 142
Joined: Dec 2002
Insert this in dlg_ftp_queue, before ShowDialog.
Function dlg_ftp_queue
qmg3x.getfile("$my qm$\ftp queue.csv"); err
And run dlg_ftp_queue when fails to upload.
Posts: 121
Threads: 33
Joined: Jun 2007
Cool, thanks.
Any ideas on how to automatically delete the successfully uploaded files from the local "Queue" directory (directory will always be the same, btw) and leave the ones that failed?
Can it be incorporated in the FilePut section upon successful callback? Here is the code I am using there:
Macro exec_ftp_queue
,;****now upload***
,int failed=0
,err-
,;...
,Ftp f.Connect(ftp_server ftp_username ftp_password)
,if(!f.DirSet(ftp_directory)) xe.AddRowSA(-1 2 &s1) ;; on directory set error, relist failed file/profile to row (this error not caught by Gintaras err- code
,f.FilePutWithProgress(local_file "")
,;...
,err+ failed=1
,if(failed) xe.AddRowSA(-1 2 &s1) ;; on any other error, relist failed file/profile to row
Or do I need to do some directory operations after the whole list is processed (like move out the failed files to temp directory, delete the contents of the Queue directory and move failed back?)
Posts: 12,097
Threads: 142
Joined: Dec 2002
Macro Macro1545
,;****now upload***
,int failed=0
,err-
,;...
,Ftp f.Connect(ftp_server ftp_username ftp_password)
,if(!f.DirSet(ftp_directory)) goto gFailed
,if(!f.FilePutWithProgress(local_file "")) goto gFailed
,;...
,err+
,,;gFailed
,,failed=1
,if(failed) xe.AddRowSA(-1 2 &s1) ;; on any other error, relist failed file/profile to row
,else del s1
;I would place FTP uploading code in separate function. Then if fails, ret 0. Finally ret 1 if succeeds.
Posts: 121
Threads: 33
Joined: Jun 2007
Posts: 121
Threads: 33
Joined: Jun 2007
Hi Gintaras,
I've reached a big roadblock and hoping you can help.
Was calling code based on your queue mac and dlg above from a button on a "main" dialog (main is several buttons that launch various other dlgs). When I went to compile, it resulted in error because the "Queue" button was calling a macro (exec_ftp_queue).
I tried to work around this by creating a single dialog dlg_ftp_queue that contains the grid and then exec's the code on OK. Been very close but no cigar.
Was wondering if it's possible to call from another dlg a combined queue dlg grid and then exec the code on ok? If so, please include your modification in later post that added the line "qmg3x.getfile("$my qm$\ftp queue.csv"); err" after calling dlg, as this was a sticking point for me.
Your help is much appreciated!
Posts: 12,097
Threads: 142
Joined: Dec 2002
Quote:Was calling code based on your queue mac and dlg above from a button on a "main" dialog (main is several buttons that launch various other dlgs). When I went to compile, it resulted in error because the "Queue" button was calling a macro (exec_ftp_queue).
What error?
,case QueueButtonId
,mac "exec_ftp_queue"
Posts: 121
Threads: 33
Joined: Jun 2007
Code in main dialog "dlg_start_here"
Function dlg_start_here
,case 6
,;FTP QUEUE
,,mac "exec_ftp_queue"
Error message when I try to make .exe:
Error in dlg_start_here: must be function.
Cannot make exe. Error 4, failed to compile.
...and error highlights "exec_ftp_queue" in editor.
EDIT: In orig post I said compile and not make exe, sorry...
Posts: 12,097
Threads: 142
Joined: Dec 2002
Posts: 121
Threads: 33
Joined: Jun 2007
OK, that worked. Unbelievably simple and obvious, given how much time I spent on other almost-solutions, grrrr.
Thanks!
Posts: 12,097
Threads: 142
Joined: Dec 2002
Too short error description. Will be more clear in next QM. Thank you.
|