Automatically rename new files added to a specific folder

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.

So, I created both buttons with the single line of commands with the folder I want to watch / stop watching.
Added the scripts. Rebooted Dopus entirely.
Clicked the Watch button. Try pasting a file into the folder then.... Nothing. It didn't change anything. Is it because my folder is on a NAS?

But then, before posting the above, I added quotes around the PATH (ie. PATH "F:\folder name\") and now I get

 24 Mar. 2024 11:43 fsUtil.WatchChanges demo:  Error at line 97, position 5
 24 Mar. 2024 11:43 fsUtil.WatchChanges demo:  Invalid procedure call or argument (0x800a0005)

these errors...

Looking at line 97 at the file pctechtv posted, where it tries to access the cache variable in memory, you have probably clicked the Unwatch button before a Watch was triggered. If that's the case try starting a Watch first.

When I wrote the sample code, I wasn't focusing on making it bulletproof, but just demonstrating how the watchFolder & cancelWatchFolder methods work.

@pctechtv: if my suspicion above is correct, you might want to add this error-handling before line 97, something like:

// cuUnwatchFolder() - added - begin
if (!Script.vars.exists('cache')) {
    // unwatch triggered before watch - use a popup/dialog if you prefer
    $dbg('cache not initialized, probably no watch running');
    return;
}
// added - end
var cache = Script.vars.get('cache');

I will update the original script as such, too.

No. It does nothing. I clicked my "Watch" icon. The Script log didn't show anything new. Copied a jpg file in. Didn't do anything.

The two buttons are in a toolbar, above my directories tree:

image

And this is the "Scripts Management" window:
image

So, yeah. Nothing happens when I paste a new file in or save a new image into the folder, which the index currently sits at 2869 (so next image should be renamed to 2870.ext)

Because of the nature of the folder being highly personal, I'm not willing to make a video.

What I'm doing:

  1. Click my "Play" watch button.
  2. Copy a test jpg file from the root of mapped network drive F:. The original filename of this test file does not have a space.
  3. I paste the file into the watch folder.
  4. Nothing happens.

Well, I just went by the line 97, I haven't looked at the numbering logic at all.

Also I don't know if DOpus can watch NAS drives or not. Have you tried it with a local drive first?

Saying just in case, you can add as many watch/unwatch buttons for as many folders as you want obviously, so you can keep the existing buttons and duplicate them with new, local test paths.

It should work with NAS, as long as they send change notifications in general.

1 Like

I didn't at the time but now I did and it worked. Why?!

Anyway, I tested somewhere else on the F:\ drive and it worked as well.

But then I moved it in the same scenario as where the folders above are (there's a "!" in the path which is used in the fodler name to keep them on top of other folders) and... it didn't work anymore. Made double-sure the paths were fine... nope... Rebooted, clicked my button and tried again. Nope.

I'm entirely lost.

My NAS is a Synology DS1821+ NAS using the latest OS version. If that helps...

try changing the line at the top of the script from var DEBUG = false; to var DEBUG = true;. The script should show more information as to what it did last. I'm guessing it exits due to some check and you're not seeing it.

There you go:

24 Mar. 2024 18:26  Main process started.
 24 Mar. 2024 20:48 fsUtil.WatchChanges demo:  watching path: F:\[...]\script, id: 724d5acd4cb070d02d7ae7fe29596021
 24 Mar. 2024 20:48 fsUtil.WatchChanges demo:  OnFilesystemChange called
 24 Mar. 2024 20:48 fsUtil.WatchChanges demo:  change for watch id: 724d5acd4cb070d02d7ae7fe29596021, path: F:\[...]\script

"Script" is the test folder. Added a jpg file to it. It didn't rename and that's all it wrote above.

Can I see the full path of a file in the directory you are trying to use?

I won't say out exactly but there are two folders before this.

The first has two words from the English dictionnary, separated with a space (the space might be the issue, tbh). The second is a single letter. Then the "script" folder is in that single letter folder.

Indeed.

var cmdLine = "Rename IGNOREEXT " + item + " TO " + mxx

@pctechtv
Appreciate the help, could you maybe enhance on the variable names? I tried to follow your snippet but mdr, mnm, ptt, nnw, nna and far are names unseen for variables as of yet (by me), it's quite hard to understand without scrolling back and forth! o)

What does "mdr" stand for when you assign a path to that variable? MyDiRectory or what is the system behind your naming? far is FileARray maybe and ppt is.. well, I fail to come up with something for that. o)

ps: Using just Powershell and FileSystemWatcher worked for me to watch folders in the past (so you do not rely on DO running/existing on the computer). It's free to use, you still need to write some code of course, but's lot's of examples out there, like this (and there are dozens more):

In the script, it was already this. So, that does not change anything.

You probably want something like this to wrap the full paths/filenames into double quotes.

I put that in the button's script or replace the last line of code in the script with this? If in the button, before or after calling for Watch folder?

:sweat_smile: I will admit they are not the most descriptive variable names. I will do better. A lot of companies I code for mention this... :joy:

Honestly I don't know exactly, not sure what script you are using, the one from lxp or pctechtv. You need to fix or replace the line containing the Rename command. Maybe it's better to focus on what I did on that line to get the blanks handled, instead of trying to replace parts of the script, so you can apply this in whatever script / button you are using (even in the future! o).

Use single quotes to enclose strings and double quotes inside the strings to enclose the variable values added to the string with +. If you can't make it work, maybe post your button / script again.

@pctechtv
I will not hire you! o)

1 Like

Simply, could you try it with a path with no spaces, something like C:\Temp, or C:\Test. Just to confirm that is it working. Also, I repeat what I asked above...?