Skip to main content
Known Participant
October 2, 2012
Question

Checking last character

  • October 2, 2012
  • 1 reply
  • 660 views

What would you consider being the best way to check the last character of a string?

var folderPath = '~/Desktop/Test';

if (folderPath[folderPath.length - 1] !== '/') { folderPath += '/'; }

  or

if (folderPath.slice(-1) !== '/') { folderPath += '/'; }

I see that changePath() can also be used to keep track of this path separator.

Thanks.

This topic has been closed for replies.

1 reply

Inspiring
October 2, 2012

well,

coercing class folder to string will never include the slash ...

Folder('~/Desktop/test/').toString() + '/' //~/Desktop/test/

Folder('~/Desktop/test').toString() + '/' //~/Desktop/test/

How's the path created¿ why should you test it?

thor2112Author
Known Participant
October 2, 2012

Hi Hans,

I was trying to come up with a function to move a folder and it's contents, or a file... similar to how the OS X Finder would do it.  However, I just noticed when the files are copied over, they're not the same file size as the original which makes me think it's stripping resource forks, etc.  :-(

I'm looking now at using doScript and running ditto from the command-line with applescript.  Here's the code I was working with earlier though:

----------------------------------------

function moveItem(oldPath, newPath) {

    newPath = newPath.toString();

    //if (newPath[newPath.length - 1] !== '/') { newPath += '/'; }

    if (newPath.slice(-1) !== '/') { newPath += '/'; }

   

    var oldPathType = oldPath.constructor.name;

    if (oldPathType === 'Folder') {

        newPath += (oldPath.name + '/');

        with (Folder(newPath)) {

            if (! exists) { create(); }

        }

        for each (var i in oldPath.getFiles()) {

            if (i.constructor.name === 'Folder') {

                moveItem(i, newPath);

            } else {

                i.copy(newPath + i.name);

                i.remove();

            }

        }

        oldPath.remove();

    } else if (oldPathType === 'File') {

        oldPath.copy(newPath + oldPath.name);

        oldPath.remove();

    } else {

        throw 'Problem moving file/folder!';

    }

}

----------------------------------------