.bat文件怎么能没有第三方工具转换为.exe文件

有很多原因想要将.bat转换为.exe – 隐藏/模糊实现,密码,资源path,从batch file创build服务…主要是为了使您的工作看起来更复杂比实际重要。

不想使用第三方工具也有很多原因。

那么如果你想在没有外部软件的情况下将batch file转换成.exe文件呢? (转换是引号,因为我不认为有真正的方法来编译batch file到可执行文件。有太多滥用麻烦的技巧和错误广泛使用,所有我知道的工具实际上创build一个临时的.bat文件,然后叫它 )

一个非常明显的方法是使用IEXPRESS – 这个古老的内置工具可以创build自解压包,并且能够执行后解压缩命令。 所以这里是IEXPRESS sed-directive /.bat文件,它创build了一个自带解压缩的.exe文件。 它接受两个参数 – 要转换的.bat文件和目标可执行文件:

  ;@echo off ; rem https://github.com/npocmaka/batch.scripts/edit/master/hybrids/iexpress/bat2exeIEXP.bat ;if "%~2" equ "" ( ; echo usage: %~nx0 batFile.bat target.Exe ;) ;set "target.exe=%__cd__%%~2" ;set "batch_file=%~f1" ;set "bat_name=%~nx1" ;set "bat_dir=%~dp1" ;copy /y "%~f0" "%temp%\2exe.sed" >nul ;(echo()>>"%temp%\2exe.sed" ;(echo(AppLaunched=cmd.exe /c "%bat_name%")>>"%temp%\2exe.sed" ;(echo(TargetName=%target.exe%)>>"%temp%\2exe.sed" ;(echo(FILE0="%bat_name%")>>"%temp%\2exe.sed" ;(echo([SourceFiles])>>"%temp%\2exe.sed" ;(echo(SourceFiles0=%bat_dir%)>>"%temp%\2exe.sed" ;(echo([SourceFiles0])>>"%temp%\2exe.sed" ;(echo(%%FILE0%%=)>>"%temp%\2exe.sed" ;iexpress /n /q /m %temp%\2exe.sed ;del /q /f "%temp%\2exe.sed" ;exit /b 0 [Version] Class=IEXPRESS SEDVersion=3 [Options] PackagePurpose=InstallApp ShowInstallProgramWindow=0 HideExtractAnimation=1 UseLongFileName=1 InsideCompressed=0 CAB_FixedSize=0 CAB_ResvCodeSigning=0 RebootMode=N InstallPrompt=%InstallPrompt% DisplayLicense=%DisplayLicense% FinishMessage=%FinishMessage% TargetName=%TargetName% FriendlyName=%FriendlyName% AppLaunched=%AppLaunched% PostInstallCmd=%PostInstallCmd% AdminQuietInstCmd=%AdminQuietInstCmd% UserQuietInstCmd=%UserQuietInstCmd% SourceFiles=SourceFiles [Strings] InstallPrompt= DisplayLicense= FinishMessage= FriendlyName=- PostInstallCmd=<None> AdminQuietInstCmd= UserQuietInstCmd= 

例:

 bat2exeIEXP.bat myBatFile.bat MyExecutable.exe 

这实际上应该在每台Windows机器上工作,但有一个主要的限制 – 你不能传递参数到创build的.exe文件

所以另一个可能的方法是看看.NET编译器(几乎每一个赢得的机器都应该可用)。我selectJscript.net 。 这是一个混合的jscript.net / .bat脚本,它将读取.batch文件内容。将创build另一个带有.bat文件内容的j​​script.net,编译完成后将在临时文件夹中创build一个新的bat文件,并将其称为。接受命令行参数(解释可能看起来很复杂,但实际上很简单):

 @if (@X)==(@Y) @end /* JScript comment @echo off setlocal for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\*jsc.exe"') do ( set "jsc=%%v" ) if not exist "%~n0.exe" ( "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0" ) %~n0.exe "%jsc%" %* endlocal & exit /b %errorlevel% */ //https://github.com/npocmaka/batch.scripts/blob/master/hybrids/.net/bat2exe.bat import System; import System; import System.IO; var arguments:String[] = Environment.GetCommandLineArgs(); if (arguments.length<3){ Console.WriteLine("Path to cmd\bat file not given"); Environment.Exit(1); } //Console.WriteLine(arguments[2]) var binName=Path.GetFileName(arguments[2])+".exe"; var batchContent:byte[]= File.ReadAllBytes(arguments[2]); var compilerLoc=arguments[1]; var content="[" for (var i=0;i<batchContent.length-1;i++){ content=content+batchContent[i]+"," } content=content+batchContent[batchContent.length-1]+"]"; var temp=Path.GetTempPath(); Console.WriteLine(temp); var dt=(new Date()).getTime(); var tempJS=temp+"\\2exe"+dt+".js"; //File.WriteAllBytes("here",batchContent); var toCompile="\r\n\ import System;\r\n\ import System.IO;\r\n\ var arguments:String[] = Environment.GetCommandLineArgs();\r\n\ //Remove the executable name from the command line\r\n\ var batCommandLIne=Environment.CommandLine.substring(arguments[0].length,Environment.CommandLine.length);\r\n\ var contet2:byte[]="+content+";\r\n\ var dt=(new Date()).getTime();\r\n\ var temp=Path.GetTempPath();\r\n\ var tempBatPath=Path.Combine(temp,arguments[0]+dt+'.bat');;\r\n\ File.WriteAllBytes(tempBatPath,contet2);\r\n\ System.Diagnostics.Process.Start('cmd.exe','/c '+' '+tempBatPath+' '+batCommandLIne);\r\n\ File.Delete(tempBatPath);\r\n\ "; File.WriteAllText(tempJS,toCompile); System.Diagnostics.Process.Start(compilerLoc,'/nologo /out:'+binName+' '+tempJS); File.Delete(tempJS); 

这是一个POC,但.NET System.Diagnostics和System.IO库function强大,可以添加隐藏的开始,encryption等function。你也可以检查jsc.exe编译选项,看看还有什么能力(如添加资源)。

我保证对.NET方法的每一个改进都赞不绝口:-)