addFile() and cmd.RunCommand

having 3 days of trouble with this. Trying to pass filepaths from an array and add them to cmd object which should then copy them to collection. What seems to happen is that when the cmd is run, each path is added twice - which results in errors that 'file already added to collection' :

Any ideas would be helpful. :upside_down_face:

var groupPathsARR = []
	for (var GROUPkey in tokensOBJ) {
		if (tokensOBJ.hasOwnProperty(GROUPkey) && counter < 10) {
			groupPathsARR = tokensOBJ[GROUPkey];
			
			for (var i = 0; i < groupPathsARR.length; i++) {
				cmd.AddFile(groupPathsARR[i]);
			}
	
			copyVirtuallyToCOll = 1;
			if (copyVirtuallyToCOll) {
				cmd.RunCommand('COPY TO coll://DUPES ');
			}
		}
		counter++;
		// cmd.ClearFiles();
	}```

If you use the cmd which is passed in func, it already contains selected files : either clear it or create a brand new one.

Although I have read about the cmd obj in the help, the concept still evades me. Could you please elaborate within my example ? Thx

In scripts add-ins and button scripts, you can get a Command object from the entry point parameter.
That cmd object already has selected files "within".
So, if you're using that cmd and do not ClearFiles before running a command, it will apply to both the files selected and the ones you added.
If you do not want that, either clear files before adding new ones or get a new command object (DOpus.Create.Command())

i'm assuming this is what you meant.

Tried it - same result. Duplicates being copied.

I'm wondering - since creating the object myself and may be prone to errors, I wonder if the '' in the string might cause issues (maybe should be escaped i.e. "\") . However that would give different error I would assume)

Here is a schema of my obj from which I get the data:

{
  "ABC-123": [
    "/path/to/file1",
    "/path/to/file2",
    "/path/to/file3"
  ],
  "DEF-456": [
    "/path/to/file4",
    "/path/to/file5"
  ],
  "GHI-789": [
    "/path/to/file6",
    "/path/to/file7",
    "/path/to/file8",
    "/path/to/file9"
  ]
}
var cmd2 =DOpus.Create.Command()
	var groupPathsARR = []
	for (var GROUPkey in tokensOBJ) {
		if (tokensOBJ.hasOwnProperty(GROUPkey) && counter < 5) {
			groupPathsARR = tokensOBJ[GROUPkey];
			
			for (var i = 0; i < groupPathsARR.length; i++) {
				cmd2.addFile(groupPathsARR[i]);
			}
	
			copyVirtuallyToCOll = 1;
			if (copyVirtuallyToCOll) {
				cmd2.RunCommand("Copy TO coll://DUPES");
			}
		}

		counter++;
	}

Adding WHENEXISTS=skip to the copy statement will handle this issue.

However, if you want these files included in a duplicates collection you'll need to introduce a duplicate ID and use DOpusRT to create the collection.

1 Like

Interesting solution. Will try.

As far as introducing a "duplicate ID", do you mean programmatically or is it part of DOPUSRT?

I am still baffled as to why two files are added. I must've checked 20 times the data structure of the OBJECT. No joy.

Basically, because you're adding new files to the Command object in each iteration and then executing the command but with all the accumulated files, not just the ones you just added. This inevitably makes you repeat some copies. RunCommand() doesn't "clear" the files loaded into the Command object.

With something like this, you should be fine.

var cmd2 =DOpus.Create.Command();
var copyVirtuallyToCOll = 1;
var groupPathsARR = [];
for (var GROUPkey in tokensOBJ) {
	if (tokensOBJ.hasOwnProperty(GROUPkey) && counter < 5) {
		groupPathsARR = tokensOBJ[GROUPkey];
			
		for (var i = 0; i < groupPathsARR.length; i++) 
			cmd2.addFile(groupPathsARR[i]);		
	}
	counter++;
}
if (cmd2.filecount && copyVirtuallyToCOll) 
	cmd2.RunCommand("Copy TO coll://DUPES");

More about the Command object