Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Self-logging .bat file

by PhilHibbs (Hermit)
on Aug 08, 2003 at 13:36 UTC ( #282177=snippet: print w/ replies, xml ) Need Help??

Description: A pl2bat-style framework that logs it's own usage history. Consists of a .bat file perl script that checks for duplicates, and a snippet to add to the top of the target script.

I find this useful for recalling commands that I did on previous sessions, as MS doesn't have good command history facilities.

Only works for NT+ versions of Windows, due to "%~dpnx0" and "%*" functions. Can be replaced by "%0" and "%1 %2 %3 %4 %5 %6 %7 %8 %9" respectively for Win9X, but only if the script is the first find in the path by name, i.e. if you're in c:\ and run c:\phil\foo.bat, and there is a foo.exe in an earlier directory in the path, then you're on your own. I may do a 4Dos version some time but I seldom use perl on Win9X.

@rem = '--*-Perl-*--
@echo off
perl -x "%~dpnx0" %*
goto endofperl
@rem ';
#!/usr/local/bin/perl -w
#line 8

my $line = $ENV{"LOGFOO"};
my $start=0;

while (<>) {
  chomp;
  $start=1 if m|^:endofperl|;
  if ( $start and m|^::| ) {
    exit(0) if $_ eq '::'.$line;
  }
}
exit(1); # Not found
__END__
Code to invoke is thus:
 set logfoo="%~dpnx0" %*
 call logfoo "%~dpnx0"
 if errorlevel 1 echo ::"%~dpnx0" %*>> "%~dpnx0"
 set logfoo=
 ... rest of batch script ...

:endofperl
Comment on Self-logging .bat file
Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://282177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2015-07-06 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (74 votes), past polls