2

I have a batch file "Sample.bat" which I am running using Perl: system("Sample.bat");

Sample.bat prompts for username and password, these username and password I want to give using a Perl script. For example, when I run this batch file using Perl:

Enter username:
Enter password:

These two things I have to provide using the Perl script.

Batch file:

@echo off
set /p username=Enter your username:%1
set /p password=Enter your password:%2

Perl Script:

my $username="Shaggy";
my $password="shaggy";

my $setup = "C:/sample.bat";
system("$setup $username $password");
JB.
  • 40,344
  • 12
  • 79
  • 106
Shaggy
  • 111
  • 1
  • 5
  • 11

2 Answers2

1

Use Win32::GuiTest Module

use strict;
use warnings;
use Win32::GuiTest qw[ SendKeys ];

system 1, q["start sample.bat"];

SendKeys( 'username~');

SendKeys( 'password~');

Note that ~ sign is for Enter key i.e. you don't have to use Enter key during execution.So use ~ sign in SendKeys method.
Update: Remove %1 and %2 and try above script.

0

Try:

my $username = 'yourusername';
my $password = 'yourpassword';
system("Sample.bat $username $password");

Then within the batch file, you'll use %1 for username and %2 for password.

For further information about using parameters in a batch file, see: How do I pass command line parameters to a batch file?

Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • In my scenario, I dont want to edit that batch file. Do we have any things which will do the work and batch file also is not edited. – Shaggy Mar 18 '12 at 07:26
  • no. not without some serious programming. you would have to get the window handle of the created prompt window, find the resource id's of the text fields and populate that way. I'm not sure how to do that with Perl and it's been a while since I've done anything like that with C. imo, not worth the effort, either modify the batch file, or create a copy that you can modify. – vol7ron Mar 18 '12 at 07:30
  • If you don't want to modify the batch file, perhaps you could avoid it altogether. Anything it's doing you could implement either within Perl or via system/backticks/pipe. – DavidO Mar 18 '12 at 07:33
  • @vol7ron, when I run my program using above contents, the output waits for user to press enter manually. For eg: Enter username: Shaggy, then it waits for me to press enter. Please have a look at it I want that it should go on. – Shaggy Mar 18 '12 at 07:57
  • @Shaggy: devendra's solution might do all the grunt work I was talking about above. without seeing the output, I'm not sure what the hang is - I'm assuming you're talking about the batch file – vol7ron Mar 18 '12 at 07:59
  • I kind of agree with DavidO, what is your batch process doing that your Perl can't? – vol7ron Mar 18 '12 at 08:00