BAT 脚本转 EXE 工具

最近要为打包的程序做一个启动器图标, 之前是用 .bat 脚本来启动程序的, 所以想到了 bat 转 exe 的工具.

下载地址: https://github.com/tokyoneon/B2E

我找到的工具同时支持可视化界面和命令行两种调用方式.

使用方法:

可视化界面

可视化界面的就不多说了, 参数如下图所示:

命令行

命令行的用法如下:

# 先 cd 到下载 bat_2_exe_converter.exe 所在的目录

# 获取帮助
.\bat_2_exe_converter.exe -h

# 将 test.bat 转换为 test.exe
.\bat_2_exe_converter.exe /bat test.bat /exe test.exe

# 转换, 以及添加 launcher.ico 图标 (注意必须是 ico 格式)
.\bat_2_exe_converter.exe /bat test.bat /exe test.exe /icon launcher.ico

# 启动时不显示命令行窗口
.\bat_2_exe_converter.exe /bat test.bat /exe test.exe /icon launcher.ico /invisible

Python 脚本

对于 python 开发者, 可以这样封装:

  • 将这个 exe 放到脚本同目录.

  • 脚本代码如下:

    # bat_2_exe.py
    import os
    import subprocess
    import sys
    
    current_dir = os.path.dirname(os.path.abspath(__file__))
    b2e_tool = f'{current_dir}/bat_2_exe_converter.exe'
    
    def bat_2_exe(file_bat: str,
                  file_exe: str = '',
                  file_ico: str = '',
                  show_console=True):
        if not file_exe:
            file_exe = file_bat.replace('.bat', '.exe')
        command = [b2e_tool, '/bat', file_bat, '/exe', file_exe]
        if file_ico:
            command.extend(['/icon', file_ico])
        if not show_console:
            command.append('/invisible')
        subprocess.run(command)
        print('done', file_exe)
    
    if __name__ == '__main__':
        bat_2_exe(*sys.argv[1:])
    

没有第三方依赖, 你可以直接复制到你的项目中.

补充: 一些其他 bat 转 exe 的工具

下面是其他测试过的工具, 其实没有上面的好用, 但也简单介绍一下.

python gen-exe 库

这个作者的思路很巧妙, 通过一个 template (二进制) 文件, 直接替换里面的字节码为你的 bat 内容, 然后生成 exe.

优点就是速度非常快, 理论上几十毫秒就能解决. 与之相比上面的工具要 1 到 5 秒的时间, 有几个数量级的差距.

缺点是生成的 exe 无法处理含有空格的参数, 比如:

.\test.exe aaa bbb "C:/Program Files/xxx"
#   它会识别成: ['aaa', 'bbb', 'C:/Program', 'Files/xxx']

由于本人不懂 C++ 代码, 所以没法修复他的源代码. 感兴趣的人可以看下作者的 github 项目, 期望有人能解决这个问题.

参考

  • https://github.com/tokyoneon/B2E
  • https://github.com/silvandeleemput/gen-exe
  • https://blog.csdn.net/qq981378640/article/details/52980741
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值