rejetto forum

Format of " (1)" added to duplicate file name?

skb · 8 · 6415

0 Members and 1 Guest are viewing this topic.

Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Is it possible to change the format of the numbers added to an uploaded file name when the menu item "Upload > Number files on upload instead of overwriting" is selected?

Currently, HFS adds " (uniqueNumber)" at the end of the name, and the embedded space is a pain for some command line file processing.

Is there any way to change the format to just "(uniqueNumber)" without the dang space?

In the [upload name] event, %item-name% has the file's original, possibly duplicate name, without the added string, " (1)", say. Is there a different %tag% that has what will be the file's name after HFS makes it unique?

Alternatively, I suppose I could use [upload completed], and search for names with " (" in them, and then rename them to remove the space, but that seems worse than fixing the name before the file is saved.

Steve


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
you can use event [upload name]
and implement your own logic.
This way HFS will see the filename is available and don't try to rename it.


Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Thanks for the reply.

At the time [Upload Name] is called, HFS has possibly not yet checked to see if the name will clash, and does not seem to indicate the proposed alternative name it will use.

That is, even if "filename" already exists in the current folder, so that HFS will indeed actually save the newly uploaded file as "filename (1)"; still, the %item-name% shown is merely "filename", not "filename (1)", within the [Upload Name] handler.

Is there an alternative %item-something% that gives the proposed name HFS will really use when it writes out this file?

If the "non-clashing" name is not available from HFS, then, for me to handle it means I'll need to replicate all of the logic that HFS already has in place to come up with a non-overlapping name. This seems both redundant and tricky.

I'd rather there was a way to suggest what separator character HFS should use before its added "(number)" suffix. Currently this separator is " ", and I'd prefer, say, "_" . With the change of separator character, then the existing file name code would fit my needs.

But, I guess this is not that big a deal. So, for now, I'll probably just ignore the issue, rather than implementing my own "check for duplicate file names" logic.

Thanks,
Steve


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
[Upload Name]  agrees to work with macros, there all variables and macros needed to test the name proposed by hfs and change it at their convenience based on the existence or not of the destination file,

http://www.rejetto.com/wiki/index.php?title=HFS:_Event_scripts

do not forget that by combining macros, it is the apparent text (such as that of a normal section) which will be used to set the new name

example to put in hfs.events
[upload name]
{.time|format=dd-mm-yyyy hh:nn.} %user% %item-name%

if the resulting name is not consistent or going to a nonexistent directory, the default name will be assigned

[upload name]
\%user%\%item-name%
« Last Edit: March 09, 2015, 11:33:06 PM by Mars »


Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Yes, thank you.

However, HFS already does an almost perfect job of checking for possible duplicate names and renaming them intelligently. I don't want to re-invent all this in macros! I'd just prefer to not have the space in the resulting name.

But, not a high priority!

Steve


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
I'd just prefer to not have the space in the resulting name.

just simply fill in the result by a macro

[upload name]
{.replace|%item-name%| |_.}

you can inspire you to Section [ajax.move|no log] in hfs.tpl to perform further analysis on the final file
« Last Edit: April 17, 2015, 09:05:17 PM by Mars »


Offline Mars

  • Operator
  • Tireless poster
  • *****
    • Posts: 2059
    • View Profile
After reconsider your first request, here's a working example, it considers overwrite the existing files according to "Number files on ...." option, otherwise every file is renamed with an increment

I define a variable {.^shortname.} contening the name file without extension, and  I completes with an increment, followed by the extension

Quote
[upload name]
{.if|{.from table|#ini|number-files-on-upload.}|{:
   {.set|tempindex|0.}
   {.set|shortname|{.substring||.%item-ext%|%item-name%.}.}
   {.set|#x|{.exists|{.force ansi|%folder%%item-name%.}.}.}
   {.if|{.^#x.}|{:{.while|#X|{:{.inc|tempindex|1.}
         {.set|fullname|{.force ansi|%folder%{.^shortname.}({.^tempindex.}).%item-ext%.}/set.}
         {.set|#x|{.exists|{.^fullname.}.}.}:}/while.}

{comment| the blue line contains the new file name formatted, it is what must be changed to your needs .}
{.^shortname.}({.^tempindex.}).%item-ext%:}|{:%item-name%:}/if.}
:}/if.}   

we can retrieve the default renaming using this line in the above script by including a space before the (

Quote
{.^shortname.} ({.^tempindex.}).%item-ext%

Copy the entire quote in your hfs.events without changing any thing except in the blue line (red caracters)
 note that empty lines are ignored

[upload name]
A BC

-->
 this will return the new name "A BC"
« Last Edit: July 31, 2015, 10:55:36 AM by Mars »


Offline skb

  • Occasional poster
  • *
    • Posts: 50
    • View Profile
Thanks! Very slick! Impressive what you can do with macros.