Copy file to path only if it has a specific extension

Hey, I'd like to know if you can copy files to a path only if they match a specific extension ? And without deselecting non matching extensions for whole button... so it can run on multiple selected files with different extensions going to different paths.

I could move a selected file to a path only if it matches the extension with this regexp in a button:

 RENAME REGEXP PATTERN  = "(.*).ext" TO="c:\Test\{file}"  

Can I put a copy command in somehow so it copies not moves?

Maybe something like this:

Copy  TO "c:\Test" "*.ext"

But that works on all files not just selected ones

Using a couple of tricks (mainly the "reselect" between each line), I think this type of thing should work:

Copy TO "C:\Test\Text Files" PATTERN "*.txt" AS "*.txt" Select RESELECT Copy TO "C:\Test\Image Files" PATTERN "*.png" AS "*.png"

It could also be done using filters, but then you'd have to define them separately.

Thanks heaps I got it working now! That code didn't quite work but I've been muddling around with it for a while and ended up only needing a slight modification to yours that made it work.

Hi can you please put the button or the code that you get working for this?. thanks-

Copy "*.txt" TO "C:\Test\Text Files" AS "*.txt" Select RESELECT Copy "*.png" TO "C:\Test\Image Files" AS "*.png"

I think that will ignore the file selection and copy all files in the current folder matching the patterns.

Ah yes, but your code ignored everything and did nothing for me (DO11). :smiley:

You're right, it wasn't working properly, although it does work sometimes & had always worked for me until I did more thorough testing just now.

I think the RESELECT line is not always selecting what I had expected it to do, with it working sometimes if you happened to have the right selection previously. Maybe it was always like that even in Opus 10.

Here's a better way to do it using Opus 11. The function type should be set to Script Function.

Function OnClick(ByRef ClickData) ClickData.Func.Command.files.count ' (Workaround for bug in current beta.) ClickData.Func.Command.RunCommand "Copy TO ""C:\Test\Text Files"" PATTERN *.txt AS *.txt" ClickData.Func.Command.ClearFailed ClickData.Func.Command.RunCommand "Copy TO ""C:\Test\Image Files"" PATTERN *.png AS *.png" ClickData.Func.Command.ClearFailed End Function

(The ClickData.Func.Command.files.count line at the top is needed in Beta 7 but should no longer be needed after the next update.)

When each Copy command runs, any files that don't match the PATTERN are flagged as "failed", which prevents them from being passed to subsequent commands. Calling ClearFailed on the Command object resets the flag so that both commands consider the full set of files that were selected at the start.

(The last ClearFailed is not entirely redundant, although it is not strictly needed either; it's there to influence which files are left selected after the script completes.)