If the first character is a space then

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

If the first character is a space then

Postby AnnieM » Wed Jun 01, 2016 12:51 am

Hi:
I need to remove spaces and periods from files only if they are the first or last character of the file names. How do I create the if/then statements?
AnnieM
 
Posts: 1
Joined: Wed Jun 01, 2016 12:46 am

Re: If the first character is a space then

Postby Admin » Wed Jun 01, 2016 12:57 am

Hi, if/then statements are supported in Javascript Renaming since version 3.
Admin
Site Admin
 
Posts: 1895
Joined: Tue Mar 08, 2005 8:39 pm

Re: If the first character is a space then

Postby Admin » Wed Jun 01, 2016 1:06 am

Javascript:

Code: Select all
newName = name;

// removes space if first char
var char2find = " ";
var index = newName.indexOf(char2find);
if (index == 0) newName = newName.substr(1);

// removes . if first char
char2find = ".";
var index = newName.indexOf(char2find);
if (index == 0) newName = newName.substr(1);
Admin
Site Admin
 
Posts: 1895
Joined: Tue Mar 08, 2005 8:39 pm

Re: If the first character is a space then

Postby therube » Wed Jun 01, 2016 7:03 am

1:RegEx

Beginning spaces or dots:

Code: Select all
Match: (^ +|^\.+)(.*)
Replace: \2



(Then ending situation I'll have to think more about. It's even harder to create files like that, especially with no extension, & then its even hard to determine if you've created them correctly, much less if they're renamed correctly Somewhat hard to "see" spaces at the end of a filename ;-).)
therube
 
Posts: 921
Joined: Mon Jan 18, 2016 6:23 pm

Re: If the first character is a space then

Postby Admin » Wed Jun 01, 2016 7:21 am

Good one! With Javascript you can go as complex as you need in terms of matching and using if/then etc... but still RegEx are very powerful too :)
Admin
Site Admin
 
Posts: 1895
Joined: Tue Mar 08, 2005 8:39 pm

Re: If the first character is a space then

Postby trm2 » Fri Jan 31, 2020 5:46 am

Hello Admin.

The Javascript you posted works fine for prefix but does not address the original poster's request for suffix as well.

Here are the scripts that will do just that:
------------------------------------------------------------------------
For dot as prefix: File 10 (example of dot as suffix)..pdf

newName = name;

var char2find = ".";
var index = name.lastIndexOf(char2find);
newName = name.substr(index+1) + name.substr(0,index);


For <space> as prefix: File 10 (example of space as suffix) .pdf

newName = name;

var char2find = " ";
var index = name.lastIndexOf(char2find);
newName = name.substr(index+1) + name.substr(0,index);
---------------------------------------------------------------------------------------------

My question is that I would like to incorporate these into your If Then script, but I can't find the right value for index in
the statement: (index == 0) 0 -is start of filename, and I need end. That probably isn't the only problem because my script needs to conform
with yours, for example I use name.substr and you use newName.substr, but I would think that doesn't matter since each script is independent
from the other- each defining their variables in each section.

if (index == ) newName = name.substr(index+1) + name.substr(0,index);

I already tried ‘name.length-1’. and other statements but I don't know what to do at this point. I only got those two scripts working with
trial and error because I do not know how to program in javascript.

Please assist.

Thank you.
trm2
 
Posts: 155
Joined: Wed Jan 15, 2020 12:47 pm

Re: If the first character is a space then

Postby Admin » Fri Jan 31, 2020 7:03 am

Hi, best option I think to use

if (name.endsWith(".")) ...

if (name.endsWith(" ")) ...

then

newName = name.substring(0, name.length - 1);

---

requires sugar.js library in menu options
Admin
Site Admin
 
Posts: 1895
Joined: Tue Mar 08, 2005 8:39 pm

Re: If the first character is a space then

Postby therube » Fri Jan 31, 2020 3:24 pm

So after you bumped, & I read it again, & I think "how to 'see' or 'create' spaces at the end", & I sort of got fixated on it, & I think, oh, I'll think about it, later.

And then I came back, again, & actually look at what I wrote before.
And it hits me, if the earlier was OK, then would this too be OK?


Ending spaces or dots:

1:RegEx
Code: Select all
Match: (.*?)( +$|\.+$)
Replace: \1

(No time at the moment to test or think more about it...)


Code: Select all
C:\out\Bug Bug\X>dir > "  abc123  .txt"

C:\out\Bug Bug\X>dir > "  abc123  "

C:\out\Bug Bug\X>

I haven't verified that the second example does actually "have" spaces at it's end or not?
But the first does.

So " abc123 .txt" does work.
Suppose, Match: (.*?)(\s+$|\.+$) might be a little clearer.
" abc123 " (if that is in fact what it is) is not changed, so guessing it is not " abc123 " but rather " abc123"?

(So much for not having time ;-).)

Code: Select all
C:\out\Bug Bug\X>dir > "  abc123....txt"

And that too works, resulting in abc123.txt.


And in that respect, the fact that they are spaces or dots is really immaterial too (just makes it harder to visualize).
It could just be any beginning or ending char that you wanted to get rid of.


No different from, Match: (^X+|^\.+)(.*) to remove opening X's (or dots).
"XXXabc123.txt" -> "abc123.txt"

(Now I do have to go ;-).)
therube
 
Posts: 921
Joined: Mon Jan 18, 2016 6:23 pm

Re: If the first character is a space then

Postby trm2 » Sat Feb 01, 2020 5:22 am

Thanks Admin. Worked great.

BTW as for the RegEx -

Removes all dots or <spaces> at prefix and suffix regardless if at one or both ends.

Match: (.*?)(\.+|\s+)(.*)(\.+|\s+)(.*)
Replace: \1\3\5

Changing it to this will work on mixed (spaces on one end, dots on the other and visa versa) but will mess up others so be selective when choosing files or filter it.

Match: (.*?)(\.+|\s+)(.+?)(\.+|\s+$)(.*)
Replace: \1\3\5

And just like therube suggested, you could modify it to any type of character(s).
trm2
 
Posts: 155
Joined: Wed Jan 15, 2020 12:47 pm

Re: If the first character is a space then

Postby bru » Sun Feb 02, 2020 9:47 am

That's the beauty of regex, and yet another way:
^([. ]*)(.*?)[. ]*$
\2

Removes any combination of Dots-or-Spaces from beginning/end of filename:
begin .. mid .. end .. .. .. --------> begin .. mid .. end
.. .. begin .. mid .. end .. .. -----> begin .. mid .. end
... ....... .. begin .. mid .. end --> begin .. mid .. end
bru
 
Posts: 62
Joined: Wed Jan 31, 2018 7:35 pm


Return to Javascript Renaming