So, I have this folder where I add pictures in and their names are 01.jpg, 02.jpg, 03.jpg... etc. I was wondering if there was a way that, if a new file is added to it (through the web or manually adding a file in), that it would automatically rename it to 05.jpg, then 06.jpg... But then, if there's a hole in the numbering (ie skip a number), it would always take the last file's number and add 1 to it.
I'd like the script to do this only for some picture file formats. So: jpg, jpeg, png, gif and bmp.
I cannot code for the life of me... so, if somebody would be able to help me with this, it'll be awesome.
What you are looking for is a watched folder. Directory Opus does not do this. This post explains an alternative. I use watched folders via a program called Quick Macros. Multiple programs can do the watched part of what you want. Quick Macros has a free trial to try it.
As for the script part, Directory Opus can definitely do that. I wonder if you could just have a *.dcf file sitting in that folder that triggers to run when a change is made.
Could you explain in more detail what you mean about
But then, if there's a hole in the numbering (ie skip a number), it would always take the last file's number and add 1 to it.
Are you just saying you want the numbering to be one higher than the then the last file?
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?
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.
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.
**** 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.
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).
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
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.
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):
(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