Inserting characters at certain position?

Hmm, you're right and I don't know what I can do about it for it seems Opus trims leading or trailing spaces when setting the value of a variable. This seems like a bug to me, but perhaps it's not. I'll do a bit more testing and if it still looks like a bug, I'll submit a bug report on it.

@JohnZeman:

Hi John!
I posted your Code at the german Forum and a Member there (joerg765) found a very simple solution that really works with Spaces:

Rename REGEXP PATTERN "({dlgstring|Enter one . for each character from left})(.*)" TO "\1{dlgstring|Enter Text to insert:}\2"

Does anybody know how to translate "number" to "number of dots" to give it the finishing Touch...?

Yes, that certainly solves it. :smiley:
I got so involved in trying to figure out why the leading/trailing spaces were being trimmed when assigned as a value to a variable, that it didn't occur to me to rewrite the command to not use variables at all.

I'm afraid I don't understand your last question about translating number to number of dots.

@JohnZeman:

What I meant with this is to get rid of the "Enter one . for each character from left" by typing just a number.
In the code the number must somehow be changed (=translated) to dots (e.g. "3" to "...").

Perhaps not a common phrase in english. In german it makes sense to say "Eine Zahl in eine Anzahl Punkte übersetzen" in this context.

A slight variation of the original (very neat) solution can be used to rename by adding a prefix..

Rename REGEXP PATTERN "()(.*)" TO "\1{dlgstring|Enter Text to insert:}\2"

I have created a 3-button rename with this command for prefix, and the original command for inserting characters in the middle. All that's missing is a command to add a suffix and I have a feeling I have seen that recently somewhere in these forums.

Regards, AB

I think you may need to use a rename script for that.

Here's one which takes some numeric inputs and might be a good starting point (even though it doesn't actually rename anything, it'll show you how to get the number into the script, and after that it'd be a standard rename script which there are a bunch of examples of):

Create Multiple Numbered Folders

This is why I first tried to solve this problem with a VBScript because you can do that with a script. But I had to give that idea up when I couldn't find a way to have a user dialog box pop up for input.

Hey thanks for that link Leo, I looked last night but missed that particular one... :exclamation: I'll take a peek at it in depth later on. :smiley:

Subsequently found this which seems to do the trick...

Rename REGEXP PATTERN "(.*)(.[^.]+)$" TO "\1{dlgstring|Enter suffix to insert:}\2"

Regards, AB

Joerg765 from the german Forum did it again:

Just by moving things round a little bit and using a temporary Name he found this tricky solution:

Insert Charcters BEHIND given Position:

@set X = {dlgstring|Insert behind Position Number:} Rename REGEXP PATTERN "(.)(.+)#{$X}" TO "\2\1" AUTORENAME Rename PATTERN "*" TO "TEMPNAME*" AUTORENAME Select TEMPNAME* Rename REGEXP PATTERN "(.+)(.)#{$X}" TO "\2\1" AUTORENAME Rename FINDREP PATTERN "TEMPNAME" TO "{dlgstring|Insert:}" AUTORENAME @NODESELECT

This one deletes characters BEHIND given Position:@set X = {dlgstring|Delete behind Position Number:} Rename REGEXP PATTERN "(.)(.+)#{$X}" TO "\2\1" AUTORENAME Rename PATTERN "*" TO "TEMPNAME*" AUTORENAME Select TEMPNAME* Rename REGEXP PATTERN "(........).(.+)#{dlgstring|Number of Charcters to Remove:}" TO "\1\2" AUTORENAME Rename PATTERN "*" TO "TEMPNAME*" AUTORENAME Select TEMPNAME* Rename REGEXP PATTERN "(.+)(.)#{$X}" TO "\2\1" AUTORENAME Rename FINDREP PATTERN "TEMPNAME" TO "\~" AUTORENAME @NODESELECT

...and here's a Variation of the first one, counting Characters from End of Filename and inserting BEFORE given Number:

@set X = {dlgstring|Insert before Position Number (counting from end of Name):} Rename TYPE=files REGEXP PATTERN "(.+)(.)(\..*)#{$X}" TO "\2\1\3" AUTORENAME Rename TYPE=dirs REGEXP PATTERN "(.+)(.)#{$X}" TO "\2\1" AUTORENAME Rename TYPE=files PATTERN "*.*" TO "*TEMPNAME.*" AUTORENAME Rename TYPE=dirs Pattern "*" TO "*TEMPNAME" AUTORENAME Select *TEMPNAME Rename TYPE=files REGEXP PATTERN "(.)(.+)(\..*)#{$X}" TO "\2\1\3" AUTORENAME Rename TYPE=dirs REGEXP PATTERN "(.)(.+)#{$X}" TO "\2\1" AUTORENAME Rename FINDREP PATTERN "TEMPNAME" TO "{dlgstring|Insert:}" AUTORENAME @NODESELECT

For me they are all fully working. Regards and many Thanks to Joerg765!

This would be so much easier if the following technique worked..

@set 0=
@set 1 = .
@set 2 = ..
@set 3 = ...
@set 4 = ....
...
...
@set numdots = {dlgstring|Insert text after this character. Enter a number:}
@set dots = {${$numdots}}
Rename REGEXP PATTERN "({$dots})(.*)" TO "\1{dlgstring|Enter text to insert:}\2"
@nodeselect

Unfortunately, iterative interpretation of variable names does not (currently) work so @set dots = {${$numdots}} simply sets {$dots} to null.

Regards, AB

@Admin:

Please edit my last Post.
The second and third Code will not work this way.
Any occurence of "JOERG765" in the code-boxes (Line 9 in second and Line 5 in third box) must be replaced by "TEMPNAME"

Ok Kundal, I've made the changes - is that correct now ?

@steve:

Yes, I tested it once again and they are all working fine now.
Thank you!

This has been a fun exercise for me. I had done some VB scripting over the past few years but up until now I had done very little scripting in Opus. I just finished the following button script code (many thanks to Leo for the link I needed to figure out the dialog box thing) and it seems to do what abr had asked for originally. To use it copy all of the code in the box below, then with Opus in customize mode, right click over a toolbar and choose PASTE.

<?xml version="1.0"?> <button backcol="none" display="both" textcol="none"> <label>Insert Text</label> <tip>Insert text a set number of characters in from left side of selected file names</tip> <icon1>#newcommand</icon1> <function type="normal"> <instruction>Rename TO=&quot;{DlgStringS|Enter number of characters from left, comma, and text to insert|2,Text to Insert}&quot;</instruction> <instruction>@nodeselect </instruction> <instruction /> <instruction>@script vbscript</instruction> <instruction>option explicit</instruction> <instruction /> <instruction>Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )</instruction> <instruction /> <instruction> Dim intComPos &apos; Location of the leftmost comma in dialog box entry</instruction> <instruction> Dim strText &apos; Extracted Text from dialog box entry</instruction> <instruction> Dim strCount &apos; Extracted number (of places from left) from dialog box entry</instruction> <instruction /> <instruction> &apos; Find the leftmost comma</instruction> <instruction> intComPos = InStr(strNewName,&quot;,&quot;)</instruction> <instruction> if intComPos &gt; 0 then</instruction> <instruction> strCount = Trim(Left(strNewName, intComPos-1))</instruction> <instruction> strText = Right(strNewName,Len(strNewName)-intComPos)</instruction> <instruction> end if</instruction> <instruction /> <instruction> Dim Dots</instruction> <instruction> Dots = &quot;&quot; </instruction> <instruction /> <instruction> Dim I</instruction> <instruction> I = 0</instruction> <instruction> While I &lt; CInt(strCount)</instruction> <instruction> Dots = Dots &amp; &quot;.&quot;</instruction> <instruction> I = I+1</instruction> <instruction> Wend</instruction> <instruction> Dots = &quot;(&quot; &amp; Dots &amp; &quot;)&quot;</instruction> <instruction> </instruction> <instruction> Dim regex</instruction> <instruction> &apos; Create a RegExp object</instruction> <instruction> Set regex = new RegExp</instruction> <instruction> regex.Pattern = Dots &amp; &quot;(.*)&quot;</instruction> <instruction> strNewName = regex.Replace(strFileName, &quot;$1&quot; &amp; strText &amp; &quot;$2&quot;) &apos;</instruction> <instruction>End Function </instruction> </function> </button>

JohnZeman, thank you very much. So we have different solutions now & also it wasn´t originally for me, i think i can use it sometimes. :laughing:

@JohnZeman:

You've done a very good job!

I was very pleased with the solution of "JOERG765" already but yours is much more comfortable using just one Dialog-Box.

Would it be possible to create a Button that deletes a given Number of Characters at certain Position with your Script?

For instance simply inserting "3,5" in the Box would delete 5 Characters behind the 3rd Position.

Now that I've got some fundamentals down about using VBScript in Opus I should be able to do that without much problem. :slight_smile:

I'll post something later on about it.

kundal I've added the new script to the Buttons & Toolbars section

http://resource.dopus.com/viewtopic.php?f=18&t=12373

@JohnZeman:

Many Thanks for your excellently working Buttons and your very fast reply!