简体   繁体   中英

Convert existing batch to an .exe running hidden (with no console window)

I am by no means a programmer but have been tasked with essentially just that. Ive been assigned to update a network of computers using our pre-existing distribution setup, think scccm, except can ONLY do .exe/msi files.

Anyways, the batch I wrote works fantastically; except I need it to run silently. Essentially here are the tasks it performs:

  1. Makes a log on a dedicated network share utilizing the %computername% and adds a line every time something happens, and reports the success using %errorlevel% with included time and date stamp.
  2. Checks to see if a specific program is running, if so it waits 60 minutes and trys again, IF NOT then it checks the version previously installed
  3. Depending on the version it will use robocopy to replace the outdated files with the new ones.
  4. Then sets TNS name path
  5. Configures a DB that was just installed based on computer name
  6. Modifies the registry
  7. Puts an icon on the desktop
  8. reports if all tasks completed properly in log and exits.

The batch pulls everything from a network share and saves the logs to the same share. Because on the amount of time tt took me to just make this, I am hoping I can somehow convert to a .exe or .msi, AND run is silently. I suppose I could re-write it in a language I am unfamiliar with if I had to.

I have tried a couple of methods; iexpress which works great, but no silent option; Bat To EXE converter which works but refuses to compile if I select silent; and finally 7zip using 7zS.sfx, but once compiled it refuses to run. For all of the exes I have been poking around using resource hacker to adjust the software information, icon, microsoft compatibility, etc etc etc. I was pointed towards INNO setup but found it cannot create silent flags, and most articles simply say to make an icon and put the flag in there, that is a poor solution to my problem.

Where should I even start?

There are many easy ways to run a batch file without a console window. No need to re-implement your batch file.

A simple approach that does not require any 3rd party software is using VBScript or JScript script that runs the batch file using the WScript.Shell.Run method with intWindowStyle parameter set to 0 (= Hides the window).

Quoting the answer by @Shaji to How to run a batch file without launching a “command window”? :

Save the following as wscript, for instance, hidecmd.vbs after replacing "testing.bat" with your batch file's name.

 Set oShell = CreateObject ("Wscript.Shell") Dim strArgs strArgs = "cmd /c testing.bat" oShell.Run strArgs, 0, false

The second parameter of oShell.Run is intWindowStyle value indicating the appearance of the program's window and zero value is for hidden window.

The reference is here https://docs.microsoft.com/en-us/previous-versions/d5fk67ky(v=vs.85)


For other methods, see:


If you really need an .exe, you can use Inno Setup to build it. Just run your batch file in the InitializeSetup event function and abort the "installation" afterwards. The resulting .exe will have no GUI.

[Setup]
AppName=My Program
AppVersion=1.5
; Mandatory directive, but not actually used in this "installer"
DefaultDirName={pf}\My Program

#define SetupBatchFile "setup.bat"

[Files]
; Embed the batch file to the installer
Source: "{#SetupBatchFile}"; Flags: dontcopy

[Code]

function InitializeSetup(): Boolean;
var
  BatchPath: string;
  ResultCode: Integer;
begin
  { Extract the batch file }
  ExtractTemporaryFile('{#SetupBatchFile}');
  Log('Running batch file');
  BatchPath := ExpandConstant('{tmp}\{#SetupBatchFile}');
  if not Exec(BatchPath, '', '', SW_HIDE, ewWaitUntilTerminated, ResultCode) then
  begin
    Log('Error running batch file');
  end
    else
  begin
    Log(Format('Batch file finished with exit code %d', [ResultCode]));
  end;
  Log('Exiting');
  { Prevents the actual Inno Setup installer from running }
  Result := False; 
end;

If your batch file does not need administrator privileges, add this directive to the [Setup] section:

PrivilegesRequired=lowest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM