Inserting the filename at the top of 100 files

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Roy Beatty

Inserting the filename at the top of 100 files

Post by Roy Beatty »

Greetings,

I would like to insert the name of the file with a carriage return at the beginning of the first line for all 142 files that I have open.

Two failed methods:

a) By using a macro, I can do an Edit > Insert > Filename all right, but the macro recorder does not capture "Next Window" navigation which would be necessary to set up the initial conditions for re-executing it. There also is no macro execution scope covering all open documents. And as others have noted, TextPad macros, unaccountably, will not close documents -- which would have obviated window navigation.

b) The Regular Expresions feature seems to preclude a pattern that matches all the text in a multiline document (a Select All). That would permit entering text to begin every file followed by "&" which substitutes all the matched text. I can trick TextPad into selecting all by replacing the RE:\n with ^^^ which forces all onto one line which can then be matched with ^.* However, there is no way to insert the dynamic filename into the replacement string. (The &f header macro would do quite nicely.)

My last resort is to use Microsoft Word and its built-in VBA to navigate my list of files: open the next file, insert the filename, close the file.

But surely TextPad will offer me an escape from the ignominy of using so monstrous a sledgehammer on so minute a gnat?

Thanks in advance for any advice!

Roy Beatty
Andreas

RE: Inserting the filename at the top of 100 files

Post by Andreas »

Hello Roy,

I just tried to record a macro just containing Ctrl-Tab and this worked. By repeatedly calling this macro I switched to every file I had open.
So I do not see why it does not work for you.

Another suggestion: why not write a perl script to do what you want? Shouldn't be too hard.

Andreas
Roy Beatty

RE: Inserting the filename at the top of 100 files

Post by Roy Beatty »

Thank you, Andreas!

You know, I've read CRTL-TAB probably for two years but have been confusing it with ALT-TAB all this time. Thanks for setting me straight!

If you happen to know how to operate a document-spanning macro over all open documents, I'd like to know how to just for future reference. For now I don't mind hitting a shortcut key a few dozen times.

As for Perl, it is high on my list of things to do in my copious amounts of spare time. I've has the Perl Cookbook for over a year and even read a little of it from time to time. But, until now, I did not have a practical need compelling me to climb the initially vertical Perl learning curve.

Thanks again for your reply to me and for your replies to other questions that I've read here.

Regards,

Roy
Alan Bellows

RE: Inserting the filename at the top of 100 files

Post by Alan Bellows »

Here's a Perl script that would do it... it took about 5 minutes to write and test (I'm hoping that the CODE tag works here so the indentation won't get fubared on this code snippet):

<CODE>
# filename.pl by Alan Bellows (hot_pastrami@yahoo.com)

my $dir = shift @ARGV || ".";
my $pattern = shift @ARGV || "*.txt";

$dir =~ s|\|/|;
$dir =~ s|/$||;

local $/ = undef;

foreach $file (glob("$dir/$pattern")) {
if(open(THIS_FILE, "+<$file")) {
my $currentContents = <THIS_FILE>;
my $filename = substr($file, rindex($file, "/")+1);
seek(THIS_FILE, 0, 0);
print THIS_FILE "$filename\n$currentContents";
close(THIS_FILE);
}
else { print "Failed to open $file\n"; }
}
print "\nProcess completed\n\n";
</CODE>

A few items about the script... it takes 2 command-line arguments, the directory and the file pattern (such as "*.txt" or "doc*.*"). If no pattern is supplied, it will assume "*.txt" (this default can be easily altered). If no directory is supplied, it will assume the current directory. However, you can't specify only a pattern, if you give it a pattern you need a path, too. Use just a dot (.) to indicate the current directory.

Examples:
perl filename.pl (adds the filename to *.txt in current folder)
perl filename.pl ..\docs (adds filename to *.txt in the docs folder 1 step down)
perl filename.pl . *.* (adds filename to ALL files in current folder)
perl filename.pl c:\windows\desktop *.doc (adds it to *.doc on the desktop)

To use this as a TextPad tool for docs in the current doc's folder, make you tool like this (adjusting paths as needed):

COMMAND: C:\Perl\bin\Perl.exe
PARAMETERS: C:\tools\filename.pl $filedir *.*

...or prompt for path and file pattern like this:

PARAMETERS: C:\tools\filename.pl $Prompt(Enter Path:) $Prompt(Enter Pattern:)

Good luck, lemme know if you have any questions at all.

Alan Bellows
Software Developer, Epixtech, inc.
Roy Beatty

RE: Inserting the filename at the top of 100 files

Post by Roy Beatty »

Alan,

That is *very* helpful! Having a solution translated into Perl really makes concrete how useful it will be.

Thank you very much! And sometime I may well take you up on your kind offer of help.

Regards,

Roy

rrbx (---@netscape.net)
Susan Hunt

RE: Inserting the filename at the top of 100 files

Post by Susan Hunt »

For those of us without Perl, could you please put the exe as an Addon, on

http://www.textpad.com/add-ons/index.html

Then we could all use it.

thanks
Post Reply