Automatically rename new files added to a specific folder

Yes, it's pretty much that. So that it counts up with each new file added.

And I wouldn't be buying a software just for a single folder I plan to "watch".

How would I go creating that .dfc file? Like I mention, I'm not much of a scripter at all unless it's really simple.

Excuse me I stated the file was called *.dfc, but it is really *.dcf. I corrected the above so no one else gets confused.
Are you saying that you want any image file extension (jpg, jpeg, png, gif, and bmp) to take on the numbering? Or, in a folder you want all *.jpg files to be numbered in order and take on the next available number?

Yes, that. But not only jpg, but the other aforementionned file types as well.

Example:

2000.jpg
2001.jpeg
2002.jpg
2003.png
2004.png
2005.gif
2006.jpg
etc.

And, if say I deleted 2005.gif, then, the next file should be 2007.ext and not 2005.ext.

I don't know your specific use case. May be running a rename script on demand is almost as good?

Here's a script that sets up the Advanced Renamer for the files in the source.

// https://resource.dopus.com/t/automatically-rename-new-files-added-to-a-specific-folder/49835

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    if (tab.all.count == 0) return;

    var re = /^(\d+)$/;
    var maxNum = 0;

    cmd.ClearFiles();

    for (var e = new Enumerator(tab.all); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var tmp = item.name_stem.match(re);
        if (tmp) {
            var num = Number(tmp[1]);
            if (num > maxNum) maxNum = num;
        } else {
            cmd.AddFile(item);
        }
    }

    ++maxNum;

    var cmdLine = 'Rename' +
        ' ADVANCED' +
        ' PATTERN=*' +
        ' TO=[#]' +
        ' NUMBER=' + maxNum +
        ' IGNOREEXT';

    // DOpus.Output(cmdLine);
    cmd.RunCommand(cmdLine);
}
XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>49835</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/automatically-rename-new-files-added-to-a-specific-folder/49835</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (tab.all.count == 0) return;</instruction>
		<instruction />
		<instruction>    var re = /^(\d+)$/;</instruction>
		<instruction>    var maxNum = 0;</instruction>
		<instruction />
		<instruction>    cmd.ClearFiles();</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.all); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        var tmp = item.name_stem.match(re);</instruction>
		<instruction>        if (tmp) {</instruction>
		<instruction>            var num = Number(tmp[1]);</instruction>
		<instruction>            if (num &gt; maxNum) maxNum = num;</instruction>
		<instruction>        } else {</instruction>
		<instruction>            cmd.AddFile(item);</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    ++maxNum;</instruction>
		<instruction />
		<instruction>    var cmdLine = &apos;Rename&apos; +</instruction>
		<instruction>        &apos; ADVANCED&apos; +</instruction>
		<instruction>        &apos; PATTERN=*&apos; +</instruction>
		<instruction>        &apos; TO=[#]&apos; +</instruction>
		<instruction>        &apos; NUMBER=&apos; + maxNum +</instruction>
		<instruction>        &apos; IGNOREEXT&apos;;</instruction>
		<instruction />
		<instruction>    // DOpus.Output(cmdLine);</instruction>
		<instruction>    cmd.RunCommand(cmdLine);</instruction>
		<instruction>}</instruction>
	</function>
</button>

How to use buttons and scripts from this forum

If I have to run the script on demand each time, it defeats its purpose.
The folder currently exists and, when I add to it, I rename the files myself before I save them. So, if I have to go and run a script after saving them... that'll just take more time than just figuring out the next number and renaming them as I save them.

maybe u just need to combine two scripts, lxp's and leo's

Whenever I enter them from Dopus? That won't work.

I will most likely access the folder from Firefox Save As prompt to save files from the internet. I'm not depositing files from a another folder to that folder or from a device to that folder. If that was the case, that could work I suppose. Unfortunately, because I'm essentially using Windows and, if I understand it correctly, it won't trigger Leo's script and thus, the other script won't run either.

So, yeah.

**** Please note that using scripts like this and other things talked about here can reek havoc on you data. Do this at you own risk!
That being said try this...

// This code was modified from this post:
// https://resource.dopus.com/t/automatically-move-last-downloaded-file/33785/2
function OnClick(clickData)
 {
    var cmd = clickData.func.command;
	cmd.deselect = false;
	cmd.ClearFiles();
	var far = []

	var mdr = "C:\\Test2"
	var newest = null;
	var folderEnum = DOpus.FSUtil.ReadDir(mdr, false);	

	while (!folderEnum.complete)
	{
		var subj = folderEnum.next;
		far.push(String(subj.name)) // add files in the direcory to array to determine the last numberd file
		if (!subj.is_dir && subj.name != "desktop.ini")
		{
			if (newest == null || newest.access.Compare(subj.access) < 0)
			{
				newest = subj;
			}
		}		
	}
	DOpus.Output(newest.name)

	if (newest != null)
	{
		DOpus.Output(far)
		//DOpus.OUtput(clickData.func.sourcetab.path)
		var nnw = far.length 
		var nna = far[nnw - 2].split('.').slice(0, -1).join('.')
	}
	var mnm = +parseInt(nna) + +1
	var ptt = clickData.func.sourcetab.path

	// below can protect files not the chosen path
	/*if (ptt == mdr) {
		cmd.RunCommand( "Rename IGNOREEXT " + newest.name + " TO " + mnm )
	}*/
	// below will only use the the defined path
	cmd.RunCommand( "Rename IGNOREEXT " + mdr + "\\" + newest.name + " TO " + mdr + "\\" + mnm )
}

and see some instruction how to use it here.
Also I have seen some other free programs online that can watch a folder.

Instructions
http://pctechtv.com/extra/forum/dOpus/240320AutoWatchRename/index.html

from the Opus 13 manual:

File change notification

  • Dialog.WatchDir(, , )

    • Let scripts monitor a folder or file for changes via a dialog.

    • Sends a "dirchange" Msg event, with "control" set to the specified change id.

    • is a user-assigned id.

    • is the full path to a filesystem folder, or a file if the "i" flag is set.
    • are:

      • f - Monitor for file change in folder (e.g. file created).

      • d - Monitor for directory change in folder (e.g. directory created).

      • r - Recursive - monitor sub-folders.

      • a - Monitor for file attribute changes.

      • s - Monitor for file size changes.

      • w - Monitor for last write time changes.

      • i - Monitor a single file rather than a folder.

  • Dialog.CancelWatchDir()

    • Cancels monitoring set up by previous WatchDir call.

    • is the ID of the previous watch, or "*" to cancel all.

  • FSUtil.WatchChanges and FSUtil.CancelWatchChanges methods let script add-ins monitor a folder or a file for changes without a dialog:

    • When a change occurs, the new OnFilesystemChange event is triggered, with a FilesystemChangeData argument.

    • FilesystemChangeData.id provides the ID of the watcher that changed.

    • Arguments are the same as for Dialog.WatchDir and Dialog.CancelWatchDir, mentioned above.

lxp kindly gave you much of the work done. If you are familiar with jscript, you might try using the highlighted part above (or wait for someone else to do all the remaining work for you).

That looks what I need indeed, @errante !

But like I said 2 other times already, I'm not a programmer unless it's extremely dead simple.

However, I can do a bit of logic.

FSUtil.WatchChanges (f)
  if "file created"
    if "what file is it?"
      if "jpg, jpeg, png, bmp, gif"
          rename file to number
      else
          do nothing
    else
       do nothing
  else
    do nothing
FSUtil.CancelWatchChanges (*)

Basically, script watch directory. When it detects a file has been created, it checks its extension. If it's an image file, it renames it. If not, it leaves it as-is. However, if other file modification are noticed, it does nothing. It is so that if I do renaming myself, the script does not f__ks up what I'm doing manually. Once the changes are noticed and actions taken, if necessary, it stops watching until a new call is done.

I thought of Dialog.WatchDir(, , ) when I was looking at this. However, I told myself it would have to be initiated by one Directory Opus script every time. Am I correct, or is there a way to do this even when Windows starts? Thanks

If I understand FSUtil.WatchChanges / CancelWatchChanges, it's a script running that does so without a dialog box and thus without user intervention?

...might be wrong though. At least the fonction I want is there but does it behave like I want it to?

Your timing is coincidental was about to ask this question as well:
Can Dialog.WatchDir(, , ) run when there is no dialog present? I will see if I can come up with the answer.

See Errante's post above:

FSUtil.WatchChanges and FSUtil.CancelWatchChanges methods let script add-ins monitor a folder or a file for changes without a dialog:

I see... This is interesting. Sounds like a lot is possible.

Yeah. Seems possible but out of my league to code on my own.

I got to look at this more… man… a lot here. I love it, though. BigUp to cyilmaz who made a super example to use right away. The example helps in understanding Script Add-in as well. BigUp to lxp, who made a great example of a concise way to traverse numbers in Directory Opus code. The example here probably has a lot of room for improvement. I only mean the little part I added. However, it seems to be working well. The regex can always be better. I have noticed that with certain file names with complex text strings, this can miss. BigUp to errante for pointing out that Directory Opus has this ability.

I am wondering what is even possible in server directories in an Opus lister. When combining scripting methods with the file handling ability Directory Opus has, its too good to be true!

**** Please note that using scripts like this and other things talked about here can reek havoc on you data. Do this at you own risk!
That being said try this...
autoNumImgfiles.js.txt (4.6 KB)
rename the file to autoNumImgfiles.js and see how to install and use it here...

nice, very nice, thank you.
but every time i put files in a watch fodler i got this error (after renaming):
image

(and it doesn't work on files with spaces)) )
P.S.
while I was playing with watch\unwatch, this error disappeared, but now unwatch button does not work, the script does not stop))
cuUnwatchFolder PATH "A:\123"

PPS
maybe that was in test mode) after restarting DOpus everything works fine

I cannot find the "Watch/Unwatch" button.
That is not mentionned anywhere. My setup is minimal and I got rid of most of the default toolbars.

You have to create the buttons.

The script description gives a couple of examples:

This script adds 2 commands: cuWatchFolder & cuUnwatchFolder with the same signature PATH/O

Use cuWatchFolder PATH Y:\ or cuUnwatchFolder PATH Y:\

See Raw Commands at the top of How to use buttons and scripts from this forum for how to put those commands into buttons.