Perform command on entering folder?

being a great fan - cause I know DirOpus can due pretty much everything :smiley:

One thing I could not figure out yet searching the forums:
Is there a way I can perform a certain command each time I do enter a folder in a lister? I´t like to automatically delete certain types of files when I enter a folder ...

Best would be if the functionality could even be limted to a certain folder and it´s sub-folders.

Any idea on this? Thanks - Peter

Is this really necessary - wouldn't you just do a find and delete periodically and be done with it?

I don't think GPsoft want to further complicate the folder format system by adding script or command execution to it... so the only way I can think of would be via a FolderChange event handling script.

Should be pretty easy... I can work on that a bit and would make it such that:

  • You could specify the folders you want this to happen in either by simple wildcard pattern or absolute folder name
  • You could specify the command you want to be run (leave it open ended so ppl could use it for other things)

Shout if interested - but also - have a look through the Script Buttons & Add-Ins forum to see if someone may have already written something like this :wink:.


I searched the "command & scripts" for it, but could not find anything that would work like that.

There is a "FolderChange" event? Sounds like that was what I would be looking for ...

If you have experience on that stuff (sound like you do :smiley: ) - I´d be highly interested on such a script, it would save me from having to click my custom button some hundred times a year ...

Thanks - Peter

PS: The search & delete does not work as the folders are created on an at least daily basis ...

If something keeps leaving unwanted folders around the place, it might be better to fix it at the source so it cleans up its own mess (if possible). Alternatively, a scheduled background task might make sense, or a hotkey you can tap to trigger the clean-up when needed... Or just hide the folders so you can't see them and don't worry about them. :slight_smile:

But if you really do want to have the clean-up triggered on entering the folders in Opus, you can do that fairly easily using a script. If you need help with scripting, please link your account.

Hi Leo,
thanks for the info - I have linked my account now. I´ll take a deeper look at the scripts documentation and come back if needed.

Regards - Peter

Go and checkout the scripting docs, it can be done.
But Leo is right, it sounds like something which should be run in the background from the built-in windows task scheduler e.g. That should be even simpler to do than a DO script.

To delete specific files only, you can probably just run robocopy.exe (provided with windows), pass a wildcard and sync your messy folder(s) against a blank dummy folder.

I do agree that I should solve that upfront and long time I´m working on this. As long as the system still puts the unessary files into my folder I would prefer to get those removed by opus, when I enter the folder, compared to running a process scheduled (opus is more "adhoc" which I would prefer).

I have figured out how to create a script doing that - and think I have the framework done, just I´m struggeling with the "delete part" - have no clue how to delete the *.ktr files from the folder ... I have tried the internal delete command, but I guess the opus internal commandas are not available for a script ... so I need to figure out how to do it correctly in VBS ... which seems not so easy.

Here is what I have so far:

Function OnInit(ByRef initData)
' Provide basic information about the script = ".ktr delete script"
initData.desc = "Script that deletes .ktr files in the entered folder"
initData.copyright = "(c) 2015 PW"
initData.default_enable = True
End Function

OnAfterFolderChange(ByRef afterFolderChangeData)
' The result property of the AfterFolderChangeData object tells us if the read was successful
If afterFolderChangeData.result Then
' Check if the path that was read begins with D:\incoming\data
If Left(, 16) = "D:\incoming\data" Then
' The path matched, so delete .ktr files
Delete *.ktr ' this is where I would neeed help
End If
End If
End Function[/code]

Thanks in advance - Peter

Hi again,

I tried the following for the "delete part" - but it does not seem to work:

Set obj = CreateObject("Scripting.FileSystemObject") obj.DeleteFile( & "\*.ktr")
Do I have to restart opus after making changes to the script? Are those read on the fly or only at startup? Did not work anyway ...

Any help appreciated - Peter

No restart required, a modified script will be reloaded instantly, so the next folder you change is going to run what you just saved.

The filesystemobject does not support wildcards like "*", so you would need to loop over all the files in the folder and check the extension separately.. or
use DOs delete command. Internal commands can be used in scripts, to delete all *.ktr files add a line like this:

DOpus.Create.Command.RunCommand("Delete FILE=*.ktr")

Maybe that was a bit too quick, I just realized, that you'd probably need to set a source tab, to not delete *.ktr files anywhere, so better try with this:

dim cmd : set cmd = DOpus.Create.Command() cmd.SetSourceTab( cmd.RunCommand("Delete FILE=*.ktr")
or use the path directly to make sure to hit the right directory when deleting:

DOpus.Create.Command.RunCommand("Delete FILE=" & & "\*.ktr")

Double quotes need to be added for paths containing spaces, not done here.


Thanks tbone for your help - that works great - at least on folders with no spaces ...! Need to remember how to use the internal commands.

Still struggling how to make it work with spaces in the path in ... guess I have to build the command line seperately with additional double quotes - should be able to figure that out hopefully.

Thanks again - great help!


PS: I find it confusing that if a script does have an error it does vanish from the list of scripts, wouldn´t it be nicer if it would just be marked as faulty?

On the command line you need to quote the path string. In VBScript this means:

DOpus.Create.Command.RunCommand("Delete FILE=""" & & "\*.ktr""")

In JScript you would do:

DOpus.Create.Command.RunCommand("Delete FILE=\"" & & "\*.ktr\"")


Thanks for the ultra fast response - works perfectly.

Jon and tbone - that will save me a lot of clicks in the future ... and that is what Opus is all about right? :smiley:

Guess I can continue to tell my colleagues - there is no configuration thing in Opus that you can think of that you can´t configure :unamused:

Thanks again - great job - Peter

+1 You cannot configure DO to do so, so there's at least one thing you can't! o))

as mentioned that works great - a very minor "annoyance" is the very short flashing of a command prompt window (actually I can´t really see it, just DO looses and regains focus).

Looking at the documentation I found that there is a modifier that allows to run a command "hidden" - just I can´t figure out how to implement the modifier in the script. I think I need to "build" the script as it now has more than one line - but I can´t get it to work. I was trying to replace the line:

            DOpus.Create.Command.RunCommand("Delete FILE=""" & & "\*.ktr""")


DOpus.Create.Command.AddLine(@runmode:hide) DOpus.Create.Command.AddLine("Delete FILE=""" & & "\*.sfv""") DOpus.Create.Command.Run
I think I´m not using the create correctly - but don´t get how to correct it. When I try this my script is removed from the list - so I know it must be wrong :unamused:

Would you mind giving me another helping hint ...

Thanks - Peter

Try this..

set cmd = DOpus.Create.Command()
cmd.AddLine("Delete FILE=""" & & "\*.sfv""")

Consider throwing some looks into the manual or other scripts around here, most of them make use of the command object.
The documentation also gives some examples: ... ommand.htm

I would expect the Delete command to show a confirmation, if it starts to do so, you might add the QUIET switch. ... Delete.htm

And if your script vanishes from the list, check the script console as said, it is crucial to find out what went wrong.

Hi tbone,

thanks for the hint - but for some reason this still does not work - it simply does not do anything (while the singe line command works fine).

I tried to figure out why that is, looking at some of the scripts around and thought my code would be missing a variable declaration, so I did add a DIM cmd but that did not help.

I´d love to dig around using the CLI but I´m afraid I do not know how. I got the CLI showing, turned on the script interpreter but do not understand how to proceed from there. Running the script does not do anything as it is event triggered, I guess? I could not find a lot on CLI usage beyond opening it and running a script in it ...

Just to give a complete picture - here is the full script code I do have currently:

[code]option explicit
' Called by Directory Opus to initialize the script
Function OnInit(initData) = "ktr deleter"
initData.desc = "Löscht ktr Dateien"
initData.copyright = "PW"
initData.version = "1.0"
initData.default_enable = false
End Function

' Called after a new folder is read in a tab
Function OnAfterFolderChange(afterFolderChangeData)
' The result property of the AfterFolderChangeData object tells us if the read was successful
If afterFolderChangeData.result Then

    ' Check if the path that was read begins with D:\incoming\data        
    If Left(, 16) = "D:\incoming\data" Then
        ' The path matched, so delete .ktr files
        set mycmd = DOpus.Create.Command()
        mycmd.AddLine("Delete FILE=""" & & "\*.ktr""")
    End If
End If

End Function[/code]

Sorry for this being so painful - I guess I have a steep learning curve on DOpus scripting ... and I don´t want to give up just so close to it working perfectly.

Thanks - Peter

It's not the CLI tbone was referring to; it's the script output log (Help -> Logs -> Other Logs). This is how you can see any errors your script has encountered - if it "disappears" from the list of scripts, the output log will tell you why.

Using runmode:hide will have no effect with an internal command (which is what Delete is); it only affects external programs or DOS batch files.

The window border flashing you're experiencing is probably caused by Explorer; when you delete files to the recycle bin, Explorer seems to like to steal the focus away briefly even if it's not displaying a progress bar. If you add the NORECYCLE argument it should fix it (but you'll then have no undo available, which is something to be aware of).

Try this, works for me. o)

option explicit
'Called by Directory Opus to initialize the script
Function OnInit(initData) = "KTRDeleter"
   initData.desc = "Löscht *.ktr Dateien"
   initData.copyright = "PW"
   initData.version = "1.0"
   initData.default_enable = true
End Function

'Called after a new folder is read in a tab
Function OnAfterFolderChange(afterFolderChangeData)
    'The result property of the AfterFolderChangeData object tells us if the read was successful    
    If not afterFolderChangeData.result Then
		OnAfterFolderChange = true 'true means non-successful
		Exit function
	End if 

	'Check if the path that was read begins with D:\incoming\data        
	If Left(, 16) = "D:\incoming\data" Then
		'The path matched, so delete .ktr files
		dim mycmd : set mycmd = DOpus.Create.Command()
		'SetModifier(): corrected syntax, but it has no effect for internal commands
		'mycmd.SetModifier "runmode","hide"
		mycmd.AddLine("Delete QUIET FILE=""" & & "\*.ktr""")
	End If
	OnAfterFolderChange = false 'success
End Function

The SetModifier call was wrong, corrected but disabled, because as Jon said, it has no effect for the Delete command.
And "dim mycmd" was indeed missing. o)

Btw: This script revealed the "Delete QUIET NORECYCLE" issue once again while testing. The QUIET switch is nonfunctional in conjunction with NORECYCLE.

tbone, jon - thanks for your help - this works great now!

I do not need the QUIET switch, so I can use NORECYCLE and the flickering is gone - atually the files are deleted faster than they are shown in the lister - I don´t even get to see them anymore when entering a folder ... cool.

Also pointing me to the "other logs" was extremely helpful, now I can at least find where to look if I run in issues with scripting.

All great - love Opus :laughing: - good weekend -