batch file: hide console when using start /wait - file

#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.

Related

How do i keep the window size on my batch file after opening it in minimized mode?

I have a VBscript that runs a bunch of programs, one of them being a batch file. In the batch file i use:
Mode 50,8
This makes the window the right size and makes it look nice. However, in the cases where i open the batch file using the VBscript i want it to be minimized, that is done like this:
objShell.Run("path_of_my_batch_file"),7
The problem is that this makes the batch file completely ignore the instructions to stay at "Mode 50,8" and open the batch file in the default CMD size.
So my question is: How can i open my batch file in minimized mode using my VBS-file, but also let it keep the size?
Putting this at the top of the batch file workes:
Pause
Mode 50,8
But that means i have to press a key before it gets the right size and i defenetly dont want that. I need a way to either run the "Mode 50,8" command as soon as the batch file is no longer minimized or a way to let it keep the size after the VBS-file minimizes it.
The problem is that the mode 50,8 command desn't seem to have an effect while in minimized state.
There are
workarounds
with .appac tivate and then using sendkeys but
I'd prefer the 3rd party app NirCMD which enables the batch to minize/restore
itself.
' Q:\Test\2018\07\23\SO_51469493.vbs
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("Q:\Test\2018\07\23\SO_51469493.cmd")
:: Q:\Test\2018\07\23\SO_51469493.cmd
#Echo off
Title Min5x80
Mode 80,5
Echo now minimizing using:
Echo nircmd win min title Min5x80
Timeout /t 5 & nircmd win min title Min5x80
Timeout /t 5 & nircmd win normal title Min5x80
cls
Echo back again
Pause
This will involve a flicker when running the batch.

Bat file wont open things from desktop

I have a bat file that I would like to open another bat file minimized from the desktop. I tried doing this:
#echo off
start /min "%USERPROFILE%\Desktop\coolkids.bat"
exit
But it just opened up a blank command prompt, even though "coolkids.bat" has commands in it.
I have a windows 10 PC
The first quoted argument of start is taken to be the caption of the new window. Try the following instead, and check start /? for the full syntax.
start /min "" "%USERPROFILE%\Desktop\coolkids.bat"

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 Console

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

Resources