Hiding a simple batch window - windows

I've searched this and some pages came which weren't really useful or were too complicated (I am not a skilled batch file programmer!)! What I need is to run a batch file in hidden form (no console window). The batch file will not be called from external application or code. It will be clicked on by the client and then I want no console pages to be shown (only pages which are called by call command should be shown)! The batch file is exactly as follows:
#echo off
call setup.exe
IF EXIST "C:/caillog" goto tracking
IF NOT EXIST "C:/caillog" goto end
:tracking
call dotnet4.exe
call ClientService.msi
goto end
:end

I use VBScripts to open it hidden, like this:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%batchfile%"), 0, True
for e.g the bat file I want to run is run.bat then I'll do like this
objShell.Run("run.bat"), 0, True
Instead of running the batch file run the vb file.
Write it in notepad and save it as *.vbs

If your Windows system supports powershell you can place this infront of "#echo off":
cmd /c powershell -Nop -NonI -Nologo -WindowStyle Hidden "Write-Host"

As others have said, use VBS.
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\FilePath" & Chr(34), 0
Set WinScriptHost = Nothing
This is what I use.

Related

Use "cmd /c" but hide the console window

I have a shortcut running this command when clicked: cmd /c "full path to my batch file". When I use it, it does what it is supposed to do, but in the process the ugly console window pops up. Is there any way to make this command start a hidden or at least minimized window?
Use the command start with switch /min to start cmd in minimized window:
start /min cmd /c "full path to my batch file"
powershell "start <path of batch file> -Args \"<batch file args>\" -WindowStyle Hidden"
This can be placed in a separate batch file which, when called, will terminate immediately while your batch file executes in the background.
From ' Args ' to ' \" ' can be excluded if your batch file has no arguments.
' -v runAs' can be added before the end quote to run your batch file as an administrator.
I found this solution :
Create a launch.vbs file and add
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Replace "C:\Batch Files\syncfiles.bat" by your absolute or relative path file name.
Source : https://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/
Source MSDN : https://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
Right-click on the shortcut icon and choose "Properties."
On the "Shortcut" tab, choose the "Run" type you desire from the dropdown menu.
The START command has a /B switch to start without creating a window. Use START /? to read all about it.
This will create a separate process (without a window), and not block parent window so it can continue your main work:
start /b cmd /c "full path to my batch file"
Use AutoHotKey file. Download and install AutoHotKey first.
suppose you have an 1.bat
you'll create a C:\2.ahk, whose content is
Run C:\1.bat,,Hide
return
and you'll create a 3.lnk, and right click it, click property, then set the Target to
"C:\Program Files\AutoHotkey\AutoHotkey.exe" C:\2.ahk
Then you'll get what you want.
This way, you can attach the 3.lnk to your taskbar or start menu, and also change its icon.
The start method can only be used in a bat, which can't be added to taskbar or changed icon.
Create a VBScript file as a shell to start it.
' Launcher.vbs
If WScript.Arguments.Count = 0 Then
WScript.Quit 1
End If
Dim WSH
Set WSH = CreateObject("WScript.Shell")
WSH.Run "cmd /c " & WScript.Arguments(0), 0, False
You may want to embed this as a Here Document in your batch file. See heredoc for Windows batch?

I want to run a 3rd party .exe file from a .bat file without a visible command prompt

Firstly I have created VBScript to run a batch file without a visible command prompt.
Following is the code:
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run Chr(34) & ("D:\Intouch_Printer_SW\spool12\spltotext.bat") & Chr(34), 0
Set WshShell = Nothing
Following is my batch file code to run a third party .exe file.
for %%f in (C:\WINDOWS\system32\spool\PRINTERS\*.SPL) do (
echo %%~nf
start "" D:\Intouch_Printer_SW\spool12\spool.exe "C:\WINDOWS\system32\spool\PRINTERS\%%~nf.SPL" "Intouch Printer"
)
Whenever I run my .vbs code a console window pops up, I want to do all of it without a visible command prompt.
I think I am getting a black window due to this snippet:
start "" D:\Intouch_Printer_SW\spool12\spool.exe "C:\WINDOWS\system32\spool\PRINTERS\%%~nf.SPL" "Intouch Printer"
start opens the command in a new window. It isn't required for running console applications, so you can simply remove it:
for %%f in (C:\WINDOWS\system32\spool\PRINTERS\*.SPL) do (
echo %%~nf
D:\Intouch_Printer_SW\spool12\spool.exe "C:\WINDOWS\system32\spool\PRINTERS\%%~nf.SPL" "Intouch Printer"
)
In addition I would recommend running the batch script synchronously from the VBScript (3rd argument to the Run method set to True), to avoid undesired side effects should anyone ever modify the VBScript.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """D:\Intouch_Printer_SW\spool12\spltotext.bat""", 0, True

VBS (Visual Basic Script) Run Program hidden/invisible

I am trying to run a program (In this case Internet Explore) hidden/invisible from a VB script.
I found a simple script for making batch files hidden, and tried it. It didn't seem to work as the program just popped up as normal.
Here is my code so far:
CreateObject("Wscript.Shell").Run "iexplore.exe",0,True
This runs the program iexplore.exe, but doesn't run it hidden/invisible.
I am also running this VBS file from a batch file which is hidden.
The batch file simply does:
start Run.vbs
Codes of each script/batch file:
Batch File: Main file launching VBS file
#echo off
:start
start HideExecuteServerVBS.vbs (To Hide the ExecuteServerVBS.bat file when running)
timeout /NOBREAK /T 5
TASKKILL /IM iexplore.exe
timeout /NOBREAK /T 3
TASKKILL /IM iexplore.exe /F
timeout /NOBREAK /T 1800
goto start
HideExecuteServerVBS.vbs
CreateObject("Wscript.Shell").Run "ExecuteServerVBS.bat",0,True
ExecuteServerVBS.vbs
#echo off
C:\Windows\sysWOW64\csript.exe C:\Users\Admin\RunInternetProcess\vbscript.vbs
vbscript.vbs
Set ie = CreateObject("InternetExplorer.Application")
Is there a possible way to run a program invisible through a VB Script (Visual Basic Script)?
So here's the deal, if you are receiving an ActiveX error, you most likely are trying to run this vbscript under a server. Server with a 64bit platform with lack of support for direct execution of 32bit vbscripts? Yeah? If so, here's what you need to do.
Make a batch file:
ExecuteServerVBS.bat
C:\windows\sysWOW64\cscript.exe C:\path\to\your\vbscript.vbs
Put your vbscript code here:
vbscript.vbs
Set ie = CreateObject("InternetExplorer.Application")
'Go crazy
And BOOM. You're done.
UPDATE
update the file ExecuteServerVBS.vbs
#echo off
C:\Windows\sysWOW64\cscript.exe C:\Users\Admin\RunInternetProcess\vbscript.vbs > errorlog.log
update the file vbscript.vbs
On Error Resume Next
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
'Perform IE functions here......
If err.number <> 0 then wscript.echo err.number & ":" & err.description
You need to load it this way instead:
Set ie = CreateObject("InternetExplorer.Application")
' To make visible, uncomment the following line...
'ie.Visible = True
Have a look at these methods:
http://ss64.com/vb/run.html
http://ss64.com/vb/exec.html
http://ss64.com/vb/shellexecute.html
http://ss64.com/vb/syntax-elevate.html

Run Script if press the close(x) button in batch file

I want to run a script if i press the close(x) button in the batch file. Suppose batch file is running, if i press the close(x) button i want to run some script before closing the batch file incomplete run.
Use start from a different batch file and add the /WAIT argument.
As such:
#echo off
start %cd%/yourbatchfilehere.bat /WAIT
REM additional post-close code here.
Additional info here
This is, as far as I can see, the only appropriate way to get even close to solving your problem using only the system you asked us to use. Since windows' batch implementation provides no way to detect when a batch window is closing and stop it.
You might want to look into something like this. To avoid a user closing the window - just don't show them the window.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "mk123456.bat" & Chr(34), 0
Set WshShell = Nothing
You can use the following vbs script, which will run two files ('Hidden.bat' and 'Visible.bat') so that when 'Visible' is closed, 'Hidden' will run without being shown. This is based on unclemeat's answer and this post.
set WshShell=createobject("wscript.shell")
WshShell.run "Visible.bat", 1, true
WshShell.Run chr(34) & "Hidden.bat" & Chr(34), 0
However I don't know how to pass arguments (especially multi-word ones) to the batch script.

How can I hide ms-dos window when running a .bat file?

I am running a .bat file for my script (Scheduled Tak (CronJob)) per minute.
When it runs, windows command prompt appears for a fiction of time.
My batch code like this;
#ECHO OFF
C:\wamp\bin\php\php5.4.3\php.exe -f "C:\wamp\www\tst\index.php"
How can I hide this window when it run?
Use a VBScript
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("C:\yourbatch.bat"), 0, True
Run that which will run your batch file hidden.
I don't like the VBScript solution.
Download and copy nircmd.exe to your %systemroot%\system32 folder, then add this command to first line of your batch:
nircmd.exe win hide ititle "cmd.exe"
You can also change the title of your batch file terminal window by title command to avoid from hiding all cmd windows, like this:
title MyBatch
nircmd.exe win hide ititle "MyBatch"
This VBScript creates a copy of your batch file in %Temp%, executes it silently and deletes it afterwards
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim tempfolder
Const TemporaryFolder = 2
Dim WshShell, strCurDir
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
batch = "#ECHO OFF" & vbCrLf & _
"C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\tst\index.php"
Set tempfolder = fso.GetSpecialFolder(TemporaryFolder)
WshShell.CurrentDirectory = tempfolder
i=1
n=0
While n <> 1
If (fso.FileExists(i&".bat")) Then
i = i + 1
Else
n = 1
End If
Wend
Set File = fso.CreateTextFile(i&".bat",True)
File.Write batch
File.Close
Dim batchfile
batchfile = fso.GetAbsolutePathName(i&".bat")
WshShell.CurrentDirectory = strCurDir
WshShell.Run chr(34) & batchfile & Chr(34), 0, TRUE
fso.DeleteFile batchfile
I know the post is old but here is my solution, AGerman from dostips helped me code this script, its very useful.
#echo off &setlocal EnableExtensions DisableDelayedExpansion
:: Change the working directory to the directory of the batch file.
:: If the first passed argument was ~e~ (that is, the batch file was called from the VBScript)
:: then shift the parameters by one and continue at label :elevated
cd /d "%~dp0"&if "%~1"=="~e~" (shift&goto :elevated)
:: Assign the passed arguments to variable param.
set "param=%*"
:: NET SESSION fails if the batch code doesn't run with elevated permissions.
:: Assign variable __verb to "open" if the batch file runs elevated or to "runas" if it doesn't run elevated
>nul 2>&1 net session &&(set "__verb=open")||(set "__verb=runas")
:: Assign the name of the VBScript to variable vbs.
:: Assign the full name of the batch file to variable me.
:: Enable delayed variable expansion.
set "vbs=%temp%\uac.vbs"&set "me=%~f0"&setlocal enabledelayedexpansion
:: If arguments were passed, prepare them to be passed from within the VBScript by doubling the quotation marks.
if defined param set "param=!param:"=""!"
:: Write the VBScript. The ShellExecute method will run the batch file in a cmd.exe process where ~e~ will be passed as
:: first argument followed by the original arguments (saved in param). The UAC will be invoked if __verb was set to "runas".
:: Elsewise the UAC will not be invoked. For further information about the ShellExecute method see:
:: https://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx
>"!vbs!" echo CreateObject("Shell.Application").ShellExecute "!comspec!", "/c """"!me!"" ~e~ !param!""", "", "%__verb%", 0
:: Run the VBScript in a cscript.exe process.
:: Delete the VBScript file.
:: Quit the batch execution.
cscript //nologo "!vbs!"&del "!vbs!"&goto :eof
:elevated
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: Do your elevated stuff here...

Resources