Next doc macro?

General questions about using TextPad

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

Post Reply
Joe Scuderi

Next doc macro?

Post by Joe Scuderi »

Can someone please tell me how to record keys that move to the next document. I have to automate putting text at the beginning and end of over 400 files. Thanks!

Next step is to write my own program, but I hope TextPad can do this.
Alan Bellows

RE: Next doc macro?

Post by Alan Bellows »

I don't know how you'd do it in Textpad, but if you wrote a program in Perl, it would be easy. It would look something like this:
-------------------

#!c:/Perl/bin/perl.exe

use strict;

my @files = glob("*.*");
my $fileContents = "";
my $file = "";
my $done = 0;
my $skipped = 0;
local $/ = undef;

my $TOP = "This will be added to the top of every file";
my $BOTTOM = "This will be added to the bottom of each";

print "\n";

ALTER_FILES: {
$file = shift @files;
if (open(FILE, "+<$file")) {
$fileContents = <FILE>;
seek(FILE, 0, 0);
print FILE "$TOP\n$fileContents\n$BOTTOM" if $fileContents;
close(FILE);
$done+=1;
}
else {
print "Skipping $_, could not open...";
$skipped+=1;
}
redo ALTER_FILES if scalar @files;
}
print "\n\nProcess completed\n$done processed, $skipped skipped.\n\n";

------------------

...then just run it in the proper folder, and you're done.

Alan Bellows
Epixtech, inc.
Alan Bellows

RE: Next doc macro?

Post by Alan Bellows »

Oops, one error:

else {
print "Skipping $_, could not open...";
$skipped+=1;
}

...should read...

else {
print "Skipping $file, could not open...";
$skipped+=1;
}
Joe Scuderi

RE: Next doc macro?

Post by Joe Scuderi »

Well, I did install PERL on my NT machine just to try this. Even managed to run the script above, with the correction, but it didn't do anything.

Process completed.
1 processed, 0 skipped.

No changes to any files.
Perl is not so easy so far, but I'll keep at it for a bit before I write something in visual basic. Thanks.
Joe Scuderi

RE: Next doc macro?

Post by Joe Scuderi »

Oh, I was wrong. The above script does add lines to the top and bottom of the first file alphabetically in a directory. Now I need to learn perl enough to figure out how to make it look at all files.
Andreas

RE: Next doc macro?

Post by Andreas »

try opendir, readdir and closedir.

as in
opendir(FOLDER, '.') or die 'cannot read dir!';
@filelist = readdir(FOLDER);
closedir(FOLDER);

foreach $file (@filelist)
{
#do with $file whatever has to be done with file (in your case: add first and last line)
}

HTH
Andreas
Post Reply