nisus.com
https://www.nisus.com/forum/

regular expression in a file name
https://www.nisus.com/forum/viewtopic.php?f=17&t=2197
Page 1 of 1

Author:  js [ 2007-04-27 02:41:10 ]
Post subject:  regular expression in a file name

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).

Author:  martin [ 2007-04-27 12:45:08 ]
Post subject: 

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:
$openFile = ''

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

Open $openFile

Author:  js [ 2007-04-28 01:58:10 ]
Post subject: 

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:
$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?

Author:  Hamid [ 2007-04-30 03:46:55 ]
Post subject:  regular expression in a file name

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.

Author:  martin [ 2007-04-30 15:11:12 ]
Post subject: 

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.

Author:  js [ 2007-05-01 04:45:26 ]
Post subject:  Re: regular expression in a file name

Hamid wrote:

Code:
$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:
@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:
@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?

Author:  martin [ 2007-05-01 12:55:52 ]
Post subject: 

Perl will only interpolate variables in a string literal if you use double quote marks, eg:
Code:
@paths = glob("~/Documents/$filePath*.txt");

Author:  js [ 2007-05-02 10:33:55 ]
Post subject: 

martin wrote:
Perl will only interpolate variables in a string literal if you use double quote marks, eg:
Code:
@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 ö?

Author:  martin [ 2007-05-02 14:07:40 ]
Post subject: 

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.

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/