multiple run and automatic response for each run in batch file - batch-file

I am creating a batch file to run a software several times. Each time the software is executed, a window with two choices will appear, and the default is just clicking enter. I don't want to click enter for each run, so I would like to write an automatic answer in my batch file. Is it possible?

The basic answer to your question is no.
However, with the help of the software you are launching, or an external tool, you might be able to achieve this.
If this other software accepts parameters, then maybe it provides one to automatically answer that first dialog that pops up.
Otherwise, you can use tools such as AutoIt to simulate a user click in the dialog.

Piping a carriage return works for some programs - that technique depends on the tool.
So see if this works for your program.
echo.|"c:\folder\filename.exe"

Related

windows automation and voice control using wake word

I want to use a "wake word" with my windows 10 computer, instead of a hotkey, to run a custom windows automation command.
How can I setup windows to use a "wake word" and then run a custom command?
(Also how can I easily make a windows automation script to launch a program, or find the launched program, and send some keyboard keys and maybe mouse clicks?)
welcome! so it sounds like you want to do 3 things, so it should be 3 questions. If you edit these into 3 separate posts, then you might already find the answers, also. without needing to post. I guess the secret is to break down your complex problem into smaller problems first and try for yourself first. Then share what you tried.
OK, I break it down for you here:
Use voice control on windows (using cortana) with a "wake word" instead of hardware input device
https://www.laptopmag.com/articles/enable-hey-cortana-windows-10
Make a voice command run a custom Command (using cortana)
https://www.windowscentral.com/how-run-custom-commands-using-cortana-windows-10
you want to easily send keyboard and mouse commands by executing one custom command, check out these options as a sample
https://windowsreport.com/automate-pc-tasks/
Have a nice day, and you are not too far off from really getting the true benefits out of this site. Also if you break your questions down correctly, you will get upvotes and good reputation instead of a downvote from the people who check your question to see if its ok for use with this site. If you fix your question the downvote will be fixed by whoever did it, too.

How to i make a .bat that targets a program and types in a text box

I have no idea where to start on this one. I have seen answers that are like this but I don't know how to format them for what I want. I just need to target the minecraft server while its open and close it by typing "stop" in the console. I have no code to show for but this will be in a other file so I can launch it and then have it close the program. btw I don't think I can use taskkill But anything will help ;) thankyou very much!
This is not possible using a batch file alone. There are two main ways to get input to another program in the system (in this case, java.exe):
Get your program to listen for a special signal. There are several administrative plugins for Minecraft which will run in the Java process and will do what you need.
Spoof user input to the program. In the case of a console app, you would probably use a SendKeys() based solution. See How to send input to the console as if the user is typing for some examples.
In the case of Minecraft, I think the first solution is going to be significantly easier, because the modding community has already supplied a number of solutions to this exact problem. #Frxstrem recommended mcrcon, which I suppose is as good a solution as any.
Most probably the console client you are using is scriptable and this is not good aproach.
Any way here's a sendKeys.bat. You can use it like:
call sendKeys.bat "Minecraft console title" "stop{enter}"
Batch files can't do this without calling out to something else; if you're going to call to something else, use AutoHotkey - that's what it's good at.
WinActivate "the title of the Minecraft server window"
https://www.autohotkey.com/docs/commands/WinActivate.htm
Send stop{Enter}
https://www.autohotkey.com/docs/commands/Send.htm

Batch files/Windows 7 - Open UAC prompt minimized on purpose

There are many users struggling with UAC prompts opening in background instead of foreground. Well, I want to do exactly the opposite, ie having the UAC prompt flash on the taskbar and waiting for my attention instead of dimming my whole screen and messing my workflow.
Here's my setup: I'm working on a Win7 x64 machine, and I placed this link in my Startup folder:
C:\CLUs\chp.exe "apps.bat"
chp.exe is an utility that loads batch files without showing the command prompt.
apps.bat calls several unelevated programs (easier to manage apps in this way rather than putting a bunch of links in Startup). The last line of such bat is
elevate chp "elevapps.bat"
elevate.exe is an utility that, as the name says, elevates whatever is called with issuing an UAC prompt. And, clearly, elevapps.bat contains another bunch of programs that require admin rights to start properly.
Now, for a number of reasons I'd like the UAC prompt to start minimized/in background. Ie, once I've logged in Windows, all I want to see is the taskbar containing the flashing icon of the UAC, but no dimmed screen.
Is this possible with batch files? All I know is that UAC "smartly" detects whether it is stealing focus or not and (doesn't) show up accordingly. And clearly during the startup there's nothing to steal focus from, so looks like I'm having no luck. However it also seems that badly coded software (namely not assigning HWNDs somewhere, see here and here) precisely produces this effect. Is there a way to reproduce this "bug" (I'd call it a feature, in my case) with some code in a batch file? Also using a third party utility would be fine, however I'm not a programmer.
I solved the problem by using winapiexec.exe, an utility that allows (as per site) to run WinAPI functions through command line parameters.
winapiexec shell32.dll#ShellExecuteW NULL "runas" "chp" "elevapps.bat" NULL 7

Run GUI from batch file is unprofessional?

I recently released a software to our customer, it will be installed on one machine at one location and maybe later at two other locations. It is a prototype and has to be tested.
This is a compiled Matlab GUI which runs scanning, does some image analysis and produces report. All is fine here. But I've got a complaint that batch-file I use is a thing from past DOS times, should not be used and looks unprofessional... Currently, the user should set up one path in batch file before the first use and then always run batch file. This bat-file kills some processes to avoid conflicts, including any running instances of the GUI, sets the path for results and runs the GUI. I proposed them to create a shortcut to this bat-file with a nice logo (such that they don't see the .bat extension ;)), but they are still unhappy.
What to do is probably not the main question here - client is always right and I should remove .bat somehow to make them happy. But is it really so unprofessional to release technical, not mass software using bat-files? Or is it just one person's opinion?
Personally I think if you already have a GUI, then use that to do the pre-processing. There is nothing wrong with using batch files, but using one when you have a GUI doesn't seem like the best way to do it.
Alternatively create a GUI with the same look as the main program to ask the user for the details it needs. For me it's not about professionalism, but how easy it is for the user to do what they need to do.
I see nothing wrong with using a batch file for small projects (especially during the testing phase if that is easier for you and delivers something to the client quicker). However, depending on the size of the project, it is nice to have an EXE to deliver to the client with a proper icon and what not.
I would agree with the customer that an EXE looks more professional. Whether it is or not... I'm not sure. The .bat files just seem a little slapped together whether that is true or not.
I would say this is similar to when people call PHP programs scripts because a lot of the times they are simple scirpts. But, then there are frameworks out there like Cake and Kohanna that are more than what someone would typically classify as a script but, since it's PHP there is still that connotation.
A batch file is not unprofessional. The batch "language" has disadvantages and outright problems (error handling!!) but it gets the job done and that's the point.
The problem is that it shows the internals of the program and some people are scared by this. They don't want to see anything like this and so try to find a disapproving label. The quickest solution is to hide the batch file behind a "vanity cover", or in this case an exe that hides the working from terrified eyes.
One simple possibility is to use an self-extracting zip file eg: http://www.7zsfx.info/en/

WinAPI C - RunAsUser from REDMON_USER and REDMON_SESSIONID

I installed a PostScript printer driver and have setup REDMON (redmonnt.dll) for redirecting postscript output to my program. In my rather simple c program I capture the data from STDIN and I am able to successfully save it into a .ps file. The file looks OK.
However, I want to start gsview.exe for viewing the file. If I call ShellExecute it fails in Windows 7 because of permission issues. It seems that my program is called under a different user account (LOCAL SERVICE). So I am looking for a way to run gsview.exe under a specific username (the user who initiated the print job) which is available to the program in a variable called REDMON_USER along with the SESSIONID as well.
Q: What are the minimum WinAPI calls required to start a program given a username and a sessionid?
Any code examples in C/C++, .NET would be very helpful.
EDIT: What I am trying accomplish is something very similar to redrunee (from redmonee). I don't want to use redrunee because it opens about a console window for a brief moment.
Note:
1) The program is called by the printer service as [LOCAL SERVICE] account.
2) The first parameter Username (REDMON_USER), in effect, points to the user currently looking at the screen
Look at CreateProcessAsUser.
Also look at CreateProcessWithLogonW and CreateProcess.
They are linked from the CreateProcessAsUser
EDIT In reply to comments by OP.
Follow advice from this thread.
I am copying this here verbatim, in case the original link stops working:
The same code works for us on Vista as
on XP, etc. The service is running as
the Local System.
use WTSGetActiveConsoleSessionId to get the ID of the current active
Windows session at the console (i.e.
the machine keyboard and display, as
opposed to WTS sessions).
use WTSQueryUserToken to get the token for that session.
use DuplicateTokenEx(hToken,MAXIMUM_ALLOWED,NULL,SecurityIdentification,TokenPrimary,
&hTokenDup) to duplicate that token.
use CreateEnvironmentBlock to create an environment that you will be
passing to the process.
use CreateProcessAsUser with the duplicated token and the created
environment. Actually, we use
CreateProcessAsUserW, since the A
version had some sort of bug on some
older systems.
Don't forget to CloseHandle on the various tokens, etc, and to
DestroyEnvironmentBlock the
environment.
Thank you efratian.
PS. Oh joy of Windows programming, did not do it for quite a while. Now I remember why. The only thing that is close or even worse documented is OpenSSH programming.
The documentation describes the "Run as User" feature, which seems to be exactly what you want:
Run as User is intended for launching a GUI program such as GSview locally via RedRun.

Resources