The below silent .bat file code prevents the need to have two bat files (using "goto" and ":").
It does it all in the same .bat file. Tested and confirmed working in Windows 10
Make sure you replace "C:\pathToFile\ThisBatFile.bat " with the path to this same .bat file! Keep the space after ".bat".
@echo off if [%1]==[] ( goto PreSilentCall ) else ( goto SilentCall )
:PreSilentCall REM Insert code here you want to have happen BEFORE this same .bat file is called silently REM such as setting paths like the below two lines
set WorkingDirWithSlash=%~dp0 set WorkingDirectory=%WorkingDirWithSlash:~0,-1%
REM below code will run this same file silently, but will go to the SilentCall section cd C:\Windows\System32 if exist C:\Windows\Temp\invis.vbs ( del C:\Windows\Temp\invis.vbs /f /q ) echo CreateObject("Wscript.Shell").Run "C:\pathToFile\ThisBatFile.bat " ^& WScript.Arguments(0), 0, False > C:\Windows\Temp\invis.vbs wscript.exe C:\Windows\Temp\invis.vbs Initialized if %ERRORLEVEL%==0 ( echo Successfully started SilentCall code. This command prompt can now be exited. goto Exit )
:SilentCall cd %WorkingDirectory% REM Insert code you want to be done silently. REM Make sure this section has no errors as you won't be able to tell if there are any, REM since it will be running silently. You can add a greater than symbol at the end of REM your commands in this section to output the results to a .txt file for the purpose REM of debugging this section of code.
:Exit
If your .bat file needs more than just the "Initialized" argument (which tells the bat file to go to :SilentCall section), add "^& WScript.Arguments(1)," , "^& WScript.Arguments(2)," ,etc. depending on the number of arguments, then edit the line where wscript.exe is called:
"wscript.exe C:\Windows\Temp\invis.vbs Initialized BatFileArgOne BatFileArgTwo"
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article