Page 1 of 1

Next doc macro?

Posted: Wed Nov 29, 2000 7:29 pm
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.

RE: Next doc macro?

Posted: Thu Nov 30, 2000 4:20 pm
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.

RE: Next doc macro?

Posted: Thu Nov 30, 2000 4:21 pm
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;
}

RE: Next doc macro?

Posted: Fri Dec 08, 2000 11:21 pm
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.

RE: Next doc macro?

Posted: Fri Dec 08, 2000 11:36 pm
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.

RE: Next doc macro?

Posted: Mon Dec 11, 2000 4:23 am
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