making a batch file run hidden/minimized without modifying the shortcut - batch-file

I'm writing an application in VB.net that creates and calls batch files. I'd like these batch files to run hidden, but since there will be no shortcuts for the files, I would need to set this in the batch code itself. How would I do this?

the vbs script in the link looks good, but if you are calling the batch files from the VB app then you can run the batch files hidden:
Dim p As New Process()
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.Arguments = "/C mybatchfile.bat"
p.StartInfo.CreateNoWindow = True
p.StartInfo.UseShellExecute = False
p.Start()
p.WaitForExit();// this line waits for the batch to finish, remove if you want to start the batch and continue your app while it runs.
Martyn

Related

Issue executing multiple Batch files within main batch file

I have 3 batch files which consume a common variable-'target'. These batch files need to be executed one after the other. I am using one main batch file where I am trying to call these three batch files. But the execution stops right after first batch job is done. If I execute these batch jobs individually within that main batch file, they gets executed fine without any issue. Not sure what's missing here.
Main batch file (MainBatch.bat) contents:
set target=OHD121
CALL C:\Users\abc\x1.bat
pause
CALL C:\Users\abc\y1.bat
pause
CALL C:\Users\abc\z1.bat
pause
I figured the reason behind the issue. in my called bat files, I have another bat file for which I wasn't using call and that's why the control wasn't returning to original call action to perform subsequent steps.

How to start Python virtual environment in .bat batch file?

I need to set up a long list of temporary environment variables every time when I start a Python Flask app on Windows 10. Now I would like to create a batch file to run with all settings in one double click. The following lines run well if I copy them and paste to cmd prompt, but I couldn't run them in the batch file.
The execution of batch file always gets tripped and exited at the 2nd line venv\scripts\activate in the batch file, which has no issue at all if I copy and paste line by line at cmd.
cd C:\py\project
venv\scripts\activate
set env1=val1
set env2=val2
set FLASK_APP=some.py
flask run
One of the many (far too many) quirks of .bat files is that if you launch another .bat file, it doesn't know where to return to.
You need to explicitly call it:
call venv\scripts\activate
you can just use
start venv\Scripts\activate
This will open up a new terminal just like that... you can pass other commands using the && or & sign.

batch file to exe to auto run when computer start

I am a new programmer, i have created a batch file to open a website but i want that batch file to run when the computer starts.
Here's the code:
#echo off
:top
start iexplore.exe http://www.website.com
timeout 3
goto top
While i do think Arescet's answer will work, i am more in favor of using Windows' Task Scheduler.
Simply create a new task :
Assign it's trigger to be At Startup :
and add a new action to Start a program giving it the path to your batch file:
I believe this is a cleaner approach which also provides you with logging and history should you decide to do more things in your batch file later on
Type run into windows search, type shell:startup in the prompt, press enter, Then, create a shortcut of your program, and move the shortcut into the startUp folder.
On note of your code, the start command should have a blank title as "", and quotes around the application name and parameters passed, like:
start "" "iexplore.exe" "http://www.website.com"

MStest fails when called as a batch file from Coded ui

I have a batch file which is used to run mstest.Clicking on the batch file executes the file just fine. However when the same file is called from a win form application, mstest fails. This behaviour seems quirky.Could anyone provide any reasons for this.
I have used the following code to call it :
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(#"D:\CodedUI\CommonAutomationFramework\Driver_batch.bat");
myProcessStartInfo.UseShellExecute = false;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
What exactly is failing? The test(s) or MSTest itself?
Anyway a batch file executes commands on the command line interpreter (cmd).
In the process you're starting, maybe you should start 'cmd.exe' instead. Read the contents of the batch file and pass them a an addiction to 'cmd.exe'
Like this:
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
(...)
Where your 'command' here is the text in the batch file.
You could also try this:
System.Diagnostics.Process.Start(#"D:\CodedUI\CommonAutomationFramework\Driver_batch.bat");

How do I call a batch file from an MSI

I have an msi installer that needs to call a few batch files to finish the install procedure. The batch file copies extra files from the installer to a few directories and then modifies permissions on several of those directories. We want to continue using the batch files because there is not a lot of time left in our development schedule. I am not using WIX.
If possible I would like to capture the output of the batch as it goes and write it to a log file.
Found bellow is the code I am using to try to run the batch file from a custom action. It opens a cmd window, runs for a while but never seems to finish. If I run the same batch files directly from the command prompt they work.
//Set the environment to the directory containing the bat files
ProcessStartInfo info = new ProcessStartInfo(batch);
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
if (!string.IsNullOrEmpty(argument))
info.Arguments = argument;
Process process = new Process();
process.StartInfo = info;
process.Start();
// Capture the standard error and standard output
What am I doing wrong?
I believe you'll need to create a custom action. See this question.
Many anti-virus programs may stop execution of .BAT files during an installation process, you should really be doing this using standard Windows Installer functionality or as a C++ Custom Action

Resources