Select files whose filenames stored in a vector

Hello,

I want to write a script shich selects files first based on a filter and then adds files to that depending which files the filter selected.

This is what I've come up with so far

[code]Function OnClick ( ByRef ClickData )
ClickData.Func.Command.RunCommand("Select ""Interrupted DVB-Files"" Filter")
number_selected_files = DOpus.Listers.LastActive().ActiveTab.selstats.selfiles
Dim Selected_by_filter
Dim Total_files_To_select
Dim slected_file
If number_selected_files = 0 Then
Set dlg = DOpus.Dlg
dlg.Window = DOpus.Listers(0)
dlg.message = "No matching files were found"
dlg.title = "Warnung"
dlg.icon = "information"
dlg.buttons = "OK"
dlg.Show
Else
'Selection = DOpus.Listers.LastActive().ActiveTab.selected_files
Set Selected_by_filter = DOpus.NewVector
Set Total_files_To_select = DOpus.NewVector

Selected_by_filter.assign(ClickData.func.sourcetab.selected_files)
Total_files_To_select.assign(ClickData.func.sourcetab.selected_files)



	Set dlg = DOpus.Dlg
	dlg.Window = DOpus.Listers(0)
	dlg.message = Selected_by_filter.count
	dlg.title = "Info"
	dlg.icon = "information"
	dlg.buttons = "OK"
	dlg.Show


'Set All_selected_Transportstreams = ClickData.func.sourcetab.selected_files.name_stem
'For i=0 To (All_selected_Transportstreams.count) -1
'	Filename = ClickData.func.sourcetab.selected_files(i).name
'	txtname = Replace(Filename, ".ts",".txt")
'	Logname =  Replace(Filename, ".ts",".log")
		

	'Set dlg = DOpus.Dlg
	'dlg.Window = DOpus.Listers(0)
	'dlg.message = Logname
	'dlg.title = "Warnung"
	'dlg.icon = "information"
	'dlg.buttons = "OK"
	'dlg.Show
'Next
For Each Selected_file In Selected_by_filter
	Filename = Selected_file.name
	'Filename = Selected_file.realpath.path
	txtname = Replace(Filename, ".ts",".txt")
	Total_files_To_select.push_back(txtname)
	Logname =  Replace(Filename, ".ts",".log")
	Total_files_To_select.push_back(Logname)
Next

' add the vector the the selected files

Clickdata.Func.Command.AddFiles(Total_files_To_select)
Clickdata.Func.Command.RunCommand("Select FROMSCRIPT")

	Set dlg = DOpus.Dlg
	dlg.Window = DOpus.Listers(0)
	dlg.message = Total_files_To_select.count
	dlg.title = "Info"
	dlg.icon = "information"
	dlg.buttons = "OK"
	dlg.Show

End If
End Function
[/code]

The script crashes at the lines where the files with their filenames in the vector Total_files_To_select should be added to the selection. Most probably I have a misunderstanding of what addfiles does really do. Can anybody help?

Thanks in advance

P.S. I tested the script in a windows library. The Dialog Boxes are just for debugging purposed

While the script does a couple of things wrong, the crash still should not happen, and we will fix that in the next update.

The main errors in the script are:

[ol][li] It was using filenames, rather than full paths. Full paths need to be used when adding files to the command object.

[/li]
[li] It was adding strings into a collection (DOpus.Vector) object, and then passing that to Command.AddFiles. You can pass a string to Command.AddFile, but Command.AddFiles expects a collection of Item objects and not strings. (note AddFile vs AddFiles).

But we will make that work in the future, as it makes sense. So that is only wrong at the moment; in the future it will work. That said, in this particular case the collection isn't really needed at all, so I've removed it in the fixed script below.[/li][/ol]

Here's a fixed version of the script, which addresses both of those. It also has some other improvements, which the comments in the script should explain.

[code]Function OnClick ( ByRef ClickData )

Set dlg = ClickData.Func.Dlg ' Dialog comes with the Window set if you get it from here.
dlg.icon = "information"
dlg.buttons = "OK"

Set cmd = ClickData.Func.Command
cmd.RunCommand("Select *.ts DESELECTNOMATCH") ' Note: Changed command for testing

ClickData.Func.SourceTab.Update ' Update snapshot as we changed the selection
Set selFiles = ClickData.Func.SourceTab.Selected_Files

If selFiles.count = 0 Then
	dlg.message = "No matching files were found"
	dlg.title = "Warning"
	dlg.Show
	Exit Function
End If

dlg.message = "Selected file count (before): " & selFiles.count
dlg.title = "Info"
dlg.Show

For Each Selected_file In selFiles
	' Must use full paths here, not just file names.
	filePath = Selected_file & "" ' Force conversion to string
	txtPath = Replace(filePath, ".ts", ".txt")
	If (txtPath <> filePath) Then
		cmd.AddFile(txtPath) ' Add to Command directly.
	End If
	logPath = Replace(filePath, ".ts", ".log")
	If (logPath <> filePath) Then
		cmd.AddFile(logPath) ' Add to Command directly.
	End If
Next

cmd.RunCommand("Select FROMSCRIPT")

ClickData.Func.SourceTab.Update ' Update snapshot as we changed the selection
Set selFiles = ClickData.Func.SourceTab.Selected_Files

dlg.message = "Selected file count (after): " & selFiles.count
dlg.title = "Info"
dlg.Show

End Function[/code]