Add A Word To Random Position Between Words In FileName

Advanced Renamer forum
#1 : 17/12-19 11:43
Tra My
Tra My
Posts: 7
How To Add A Word To Random Position Between Words (Not Character) In FileName
Example: Add word "You" To Random FileName "Hello EveryBody.txt"
I wanted result as:
You Hello EveryBody.txt
Hello You EveryBody.txt
Hello EveryBody You.txt
I am newbie and don't know how to do this. Please help me?



17/12-19 11:43
#2 : 17/12-19 14:33
Tra My
Tra My
Posts: 7
Reply to #1:
Please anyone can help me!


17/12-19 14:33
#3 : 17/12-19 14:45
David Lee
David Lee
Posts: 1125
That was fun to sort out!

You need to use a script for this one. Copy the following code into a Script window - if you want to change the word to insert at random then simply edit the first line appropriately:

var insert = "You";

var newWords="";
words = (item.name + " ").match(/.\w* /g);
len = words.length;
pos = Math.floor(Math.random() * (len + 1));
j = 0;
while (j < pos) {
newWords = newWords + words[j];
j++;
}
newWords = newWords + insert + " ";
while (j < len) {
newWords = newWords + words[j];
j++;
}
newWords = newWords.replace(/ $/, "");
return newWords;


17/12-19 14:45 - edited 17/12-19 19:31
#4 : 17/12-19 15:19
Tra My
Tra My
Posts: 7
Reply to #3:
Thanks David Lee,
Sorry, I didn't make it clear.
Suppose there are 3 files in the directory need Rename: Hello World.txt,Hi Baby.txt, I Am Fine.txt
How To Add A Word To Random Position Between Words (Not Character) In FileNames
Example: Add word "You" To Above FileNames
Result As:
You Hello World.txt
Hi You Baby.txt
I Am Fine You.txt


17/12-19 15:19
#5 : 17/12-19 15:32
David Lee
David Lee
Posts: 1125
Reply to #4:

Tra My

The script should work with any number of words provided that they are separated by spaces.

However note that I have just edited the script in my original post. The original version was corrupted by the Forum editor which interprets "i in square brackets" as an instruction to change the font to italics.

I've replaced the variable "i" with "j" in the new version, which should now work properly.



17/12-19 15:32 - edited 17/12-19 15:37
#6 : 17/12-19 15:36
Tra My
Tra My
Posts: 7
Reply to #5:
How can I run it in a window script? I am completely a newbie!
Thank you so much!


17/12-19 15:36
#7 : 17/12-19 15:53
David Lee
David Lee
Posts: 1125
Reply to #6:

Add a Script method - either click the "Add method" button above the "Renaming method list" and select "Script" from the drop-down menu or just click on "Script" under "Add batch method" at the bottom.

Then copy and paste the scripting code in the Script window. The script will run once for every filename and you should see the results in the "New Filename" column of the file-list.

You can re-run the script to get another random set of results by clicking on the "Show in window..." button and immediately clicking "Close and apply script".

You should read the User Guide for full details about how to use Advanced Renamer: www.advancedrenamer.com/user_guide

However, for more advanced help about Javascript and the advanced use of regular expressions you will need to use Internet resources (I find Google and www.w3schools.com very helpful)



17/12-19 15:53
#8 : 17/12-19 16:43
Tra My
Tra My
Posts: 7
Reply to #7:
Thanks David Lee,
Did you test it?
It show error: Invalid Script: line 0: Syntax error
After I edited
var insert = "You" become var insert = "You";
But it is still same error!


17/12-19 16:43
#9 : 17/12-19 18:21
Tra My
Tra My
Posts: 7
Reply to #8:
I tested it again after reinstall Advanced Renamer
It's working. But in case the filename contain character "'" example: I'll love.txt
=> After rename it becomes: 'll love You.txt, the character before "'" is missing.
Please help me fix it!
Thanks again!


17/12-19 18:21 - edited 17/12-19 18:22
#10 : 17/12-19 19:30
David Lee
David Lee
Posts: 1125
Reply to #9:

Just needed a small change to the regular expression - this should work.

var insert = "You";

var newWords="";
words = (item.name + " ").match(/[^ ]* /g);
len = words.length;
pos = Math.floor(Math.random() * (len + 1));
j = 0;
while (j < pos) {
newWords = newWords + words[j];
j++;
}
newWords = newWords + insert + " ";
while (j < len) {
newWords = newWords + words[j];
j++;
}
newWords = newWords.replace(/ $/, "");
return newWords;


17/12-19 19:30 - edited 17/12-19 19:31
#11 : 17/12-19 20:05
Tra My
Tra My
Posts: 7
Reply to #10:
You are awesome.
I try replace:
words = (item.name + " ").match(/\S+ /g);
It is also working!
Very nice code! Thanks David!


17/12-19 20:05