Page 1 of 1

regular expression in a file name

Posted: 2007-04-27 02:41:10
by js
I wrote a macro that uses the Open command, like this:

Open "~/Documents/myFolder/myFile"

Now if I don‘t know the complete name of myFile, but just let‘s say the beginning, is it possible to open it anyway, using a regular expression? (This seems to work with command lines in the terminal).

Posted: 2007-04-27 12:45:08
by martin
This is possible, but will require Perl's "glob" operator, which returns an array of matching file names. An example which opens the first text file in the user's Documents folder:

Code: Select all

$openFile = ''

Begin Perl
	@paths = glob('~/Documents/*.txt');
	$openFile = $paths[0];
End

Open $openFile

Posted: 2007-04-28 01:58:10
by js
martin wrote:This is possible, but will require Perl's "glob" operator, which returns an array of matching file names. An example which opens the first text file in the user's Documents folder:

Code: Select all

$openFile = ''

Begin Perl
	@paths = glob('~/Documents/*.txt');
	$openFile = $paths[0];
End

Open $openFile
Thanks. This was very helpful. Just one point: When I simply copied your code and pasted it into a nisus macro document, it would not execute. Only after applying Formats:Remove Attributes AND removing the empty space in the 2 lines after "Begin Perl" it worked. I am somewhat astonished because once the macro worked, I tried to add attributes like color and bold and it still executed fine. How come?

regular expression in a file name

Posted: 2007-04-30 03:46:55
by Hamid
If you use Firefox and copy the code, the code will execute as it is.
You must have used Safari, I presume; it added spaces, including 2 no-break spaces and a Control character to the code. Removing attributes was not necessary.

Posted: 2007-04-30 15:11:12
by martin
Hamid has got it- Perl doesn't seem to like the non-breaking space (or probably any other "high" UTF-8 characters) outside of string literals. I'll see if there's anything we can do.

Re: regular expression in a file name

Posted: 2007-05-01 04:45:26
by js
Hamid wrote:

Code: Select all

$openFile = ''

Begin Perl
	@paths = glob('~/Documents/*.txt');
	$openFile = $paths[0];
End

Open $openFile
If you use Firefox and copy the code, the code will execute as it is.
You must have used Safari, I presume; it added spaces, including 2 no-break spaces and a Control character to the code. Removing attributes was not necessary.
Thanks for your and everybody's help. I should insist though that removing attribute is necessary. (In fact this is why I don't use Firefox: because it is not able to get text attributes on a clipboard, for me a top priority in a Browser, but in this case the source of a little problem).

But here is a sequel to my earlier query. While Martin's script works well, what I really want to do is not to find the first file of a folder (here don with $paths[0]), but one of which I know only the beginning of the name. Of course I could manually enter it, like

Code: Select all

@paths = glob('~/Documents/BEGOFFILENAME*.txt');
But what I finally want is to use a selection of text for that. So I tried to start my Nisus macro with
$BEGOFFILENAME = Read Selection
and then substituting this into the Perl macro, like

Code: Select all

@paths = glob('~/Documents/$BEGOFFILENAME*.txt');
But this does not work. It seems that Perl cannot evaluate the scalar value within the expression. Is there a solution to this?

Posted: 2007-05-01 12:55:52
by martin
Perl will only interpolate variables in a string literal if you use double quote marks, eg:

Code: Select all

@paths = glob("~/Documents/$filePath*.txt");

Posted: 2007-05-02 10:33:55
by js
martin wrote:Perl will only interpolate variables in a string literal if you use double quote marks, eg:

Code: Select all

@paths = glob("~/Documents/$filePath*.txt");
Thanks. Finally my macro seems to work. Nearly. First I found problems with empty space in pathnames and or input and how this can be solved. Llke a pathname 'My file' has to be coded as "My\\ file". But how to deal with filenames that use letters in the higher ASCII range, like é or ö?

Posted: 2007-05-02 14:07:40
by martin
This looks like a problem with the character encoding. It appears that the glob function doesn't like to handle UTF-8 strings. I'll have to look around for a solution to this.