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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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?

6 Comments

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.
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.
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.
@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: 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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.