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: 1465
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: 1465
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: 316
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: 1465
Joined: Tue Mar 08, 2005 8:39 pm


Return to Javascript Renaming


cron