Use filename in reg Exp?

Find, replace, find in files, replace in files, regular expressions

Use filename in reg Exp?

Postby JimC » Wed Sep 19, 2007 2:51 pm

Hi folks,

Great forum - no less than I'd expect for the best darn editor EVER!

Today's question: can one use the current filename (a la "%f" under Tools) with a RegExp? I've got the usual ton of files that we want to go back and edit to include a line with the (preferably unqualified) filename in it. Caveat: I am a few release behind (currently on 11.something) so if this feature has made it's way into the current product, am more than happy to upgrade (and will soon anyway) but not sure if it's in there yet or not. Failing that, any other clever ideas on how to do this? Will write a program (Java/sed/whatever) if I have to - already have a great regExp to include a line with a "<filename>" tag in it just primed for replacement, but would like to use UE if possible.

Thanks!
Jim C.
User avatar
JimC
Newbie
 
Posts: 2
Joined: Tue Sep 18, 2007 11:00 pm

Re: Use filename in reg Exp?

Postby pietzcker » Wed Sep 19, 2007 4:38 pm

Hi Jim,

a regex can't know your current filename (there are no variables in regexes other than the contents of matches). So it seems that scripting would be the right tool for this - which means that you need version 13. I guess jorrasdk will be able to answer definitely if what you want to do is possible with the scripting engine.

Regards,
Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Use filename in reg Exp?

Postby jorrasdk » Wed Sep 19, 2007 8:11 pm

Yes I can see this solved using scripts made available from UE13 and beyond. But since JimC is still on UE11 something I have this suggestion:

I think you can get around the problem with two macros:

A) One to run through all documents in a folder with a given extension
B) and one to find the position where current filename is to be inserted and insert current filename.

I will start with the second B). Name it "insertFilename" and enter this macro code:

InsertMode
ColumnModeOff
HexOff
UnixReOff
Top
Find "
Program:"
IfFound
Key LEFT ARROW
Key RIGHT ARROW
"
"
CopyFilePath
Paste
Find Up "\"
Replace ""
StartSelect
KEY HOME
Delete
EndSelect
Key BACKSPACE
EndIf


Replace Program: with whatever is needed to find the position in your files where filename should be inserted. Basicly it take the full qualified filename and path from CopyFilePath and then strips of the path part.

The first A) is an adaptation of one of Mofi's brilliant macros:

NewFile
FindInFiles "
directory" "file type" ""
Loop
Find MatchCase Up "Search complete, found "
IfFound
ExitLoop
Else
NextWindow
EndIf
EndLoop
DeleteLine
Top
NextWindow
CloseFile NoSave
Loop
IfEof
ExitLoop
EndIf
StartSelect
Key END
Open "^s"
PlayMacro 1 "
InsertFilename"
CloseFile Save
EndSelect
DeleteLine
EndLoop
CloseFile NoSave


InsertFilename is the call to the B) macro above.

Disclaimer: I have tested this using UE13 and hope it will execute on UE11. Be sure to test it first before executing the macro against your production files.

I hope it is useful for you!

Cheers!
Jorras

Edited by Mofi: Replaced GotoLineSelect 0 1 in macro "insertFilename" with appropriate code because this command is not available in UE v11.x.
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Use filename in reg Exp?

Postby JimC » Thu Sep 20, 2007 1:19 pm

Wow - amazing stuff! Thanks for the quick response.

OK - so assuming I update to current 13.x, is there some other preferred way to do this with the current version of UE?

And - I noticed in the linked-in Mofi macro a reference to this NOT working for FTP files - is that doable/possible also? THAT would be hugely beneficial for us as well if so.

Thanks,
Jim C.

BTW - Does anyone else see value in getting "editting variables", (such as %f, %F, %p, etc) added to regExp? I'm thinking of posting this as a possible product enhancement but not sure if I'm just out in left field with this one... Seems like they might have uses in various scenarios though.
User avatar
JimC
Newbie
 
Posts: 2
Joined: Tue Sep 18, 2007 11:00 pm

Re: Use filename in reg Exp?

Postby Mofi » Thu Sep 20, 2007 2:09 pm

I have written that I have not tested, if it works also with FTP files. I have never used the FTP feature of UE. So I simply don't know if the macro commands Open and Save work also for FTP files. Well, you have surely to adapt the FindInFiles command to

FindInFiles ProjFiles "" "" ""

to get a list of all project files of your FTP project. Maybe the command Open "^c" also works for FTP files. Test it and report it here. I really would be interested in.

With v13 you can use scripts which supports variables and there is already an "array" (document object) which contains all the full file names of all opened files which can be easily used in scripts. But a ReplInFiles with current file name is also not possible with the script engine. A script must also open each file and run the replace(s).
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions

cron