slicing out text

Hi all

I have a field which contains text ranging from something like:

/Users/thunder/Desktop/aaatest_run/DHPH 1 1.jpg

to

/Users/thunder/Desktop/aaatest_run/DHC 2527 1472.jpg

I need to extract from these strings DHPH 1 and DHC 2527 and lose all the rest. I think I need to slice and I know where my first useful character is (the 36th character is the first I want to keep). I guess I also want to trim from before the SECOND space to the end. Can anyone help me with the string function syntax of this please…

Thanks for any help

Bevil

Hi Bevil,

// The string of the file path
var pathString = '/Users/thunder/Desktop/aaatest_run/DHC 2527 1472.jpg';
// Break the string down into an array using the slash as delimiter
var pathArray = pathString.split('/');
// get the last record of the array (Arrays are zero based)
var fileName = pathArray[pathArray.length-1];

// A word is a string delimited by a space, so commas and such are not used to determine a word
// Get word 1 to 2
var nameExtract = utils.stringMiddleWords(fileName, 1, 2)
application.output(nameExtract);

Ofcourse when you store the path of a file object already you can use the getName() function to get the filename of the whole path.
I assume this question is related to your previous image reading question. So your code would look something like this:

var theFolder = plugins.file.showDirectorySelectDialog();

if ( theFolder )
{
	var theFolderContents = plugins.file.getFolderContents( theFolder,  ['jpg','JPG']);

	for ( var i = 0 ; i < theFolderContents.length ; i++ )
	{
		if ( theFolderContents[i].isFile() )
		{
			controller.newRecord();
			myImageField = plugins.images.getImage(theFolderContents[i]);
			myFileNameField = theFolderContents[i].getName();
			myFilenameExtractField = utils.stringMiddleWords(myFileNameField, 1, 2)

		}
	}
}

Hope this helps.

Robert thank you VERY much.. Perfect.

For anyone passing this way, my final code was:

var theFolder = plugins.file.showDirectorySelectDialog();
if (theFolder)
{
var theFolderContents = plugins.file.getFolderContents(theFolder, [‘jpg’,‘JPG’]);
for (var i = 0 ; i < theFolderContents.length ; i++)
{
if (theFolderContents*.isFile())*

  • {*
  • controller.newRecord()*
    _ image = plugins.images.getImage(theFolderContents*)_
    imported_filename = theFolderContents*

    * reference_number = utils.stringReplace(imported_filename, ‘/Users/thunder/Desktop/aaatest_run/’, ‘’)
    reference_number = utils.stringMiddleWords(reference_number, 1, 2)
    _ createThumbnail()
    }
    }
    }*_

Thanks again Robert