Shaggy
Shaggy

Reputation: 111

Perl script that provides input to batch file

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");

Upvotes: 2

Views: 1805

Answers (2)

user966588
user966588

Reputation:

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.

Upvotes: 1

vol7ron
vol7ron

Reputation: 42089

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?

Upvotes: 0

Related Questions