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).
regular expression in a file name
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
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?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
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.
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.
Re: regular expression in a file name
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).Hamid wrote:If you use Firefox and copy the code, the code will execute as it is.Code: Select all
$openFile = '' Begin Perl @paths = glob('~/Documents/*.txt'); $openFile = $paths[0]; End Open $openFile
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.
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');
$BEGOFFILENAME = Read Selection
and then substituting this into the Perl macro, like
Code: Select all
@paths = glob('~/Documents/$BEGOFFILENAME*.txt');
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
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 ö?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");