Need a simple Perl Program in .bat

I am in need of a very simpel perl program.  Im limmited in my programming know how.  I took 1 semister of Perl 5 in college and it seemd to be a very nice easy language... I need a basic app Im sure you experts could type out in like 10 lines of code and in 2 min ehehhe....

Below are some Scalars I have used in class that I would like you to use if you need  them...

I need it to be somefile.bat  When I run it from a comand propt it  will display  PROGAM RUNING or something on the command window.

When the .bat is run  it should start have a count down timer in seconds.... I will edit it to set the var to the actual time I want
please set it to count down from say  10min.... I will modifity the timer when I need  in the code.

At the end of timmer it should EMAIL a .txt file.   I  will edit it and provide the name of the txt file and location of the text file.
please set the file to send to c:\temp.txt

I will eddit the code for the email address I need and file I need sent.

I want it to send me an .txt file every   20-30 min.   THe text file alwasy ahs same name.


my $email = "self\@home.com";      # Your email account
my $support = "send\@isp.com,$email"; # Where Txt File is to go to addresses
my $msvr = "mail";                  # mail.isp.what ever.. this is the SMTP/POP3 Server Var


Let me know if you need more info  thanks.
LVL 1
mrchaos101Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OliWarnerCommented:
Why not install perl and do it that way?

You wont be able to do this with a bat unless you have a lot of other things to handle email which would also have to be installed.
0
lbertaccoCommented:
As far as I know there no way to send an email from a .bat without additional external tools.
So you have the following options:
- use a VBscript script in place of a batch file. So you don't need any additional utility
- or install some additional tool, such as
+ an external mail program (and then stick to batches)
+ a perl interpreter, so you can just use your script
+ an enhanced command shell, such as JPsoft (www.jpsoft.com) 4nt, which includes a sendmail program
0
mrchaos101Author Commented:
I  have perl set up on my pc now.

I have an applacation that sends me an email every when my isp is off line for x amount of item. I just dont know how to MODIFY it ot send a TEXT file vs. Making an email.

here is the app.... I am hoping sombody can help me modify it.

Please note the orgianl content of the code came from
Version 1.1, 08/23/2000, by Dave Methvin
#              See http://www.winmag.com/fixes/2000/0818.htm

I have made personal changs to it...


_____________________________________________________________________________

@rem = '
@echo off
if exist %0.bat goto NOEXT
perl %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
goto END_BATCH_FILE
:NOEXT
perl %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
goto END_BATCH_FILE
@rem ';

my $gate  = "10.10.10.10";  # Ip of ISP gateway or any IP on the net you want to ping

my $complain = 0;                  # Email if outage >= $complain minutes
my $email = "self\@home.com";      # Var for my email Address
my $support = "support\@isp.com,$email"; # Var for where were emial will be sent
my $name  = "Your Name";            # Your name for email
my $phone = "(800)555-1212";      # Your phone number
my $msvr = "mail";                  # Mail server name or IP

# All of these packages are installed by default with ActivePerl
# for Win32, available at http://www.activestate.com/. You may
# need to load your own packages for other versions.

use strict;
use Win32::OLE;
use Net::Ping;
use Net::SMTP;


# Create ping object

my $p = Net::Ping->new("icmp")
  or die("new ping failed");
my $last;

PING: {
  Win32::Sleep(1*60*1000);
  my $alive = $p->ping($gate) || $p->ping($gate);
  if ( $alive && $last ) {
    my $out = int((time - $last + 30)/60);
    status("outage ended, $out minutes");
    complain($last, $out) if $complain && $out >= $complain;
    $last = 0;
  }
  elsif ( !$alive ) {
    if ( !$last ) {
      $last = time;
      status("outage started");
    }
    redo PING;      # ping faster if we're down
  }
  Win32::Sleep(1*60*1000);
  print scalar(localtime), " [OK]\n";
  redo PING;
}

# need some way to signal exit
$p->close();
exit(0);

sub status {
  my ($hr, $dy, $mn, $yr) = (localtime)[2..5];
  open(F, sprintf(">> cm%04d%02d.txt",
    $yr+1900, $mn+1)) or warn("log file, $!");;
  print F scalar(localtime), ": ", join(" ", @_), "\n";
  print scalar(localtime), ": ", join(" ", @_), "\n";
  if ( $speak && $hr >= $speak_hr_start && $hr <= $speak_hr_end ) {
    $voice->speak("$alert $_[0]", 1);
  }
  close(F);
}

sub complain {
  my ($last, $mins) = @_;
  my $died = scalar localtime $last;

  my $smtp = Net::SMTP->new($msvr, Hello => $msvr) or
    do { warn("Net::SMTP failed for server '$msvr'\n"), return };
  $smtp->mail($email);
  $smtp->to(split(/,/, $support));
  $smtp->data(<<"BODY");
From: $email
To: $support
Subject: Service interruption

This email is being sent from
$name, $email.

A service interruption has just ended. The address $gate
became unreachable starting at $died and
was unavailable for $mins minutes.

Logs have been made.

If you have any more questions contact $name through $email
or call $phone.

BODY
  $smtp->quit or warn("SMTP quit failed");
}


__END__
:END_BATCH_FILE
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mrchaos101Author Commented:
On a side note...

this is all done in not pade of course..   when I save the file I give it a  name.bat   it runs great.

It is just I want another program to run on a different computer to send me a .txt file ever so often so I can review it.

Tim
0
OliWarnerCommented:
why not have the text in the body of the email?
0
mrchaos101Author Commented:
Ok here is the situation.

I am a DIE HARD Ever Quest fan.  I leave my player in what is called TRADER MODE while I am at work.  Basicly he has items for sale at price I set.  When something is sold  there is  a .txt file that gets a log.   The every time something is sold it is added to the END of the  txt file.  I want to be able to check it from work.  

Ever so often I want it to email me a copy of that .txt file.
0
OliWarnerCommented:
well there's a little bit of code that will read in a text file...
Just modify the "print RECORDVALUES $_;" bit to throw that into the body of an email...

open(TESTFILE,$textFilename);
binmode(TESTFILE);
open(RECORDVALUES,">$outdatafile");
binmode(RECORDVALUES);
         while(<TESTFILE>)
         {
            print RECORDVALUES $_;
         }
close (TESTFILE);
close (RECORDVALUES);
0
mrchaos101Author Commented:
WOOOT that may work..

OK

Im not good with paths....  I asume it would be

Can you show me where to put the path say..

OPEN   c:\temp\test.txt    I want to read all data in that file into the email.

0
lbertaccoCommented:
There is a simpler way: change lines
---------
$smtp->data(<<"BODY");
From: $email
To: $support
Subject: Service interruption

This email is being sent from
$name, $email.

A service interruption has just ended. The address $gate
became unreachable starting at $died and
was unavailable for $mins minutes.

Logs have been made.

If you have any more questions contact $name through $email
or call $phone.

BODY
--------------
to:
-------------
$path = "c:\\path_to_your_file\\file.txt"
open(TXT, "<:slurp", $path) or die "can't open $path: $!";
$smtp->data(<TXT>);
close TXT;
-------------
0
lbertaccoCommented:
However using VBScript might be easier. This script (sendfile.vbs) does it (well, should...). Remember to change email addresses, smtp server and filepath strings). By default, the smtp server configured in Outlook express is used, but you can change that uncommenting the configuration lines:
----------------- cut here (this line is not part of the script) -------------------
With CreateObject("CDO.Message")
.Subject = "Attached text file"
.Sender = "me@my.com"
.To = "myemail@hotmail.com"
.TextBody = "This is some sample message text."
'Uncomment the following line if you need to set any of the configuration fields below
'cdoconf="http://schemas.microsoft.com/cdo/configuration/"
'Uncomment the following line to force external smtp server. Default is use local SMTP service if available
'.Configuration.Fields.Item(cdoconf & "sendusing") = 2
'uncomment and update the following line if you want to set a different smtp server. Default is the one configured in OExpress
'.Configuration.Fields.Item(cdoconf & "smtpserver") = "mail.myprovider.com"
'uncomment and update the following line if you want to use a different smtp server port. Default is 25
'.Configuration.Fields.Item(cdoconf & "smtpserverport") = 25
'uncomment the following line if you have set any of the Configuration fields above here
'.Configuration.Fields.Update
.AddAttachment "c:\file.txt"
.Send
End With
0
mrchaos101Author Commented:
Tacco

The vb script is a possablity... can it be run from a command prompt?

Also I need it in a loop

I need to repeat the code every 20 min or so?
0
OliWarnerCommented:
The best way to get this running every 20 minutes is make a bat file to call compile/run the script. then call that bat file frm windows's scheduled tasks
0
lbertaccoCommented:
It can be run from the command prompt as in
C:\> myscript.vbs

I can easily wrap a loop around it, but in my opinion it's much better to schedule it to run every 20minutes using
control-panel->scheduled tasks (first create a new job with the "new scheduled task wizard" but this won't let you schedule the job every x minutes, so jsut schedule it say once a day. Then right click the newly created job, choose properties and there you can schedule it every 20 minutes)
0
mrchaos101Author Commented:
lbertacco

I like what you posted...


but the thing is the only way it triggers an email send is AFTER there has been an outtage.

any other advice on what to add or change on that?
0
lbertaccoCommented:
Maybe your default are not appropriate. Try uncommenting the configuration lines in the script I posted (as I've done in the script below) and set your smtp server hostname (plus eventually the port number too) appropriately. I hope this will solve.

Also, can you clarify "AFTER there has been an outtage" ?

----------------- cut here (this line is not part of the script) -------------------
With CreateObject("CDO.Message")
.Subject = "Attached text file"
.Sender = "me@my.com"
.To = "myemail@hotmail.com"
.TextBody = "This is some sample message text."
cdoconf="http://schemas.microsoft.com/cdo/configuration/"
.Configuration.Fields.Item(cdoconf & "sendusing") = 2
.Configuration.Fields.Item(cdoconf & "smtpserver") = "mail.myprovider.com"
.Configuration.Fields.Item(cdoconf & "smtpserverport") = 25
.Configuration.Fields.Update
.AddAttachment "c:\file.txt"
.Send
End With
0
mrchaos101Author Commented:
That script up there is a Netwokr monitor.

It is set to PING a server or IP that I enter every x amount of seconds.  If a ping is LOST it logs the loss and keeps pinging.

Once it gets a reply agian it Logs loss over and then emails the information about the outtage to me.

Win32::Sleep(1*60*1000);
  print scalar(localtime), " [OK]\n";
  redo PING;


that pings it ever 60 seconds I belive.

0
lbertaccoCommented:
Oh I understand your question. Disregard my last post which is quite meaningless  in this case.
However I'm a bit confused now. Are you trying to monitor the status of a remote server with ping, or to email yourself an Ever Quest  log files every x minutes?
0
mrchaos101Author Commented:
HEHE
Ok  what it is that i want is to eamail my self and Ever Quest.txt file every X min.

Sombody up there said that he didn't think y ou could email ne Perl... Perl is know for being good with Web/Email etc.

I have a script done in perl that moniters my connection to my ISP. It emails me when it goes down.
I posted that script hopeing he would see the code for how to send an email.

I was then hoping to get get following:

Function that looped
                   Timer starts
                         Timer Ends
                   Sends emial with the file attached.
Loop starts over.

Hope that helps.  
0
lbertaccoCommented:
You can just put a loop around the email script as in:

Do
  ...email script code here ...
  WScript.sleep 20*60*1000     ' wait for 20 minutes (20 min * 60 seconds * 1000 milliseconds)
Loop While True

or, as I already suggested, just use the Windows buildin scheduling features (which I think would be better): from control-panel->scheduled tasks create a new job with the "new scheduled task wizard" but this won't let you schedule the job every x minutes, so just schedule it say once a day. Then right click the newly created job, choose properties and there you can schedule it every 20 minutes
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.