Batch File Console - batch-file

How Can I hide the console of a running batch file
the batch is running from a cmd or start>run

You can try a couple of things:
Schedule it with a user other than you.
or
CMD /C START /MIN your.CMD
or
This WSH/VBScript will run your batch file in a hidden window:
'MyCmd.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
cmd = "C:\bin\scripts\MyCmd.cmd"
Return = WshShell.Run(cmd, 0, True)
set WshShell = Nothing

At the start of your batch file add these lines.
#echo off
if not defined PIL (
set PIL=1
start /min "" %~0
exit /b
)
Replace the rest of your batch here
If PIL is not defined in your batch file then the batch file will restart itself minimized. If PIL is defined in your environment then change it to something else. This will restart the batch file with PIL defined as 1 and since PIL is defined it will skip the restarting section after the file has been restarted.

make a shortcut to the batch then right click and select properties
now under the general tab there will be a run option click the arrow beside it and select minimized now click apply and ok now start the program and it will run in the background.
hope i helped you this is pretty much guaranteed to work on any bat file.

I use this software to code .exe apps and it has a command that can do this. The name of the software is Advanced Bat to Exe Converter but it doesn't only convert, you can code and compile them too. Try this coding once you download the program from here, this should also work with other exe compilers, but I am not sure.
#echo off
:menu
cls
color 9e
rem ChangeColor 14 0
rem ChangeColor 14 9
rem PrintBoxAt 6 11 15 60 1
rem PrintBoxAt 11 21 5 40 1
rem printcenter Enter window title: 13 14 9
rem getinput
set w=%result%
rem hidewindow "%w%"
goto menu

Related

Why does command START used in a batch file not start a batch file?

I made a Main batch file with the lines below:
#echo off
color 1e
title ------ Just a Test ------
start "C:\Users\%USERNAME%\Desktop\Check.bat"
:START
echo Welcome to the Game!
...
And Check.bat contains:
#echo off
if not exist "C:\Users\%USERNAME%\Desktop\Batch_System\importantFile.dll" goto ERROR
if exist "C:\Users\%USERNAME%\Desktop\Batch_System\importantFile.dll" goto CONTINUE
:ERROR
cls
echo ERROR :
echo Important file not found. please reinstall the program
pause
exit /b
:CONTINUE
cls
exit /b
When I use the command start, it starts only a command prompt with the Check.bat directory and the main batch file continues executing the game. I want to force close the main batch file if importantFile.dll doesn't exist.
Okay, let me explain: When the main batch file is executed and runs the command start to start another batch file called Check.bat, the file Check.bat checks if the file importantFile.dll exists, and if not, Check.bat displays an error message.
Does anyone know how to write Check.bat in a manner that when the .dll file does not exist, force the main batch file to exit?
First, help on every command can be get by running in a command prompt window the command with /? as parameter. start /? outputs the help of command START. call /? outputs the help of command CALL usually used to run a batch file from within a batch file. Those two commands can be used to run a batch file as explained in detail in answer on How to call a batch file that is one level up from the current directory?
Second, the command line
start "C:\Users\%USERNAME%\Desktop\Check.bat"
starts a new command process in foreground with a console window with full qualified batch file name as window title displayed in title bar at top of the console window. That is obviously not wanted by you.
Third, the Wikipedia article Windows Environment Variables lists the predefined environment variables on Windows and their default values depending on version of Windows.
In general it is better to use "%USERPROFILE%\Desktop" instead of "C:\Users\%USERNAME%\Desktop".
There is no C:\Users on Windows prior Windows Vista and Windows Server 2008 by default at all.
The users profile directory can be on a different drive than drive C:.
It is also possible that just the current user's profile directory is not in C:\Users, for example on a Windows server on which many users can logon directly and for which the server administrator decided to have the users' profile directories on a different drive than system drive making backup and cleaning operations on server much easier and is also better for security.
Well, it is also possible to have the user's desktop folder not in the user's profile directory. But that is really, really uncommon.
Fourth, on shipping a set of batch files, it is recommended to use %~dp0 to call other batch files from within a batch file because of this string referencing drive and path of argument 0 expands to full path of currently executed batch file.
The batch file path referenced with %~dp0 always ends with a backslash. Therefore concatenate %~dp0 always without an additional backslash with another batch file name, folder or file name.
See also What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?
Fifth, I suggest following for your two batch files:
Main.bat:
#echo off
color 1e
title ------ Just a Test ------
call "%~dp0Check.bat" || color && exit /B
echo Welcome to the Game!
Check.bat:
#echo off
cls
if exist "%~dp0Batch_System\importantFile.dll" exit /B 0
echo ERROR:
echo Important file not found. Please reinstall the program.
echo/
pause
exit /B 1
The batch file Check.bat is exited explicitly on important file existing with returning exit code 0 to the parent batch file Main.bat. For that reason Windows command processor continues execution of Main.bat on the command line below the command line calling the batch file Check.bat.
Otherwise Check.bat outputs an error message, waits for a pressed key by the user and exits explicitly with non zero exit code 1. The non zero exit code results in Main.bat in executing the next command after || which is COLOR to restore initial colors and next executing also EXIT with option /B to exit the execution of Main.bat.
See also:
Single line with multiple commands using Windows batch file
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
Where does GOTO :EOF return to?
exit /B without an additionally specified exit code is like goto :EOF.
The CALL command line in Main.bat could be also written as:
call "%~dp0Check.bat" || ( color & exit /B )
And Main.bat could be also written as:
#echo off
color 1e
title ------ Just a Test ------
call "%~dp0Check.bat"
if errorlevel 1 (
color
goto :EOF
)
echo Welcome to the Game!
I do not recommend using in Main.bat just EXIT instead of exit /B or goto :EOF. Just EXIT would result in exiting the current command process independent on calling hierarchy and independent on how the command process was started: with option /K to keep it running to see error messages like on opening a command prompt window and next running a batch file from within command prompt window, or with /C to close the command process after application/command/script execution finished like on double clicking on a batch file.
It is advisable to test batch files by running them from within an opened command prompt window instead of double clicking on them to see error messages on syntax errors output by cmd.exe. For that reason usage of just EXIT is counter-productive for a batch file in development. Run cmd /? in a command prompt window for help on Windows command processor itself.
Last but not least see:
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
start is asynchronous by default. Use start /wait so that main.bat can test the exit code of check.bat. Make check.bat return an appropriate exit code.
For example...
main.bat
#echo off
start /b /wait check.bat
if not %errorlevel% == 0 exit /b
echo "Welcome to the game!"
...
check.bat
#echo off
if exist "importantfile.dll" exit 0
echo ERROR: Important file not found. Please reinstall the program.
pause
exit 1
notes
Added /b to start to avoid opening another window. Change that per your preference.
You could use call instead of start but call gives the called code access to the variables of main.bat so encapsulation is improved if you use start as you did.
The logic in check.bat is simplified above. Once you identify the success path early in the script and exit, the rest of the script can assume the fail path. This saves you a few if's and labels which you might find simplifies writing and reading of similar scripts. Beware of potentially confusing multiple exit points in longer scripts though!
When choosing exit codes, 0 is a common convention for success.
The above code is just one technique - there are several other options (such as checksomething && dosomethingifok). Some useful information on return codes, and checking them, can be found in http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html
Thanks to the answer from Mofi. I've my example and exp. on this. To be short, it's about the setting of log date format. You may change the format of time , date and log. you may have the result.
why-batch-file-run-with-failure-in-windows-server

How to open a .bat file from another in a certain situation

So I am using the following code:
:begin
SET /P runscript= [Question Here]
if %runscript%==:100 goto run blahblah.bat
if %runscript%==EXIT goto :A
pause
I am trying to make there be an option to open another .bat file in a different window, but when I answer :100, command prompt just shuts down. I am trying to be as clear as possible as to what I am trying to do, but this is just a snip of a very big project I am working on.
Try start cmd /k to run the bat in new window. For goto, notice no colon before A. Lastly, your set command as written will include the space. I closed it here.
:begin
SET /P runscript=[Question Here]
if %runscript%==:100 start cmd /k blahblah.bat
if %runscript%==EXIT goto A
pause
if %runscript%==:100 goto run blahblah.bat
will attempt to find the label run on entry of :100
If you want to transfer execution to the batch blahblah.bat then remove the goto run

Running a batch (.bat) file from a VB macro (.vbs)

I am having issues getting a simple batch file (opening command prompt) to run from a vbs macro, I know this question gets asked a lot and I have tried many different suggested solutions for this without success. I am using notepad ++ to run the scripts/VB code for testing.
I have verified that the .bat file will execute correctly by itself, any suggestion on how to get this to work correctly would be greatly appreciated.
Here is my code for each instance.
VB CODE:
Sub CallBATCH()
Dim argh As Double
argh = Shell.Run "C:\Temp\cmdPrompt.bat"
End Sub
BATCH FILE:
start cmd.exe /k
EDIT: The following is the .bat file that I actually intend on calling up:
#echo OFF
title AutoCAD DWG Duplicator
color 0a
:start
set /P TemplateName=Please enter the template name you wish to copy:
set /P NumberOfCopies=Please enter how many copies you wish to make:
set Pathname="<filepath>"
cd /d %Pathname%
:init
for /L %%f in (1,1,%NumberOfCopies%) do copy %TemplateName%.dwg C:\Temp\%%f%TemplateName%.dwg
You seem to be calling a .BAT file that in turn opens a command prompt with START. I'm unclear on why you need the .BAT at all.
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /K"
Set oShell = Nothing
The /K parameter will open the command prompt window and keep it open. You've supplied no parameters for START and no commands to execute when the command prompt opens so this should do what you are looking for. More at: Run Method (Windows Script Host)

WshShell.run and major problems in Windows 7

This is a VBScript that I would like to improve. I would like four things :
1) Add a line that would rename the extension cleanup.dll to cleanup.exe, so as it can be called by the WshShell.run and executed (hidden).
2) The way it is written just below, the script opens two screens : the screen of the cleanup.exe and a blank screen, which should be hidden for the user and it is not what is happening ! How to hide the second screen ? I want to run it invisibly (the user cannot close or manipulate the second screen. It will be closed via code that is inside the cleanup.exe).**NOTE : The code below works perfectly in Windows XP, but not on Windows 7. How to make it work in all Windows platforms ?
VBSCRIPT "Second.vbs"
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
WshShell.Run "c:\cleanup.exe", 0, TRUE
Set WshShell = Nothing
BATCH "Master.bat"
#echo off
wscript Second.vbs
exit /b
3) Is there a good and reliable software to convert from VBS to EXE ?
4) The other problem I am having is that the command line below does not yield results.Must I use the second pard of the code below instead ? Why ??
Suppose that my Batch file is in located in drive f:\
If I double click on it, my screen should be then populated with information extracted from the TXT file, which actually resides in drive c:\
#echo off
set DRV=C:\August\MyProgram
cd\
cd %DRV%
type test.txt & pause>nul
#echo off
set DRV=C:\August\MyProgram
cd\
c:
cd %DRV%
type test.txt & pause>nul
Thank you in advance for the explanations and solutions
Why run with batch, vbscript is more powerfull and offers more controll.
about the visible console window
WshShell.Run "c:\cleanup.exe", 0, TRUE should hide the console while running and waits before continuing
Make sure you start your script with wscript.exe, not cscript.exe and don't use any wscript.echo
Renaming a file
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
about the batch cd, practice this in a console window
cd never changes to a drive, only to another map, drive: changes the active drive
d: => d:\>
c: => c:\> (so now if you are on c:\)
cd d:\test =>c:\ (changes your active map on d: to d:\test but since your c: drive is still the active drive you see nothing happening)
d: => d:\test (change drive to d:, you do see that the active map on drive d: is d:\test (at least with the default prompt)

batch file: hide console when using start /wait

#echo off
start /wait notepad.exe somefile.txt
if exist somefile.txt echo it exists.
this will display the normal console screen as well as notepad until notepad is closed. i don't want the console screen to appear; notepad must have focus; the 'if exist' line must not run until notepad is closed. the script is run from 'total commander' but running it from 'start/run' returns same results. the second line of code is irrelevant for this example.
cmd.exe /k does same thing.
internal batch commands only.
thanks!
win 7 64 bit
You can use Windows Script Host to run your batch file. For example, create the file "startmybatch.vbs" with the following content:
Set ws = WScript.CreateObject("WScript.Shell")
cmd = "c:\mypath\mybatch.bat"
ret = ws.Run(cmd, 0, True)
Set ws = Nothing
then run "startmybatch.vbs" directly, or using "wscript startmybatch.vbs". At least on XP this works (console window is not visible). I think this will work on all Windows versions >= Win98.

Resources