:: ANSI escape codes setup
set ESC=
set GREEN=[32m
set RED=[31m
set RESET=[0m
:: Initialize variables
set "MAIN_FILE="
set "ITEM_LIST="
set "ITEM_SELECTED="
set /a index=0
:: Display all .py and .pyw files for user to select as the main file
echo Select a main Python script to package:
for %%f in (*.py *.pyw) do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
echo !index!: %%f
)
:: Prompt user to select the main file
set /p choice=Enter the number of the main file:
set "MAIN_FILE=!ITEM_LIST[%choice%]!"
:: Confirm selection
if not defined MAIN_FILE (
echo Invalid selection.
pause
exit /b
)
:: Reset indices and gather directories and files
set /a index=0
:: Loop through all items (files and directories)
for /f "delims=" %%d in ('dir /b /a-d /a-d ^| findstr /v /i /c:"%MAIN_FILE%" /c:"%~nx0"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%d"
set "ITEM_SELECTED[!index!]=1"
)
for /f "delims=" %%d in ('dir /b /ad ^| findstr /v /i /c:"%~nx0"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%d"
set "ITEM_SELECTED[!index!]=1"
)
:MENU
cls
echo Select items to include (toggle by entering number, type "confirm" to proceed):
echo.
:: Display all items and their selection status
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
:: Get user input
set "choice="
set /p choice=Enter your choice:
:: Handle user input to toggle selection or proceed
if /i "!choice!"=="confirm" (
goto GENERATE_CMD
) else if "!choice!" neq "" (
set /a toggleIndex=!choice!
if defined ITEM_LIST[%toggleIndex%] (
if "!ITEM_SELECTED[%toggleIndex%]!"=="1" (
set "ITEM_SELECTED[%toggleIndex%]=0"
) else (
set "ITEM_SELECTED[%toggleIndex%]=1"
)
) else (
echo Invalid choice. Please enter a valid number.
)
goto MENU
)
:GENERATE_CMD
set "ADD_DATA="
for /L %%i in (1,1,!index!) do (
if "!ITEM_SELECTED[%%i]!"=="1" (
set "ADD_DATA=!ADD_DATA! --add-data ""!ITEM_LIST[%%i]!*;!ITEM_LIST[%%i]!"""
)
)
:: Generate the PyInstaller command
set CMD=python -m PyInstaller --onefile !ADD_DATA! "%MAIN_FILE%"
echo Generated command: !CMD!
:: Execute the command
%CMD%
:: Check if execution was successful
if %ERRORLEVEL% neq 0 (
echo PyInstaller failed.
pause
exit /b
)
:: Move the generated .exe file to the current directory
set exeFile=%MAIN_FILE:.py=.exe%
set exeFile=%exeFile:.pyw=.exe%
if exist "dist%exeFile%" (
move "dist%exeFile%" .
echo Moved %exeFile%
) else (
echo Could not find %exeFile%.
pause
exit /b 1
)
:: Delete the dist folder
if exist dist (
rd /s /q dist
echo Dist folder cleaned up.
)
:MENU
cls
echo Select items to include (toggle by entering number, type "confirm" to proceed, "ea" to enable all, "da" to disable all):
echo.
:: 缓存已选状态的显示,而不是每次都实时重新计算
setlocal enabledelayedexpansion
set output=
:: 单次构建整个列表输出到内存缓冲,减少在循环中频繁操作
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
set "output=!output!%ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!^n"
) else (
set "output=!output!%ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!^n"
)
)
:: 最后一次性输出结果
echo !output!
setlocal disabledelayedexpansion
:MENU
:: 用户输入的切换操作
if "!choice!" neq "" (
set /a toggleIndex=!choice!
:: 快速验证索引是否在范围内,加速检查逻辑
if !toggleIndex! geq 1 if !toggleIndex! leq !index! (
if "!ITEM_SELECTED[%toggleIndex%]!"=="1" (
set "ITEM_SELECTED[%toggleIndex%]=0"
) else (
set "ITEM_SELECTED[%toggleIndex%]=1"
)
) else (
echo Invalid choice. Please enter a valid number.
)
goto MENU
)
@echo off
setlocal enabledelayedexpansion
:: ANSI escape codes setup for colored output
set ESC=
set GREEN=[32m
set RED=[31m
set RESET=[0m
:: Initialize variables
set "MAIN_FILE="
set "ITEM_LIST="
set "ITEM_SELECTED="
set /a index=0
:: Display all .py and .pyw files for the user to select as the main file
echo Select a main Python script to package:
for %%f in (*.py *.pyw) do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
echo !index!: %%f
)
:: Prompt user to select the main file
set /p choice=Enter the number of the main file:
set "MAIN_FILE=!ITEM_LIST[%choice%]!"
:: Confirm the selection
if not defined MAIN_FILE (
echo Invalid selection.
pause
exit /b
)
:: Reset indices and gather directories and files
set /a index=0
for /f "delims=" %%f in ('dir /b /a-d ^| findstr /v /i /c:"%MAIN_FILE%" /c:"%~nx0" ^| findstr /v /i /c:".bat"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
set "ITEM_SELECTED[!index!]=1"
)
for /f "delims=" %%d in ('dir /b /ad ^| findstr /v /i /c:"%~nx0"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%d"
set "ITEM_SELECTED[!index!]=1"
)
:MENU
cls
echo Select items to include (toggle by entering number, type "confirm" to proceed, "ea" to enable all, "da" to disable all):
echo.
:: Display all items and their selection status
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
:: Get user input
set "choice="
set /p choice=Enter your choice:
:: Handle user input to toggle selection, enable all, disable all, or proceed
if /i "!choice!"=="confirm" (
goto GENERATE_CMD
) else if /i "!choice!"=="ea" (
echo Enabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=1"
)
goto MENU
) else if /i "!choice!"=="da" (
echo Disabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=0"
)
goto MENU
) else if "!choice!" neq "" (
set /a toggleIndex=!choice!
if defined ITEM_LIST[%toggleIndex%] (
if "!ITEM_SELECTED[%toggleIndex%]!"=="1" (
set "ITEM_SELECTED[%toggleIndex%]=0"
) else (
set "ITEM_SELECTED[%toggleIndex%]=1"
)
) else (
echo Invalid choice. Please enter a valid number.
)
goto MENU
)
:GENERATE_CMD
:: Build the command for PyInstaller
set "ADD_DATA="
for /L %%i in (1,1,!index!) do (
if "!ITEM_SELECTED[%%i]!"=="1" (
set "ADD_DATA=!ADD_DATA! --add-data ""!ITEM_LIST[%%i]!;!ITEM_LIST[%%i]!"""
)
)
set CMD=python -m PyInstaller --onefile !ADD_DATA! "%MAIN_FILE%"
echo Generated command: !CMD!
:: Execute the command
%CMD%
:: Check if execution was successful
if %ERRORLEVEL% neq 0 (
echo PyInstaller failed.
pause
exit /b
)
:: Move the generated .exe file to the current directory
set exeFile=%MAIN_FILE:.py=.exe%
set exeFile=%exeFile:.pyw=.exe%
if exist "dist\%exeFile%" (
move "dist\%exeFile%" .\
echo Moved %exeFile%
) else (
echo Could not find %exeFile%.
pause
exit /b 1
)
:: Delete the dist folder
if exist dist (
rd /s /q dist
echo Dist folder cleaned up.
)
echo Build and cleanup completed.
pause
@echo off
setlocal enabledelayedexpansion
:: ANSI escape codes setup for colored output
set ESC=
set GREEN=[32m
set RED=[31m
set RESET=[0m
:: Initialize variables
set "MAIN_FILE="
set "ITEM_LIST="
set "ITEM_SELECTED="
set /a index=0
:: Display all .py and .pyw files for the user to select as the main file
echo Select a main Python script to package:
for %%f in (*.py *.pyw) do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
echo !index!: %%f
)
:: Prompt user to select the main file
set /p choice=Enter the number of the main file:
set "MAIN_FILE=!ITEM_LIST[%choice%]!"
:: Confirm the selection
if not defined MAIN_FILE (
echo Invalid selection.
pause
exit /b
)
:: Reset indices and gather directories and files
set /a index=0
for /f "delims=" %%f in ('dir /b /a-d ^| findstr /v /i /c:"%MAIN_FILE%" /c:"%~nx0" ^| findstr /v /i /c:".bat"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
set "ITEM_SELECTED[!index!]=1"
)
for /f "delims=" %%d in ('dir /b /ad ^| findstr /v /i /c:"%~nx0"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%d"
set "ITEM_SELECTED[!index!]=1"
)
:MENU
cls
echo Select items to include (toggle by entering number, type "confirm" to proceed, "ea" to enable all, "da" to disable all):
echo.
:: Display all items and their selection status
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
:: Get user input
set "choice="
set /p choice=Enter your choice:
:: Handle user input (toggle, enable all, disable all, or confirm)
if /i "!choice!"=="confirm" (
goto GENERATE_CMD
) else if /i "!choice!"=="ea" (
echo Enabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=1"
)
goto MENU
) else if /i "!choice!"=="da" (
echo Disabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=0"
)
goto MENU
) else if "!choice!" neq "" (
set /a toggleIndex=!choice!
if !toggleIndex! geq 1 if !toggleIndex! leq !index! (
if "!ITEM_SELECTED[%toggleIndex%]!"=="1" (
set "ITEM_SELECTED[%toggleIndex%]=0"
) else (
set "ITEM_SELECTED[%toggleIndex%]=1"
)
:: Sleep for 50ms
>nul ping -n 1 -w 50 127.0.0.1
) else (
echo Invalid choice. Please enter a valid number.
)
goto MENU
)
:GENERATE_CMD
:: Build the command for PyInstaller
set "ADD_DATA="
for /L %%i in (1,1,!index!) do (
if "!ITEM_SELECTED[%%i]!"=="1" (
set "ADD_DATA=!ADD_DATA! --add-data ""!ITEM_LIST[%%i]!;!ITEM_LIST[%%i]!"""
)
)
set CMD=python -m PyInstaller --onefile !ADD_DATA! "%MAIN_FILE%"
echo Generated command: !CMD!
:: Execute the command
%CMD%
:: Check if execution was successful
if %ERRORLEVEL% neq 0 (
echo PyInstaller failed.
pause
exit /b
)
:: Move the generated .exe file to the current directory
set exeFile=%MAIN_FILE:.py=.exe%
set exeFile=%exeFile:.pyw=.exe%
if exist "dist\%exeFile%" (
move "dist\%exeFile%" .\
echo Moved %exeFile%
) else (
echo Could not find %exeFile%.
pause
exit /b 1
)
:: Delete the dist folder
if exist dist (
rd /s /q dist
echo Dist folder cleaned up.
)
echo Build and cleanup completed.
pause
@echo off
setlocal enabledelayedexpansion
:: ANSI escape codes setup for colored output
set ESC=
set GREEN=[32m
set RED=[31m
set RESET=[0m
:: Initialize variables
set "MAIN_FILE="
set "ITEM_LIST="
set "ITEM_SELECTED="
set /a index=0
:: Display all .py and .pyw files for the user to select as the main file
echo Select a main Python script to package:
for %%f in (*.py *.pyw) do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
echo !index!: %%f
)
:: Prompt user to select the main file
set /p choice=Enter the number of the main file:
set "MAIN_FILE=!ITEM_LIST[%choice%]!"
:: Confirm the selection
if not defined MAIN_FILE (
echo Invalid selection.
pause
exit /b
)
:: Reset indices and gather directories and files
set /a index=0
for /f "delims=" %%f in ('dir /b /a-d ^| findstr /v /i /c:"%MAIN_FILE%" /c:"%~nx0" ^| findstr /v /i /c:".bat"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
set "ITEM_SELECTED[!index!]=1"
)
for /f "delims=" %%d in ('dir /b /ad ^| findstr /v /i /c:"%~nx0"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%d"
set "ITEM_SELECTED[!index!]=1"
)
:MENU
cls
echo Select items to include (toggle by entering number, type "confirm" to proceed, "ea" to enable all, "da" to disable all):
echo.
:: Display all items and their selection status
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
:: Get user input
set "choice="
set /p choice=Enter your choice:
:: Handle double refresh for toggle, enable all, disable all, or confirm
if /i "!choice!"=="confirm" (
goto GENERATE_CMD
) else if /i "!choice!"=="ea" (
echo Enabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=1"
)
call :REFRESH
goto MENU
) else if /i "!choice!"=="da" (
echo Disabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=0"
)
call :REFRESH
goto MENU
) else if "!choice!" neq "" (
set /a toggleIndex=!choice!
if !toggleIndex! geq 1 if !toggleIndex! leq !index! (
if "!ITEM_SELECTED[%toggleIndex%]!"=="1" (
set "ITEM_SELECTED[%toggleIndex%]=0"
) else (
set "ITEM_SELECTED[%toggleIndex%]=1"
)
call :REFRESH
) else (
echo Invalid choice. Please enter a valid number.
)
goto MENU
)
:REFRESH
:: Refresh the menu twice
cls
for /L %%i in (1,1,!index!) do (
:: Re-display items' state
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
timeout /t 1 >nul
cls
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
:: Delay finished
goto :eof
:GENERATE_CMD
:: Build the command for PyInstaller
set "ADD_DATA="
for /L %%i in (1,1,!index!) do (
if "!ITEM_SELECTED[%%i]!"=="1" (
set "ADD_DATA=!ADD_DATA! --add-data ""!ITEM_LIST[%%i]!*;!ITEM_LIST[%%i]!"""
)
)
set CMD=python -m PyInstaller --onefile !ADD_DATA! "%MAIN_FILE%"
echo Generated command: !CMD!
:: Execute the command
%CMD%
:: Check if execution was successful
if %ERRORLEVEL% neq 0 (
echo PyInstaller failed.
pause
exit /b
)
:: Move the generated .exe file to the current directory
set exeFile=%MAIN_FILE:.py=.exe%
set exeFile=%exeFile:.pyw=.exe%
if exist "dist\%exeFile%" (
move "dist\%exeFile%" .\
echo Moved %exeFile%
) else (
echo Could not find %exeFile%.
pause
exit /b 1
)
:: Delete the dist folder
if exist dist (
rd /s /q dist
echo Dist folder cleaned up.
)
echo Build and cleanup completed.
pause
实现说明
1. 双次刷新逻辑
通过定义一个 :REFRESH 子例程(即 call :REFRESH),手动刷新菜单两次:
第一次刷新显示初始切换状态。
延迟大约 1 秒后(通过 timeout /t 1 命令),重新刷新一次显示最新状态。
2. 双刷新触发场景
任何需要用户操作导致选项状态变化的场景都会调用 :REFRESH,包括以下输入:
输入数字进行单项 toggle。
输入 ea 启用所有条目。
输入 da 禁用所有条目。
3. 保留全部功能
所有原始功能(如选择主文件、confirm 确认生成命令、过滤 .bat 文件等)完全保留。
体验改进
双次刷新将清晰展示状态变化,给用户更平滑的交互体验。
第一次刷新立即反映更新内容;第二次刷新再强调当前完整状态,防止遗漏。
如果还有其他功能需求或优化建议,请随时告诉我!
@echo off
setlocal enabledelayedexpansion
:: ANSI escape codes setup for colored output
set ESC=
set GREEN=[32m
set RED=[31m
set RESET=[0m
:: Initialize variables
set "MAIN_FILE="
set "ITEM_LIST="
set "ITEM_SELECTED="
set /a index=0
:: Display all .py and .pyw files for the user to select as the main file
echo Select a main Python script to package:
for %%f in (*.py *.pyw) do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
echo !index!: %%f
)
:: Prompt user to select the main file
set /p choice=Enter the number of the main file:
set "MAIN_FILE=!ITEM_LIST[%choice%]!"
:: Confirm the selection
if not defined MAIN_FILE (
echo Invalid selection.
pause
exit /b
)
:: Reset indices and gather directories and files
set /a index=0
for /f "delims=" %%f in ('dir /b /a-d ^| findstr /v /i /c:"%MAIN_FILE%" /c:"%~nx0" ^| findstr /v /i /c:".bat"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
set "ITEM_SELECTED[!index!]=1"
)
for /f "delims=" %%d in ('dir /b /ad ^| findstr /v /i /c:"%~nx0"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%d"
set "ITEM_SELECTED[!index!]=1"
)
:MENU
cls
echo Select items to include (toggle by entering number, type "confirm" to proceed, "ea" to enable all, "da" to disable all):
echo.
:: Display all items and their selection status
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
:: Get user input
set "choice="
set /p choice=Enter your choice:
:: Handle user input to toggle selection, enable all, disable all, or proceed
if /i "!choice!"=="confirm" (
goto GENERATE_CMD
) else if /i "!choice!"=="ea" (
echo Enabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=1"
)
goto MENU
) else if /i "!choice!"=="da" (
echo Disabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=0"
)
goto MENU
) else if "!choice!" neq "" (
set /a toggleIndex=!choice!
:: Check if the input index is within range
if !toggleIndex! geq 1 if !toggleIndex! leq !index! (
:: Toggle the state of the selected item
if "!ITEM_SELECTED[%toggleIndex%]!"=="1" (
set "ITEM_SELECTED[%toggleIndex%]=0"
) else (
set "ITEM_SELECTED[%toggleIndex%]=1"
)
) else (
echo Invalid choice. Please enter a valid number.
)
goto MENU
)
:GENERATE_CMD
:: Build the command for PyInstaller
set "ADD_DATA="
for /L %%i in (1,1,!index!) do (
if "!ITEM_SELECTED[%%i]!"=="1" (
set "ADD_DATA=!ADD_DATA! --add-data ""!ITEM_LIST[%%i]!;!ITEM_LIST[%%i]!"""
)
)
set CMD=python -m PyInstaller --onefile !ADD_DATA! "%MAIN_FILE%"
echo Generated command: !CMD!
:: Execute the command
%CMD%
:: Check if execution was successful
if %ERRORLEVEL% neq 0 (
echo PyInstaller failed.
pause
exit /b
)
:: Move the generated .exe file to the current directory
set exeFile=%MAIN_FILE:.py=.exe%
set exeFile=%exeFile:.pyw=.exe%
if exist "dist\%exeFile%" (
move "dist\%exeFile%" .\
echo Moved %exeFile%
) else (
echo Could not find %exeFile%.
pause
exit /b 1
)
:: Delete the dist folder
if exist dist (
rd /s /q dist
echo Dist folder cleaned up.
)
echo Build and cleanup completed.
pause
@echo off
setlocal enabledelayedexpansion
:: ANSI escape codes setup for colored output
set ESC=
set GREEN=[32m
set RED=[31m
set RESET=[0m
:: Initialize variables
set "MAIN_FILE="
set "ITEM_LIST="
set "ITEM_SELECTED="
set /a index=0
:: Display .py and .pyw files for the user to select as the main file
echo Select a main Python script to package:
for %%f in (*.py *.pyw) do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
echo !index!: %%f
)
:: Prompt user to select the main file
set /p choice=Enter the number of the main file:
set "MAIN_FILE=!ITEM_LIST[%choice%]!"
:: Confirm the selection
if not defined MAIN_FILE (
echo Invalid selection.
pause
exit /b
)
:: Reset indices and gather files/directories excluding certain items
set /a index=0
for /f "delims=" %%f in ('dir /b /a-d ^| findstr /v /i /c:"%MAIN_FILE%" /c:"%~nx0" ^| findstr /v /i /e /c:".py" /c:".pyw"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%f"
set "ITEM_SELECTED[!index!]=1"
)
for /f "delims=" %%d in ('dir /b /ad ^| findstr /v /i /c:"build" /c:"dist" /c:"%~nx0"') do (
set /a index+=1
set "ITEM_LIST[!index!]=%%d"
set "ITEM_SELECTED[!index!]=1"
)
:MENU
cls
echo Select items to include (toggle by entering a number, type "confirm" to proceed, "ea" to enable all, "da" to disable all):
echo.
:: Display all items and their selection status
for /L %%i in (1,1,!index!) do (
set "ITEM=!ITEM_LIST[%%i]!"
set "STATUS=!ITEM_SELECTED[%%i]!"
if "!STATUS!"=="1" (
echo %ESC%%GREEN%[Included]%ESC%%RESET% %%i: !ITEM!
) else (
echo %ESC%%RED%[Excluded]%ESC%%RESET% %%i: !ITEM!
)
)
:: Get user input
set "choice="
set /p choice=Enter your choice:
:: Handle user input to toggle the selection, enable all, disable all, or confirm
if /i "!choice!"=="confirm" (
goto GENERATE_CMD
) else if /i "!choice!"=="ea" (
echo Enabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=1"
)
goto MENU
) else if /i "!choice!"=="da" (
echo Disabling all items...
for /L %%i in (1,1,!index!) do (
set "ITEM_SELECTED[%%i]=0"
)
goto MENU
) else if "!choice!" neq "" (
set /a toggleIndex=!choice!
if !toggleIndex! geq 1 if !toggleIndex! leq !index! (
if "!ITEM_SELECTED[%toggleIndex%]!"=="1" (
set "ITEM_SELECTED[%toggleIndex%]=0"
) else (
set "ITEM_SELECTED[%toggleIndex%]=1"
)
) else (
echo Invalid choice. Please enter a valid number.
)
goto MENU
)
:GENERATE_CMD
:: Build the command for PyInstaller
set "ADD_DATA="
for /L %%i in (1,1,!index!) do (
if "!ITEM_SELECTED[%%i]!"=="1" (
set "ADD_DATA=!ADD_DATA! --add-data ""!ITEM_LIST[%%i]!;!ITEM_LIST[%%i]!"""
)
)
set CMD=python -m PyInstaller --onefile !ADD_DATA! "%MAIN_FILE%"
echo Generated command: !CMD!
:: Execute the command
%CMD%
:: Check if execution was successful
if %ERRORLEVEL% neq 0 (
echo PyInstaller failed.
pause
exit /b
)
:: Move the generated .exe file to the current directory
set exeFile=%MAIN_FILE:.py=.exe%
set exeFile=%exeFile:.pyw=.exe%
if exist "dist\%exeFile%" (
move "dist\%exeFile%" .\
echo Moved %exeFile%
) else (
echo Could not find %exeFile%.
pause
exit /b 1
)
:: Delete the dist and build folders
if exist dist (
rd /s /q dist
echo Dist folder cleaned up.
)
if exist build (
rd /s /q build
echo Build folder cleaned up.
)
echo Build and cleanup completed.
pause
功能说明
自动排除特定文件夹和文件:
排除了当前批处理脚本文件(%~nx0)。
排除了 build 和 dist 文件夹。
排除了其余的 .py 和 .pyw 文件,除了主文件(用户选择的 .py 文件)。
关键代码如下:
batch
for /f "delims=" %%f in ('dir /b /a-d ^| findstr /v /i /c:"%MAIN_FILE%" /c:"%~nx0" ^| findstr /v /i /e /c:".py" /c:".pyw"') do (...)
for /f "delims=" %%d in ('dir /b /ad ^| findstr /v /i /c:"build" /c:"dist" /c:"%~nx0"') do (...)