Batch readability without compromising execution with variables

时间:2016-06-20 04:21:55

标签: windows variables batch-file

I'm currently writing a performance assistant in batch, written so everyday people can read, and edit it. My question is, is it possible to optimize batch code without compromising it's readability? Here's a little snippet of what I have:

    for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
    if "%version%" == "10.0" SET WinX10=1
    if "%version%" == "6.3" SET WinX81=1
    if "%version%" == "6.2" SET WinX80=1
    if "%version%" == "6.1" SET WinX7=1
    if "%version%" == "6.0" SET WinXVista=1
    if "%version%" == "5.2" SET WinXXP64=1
    if "%version%" == "5.1" SET WinXXP32=1
    goto OSScanFinder
    rem Scans your OS for version specific files, required for Extreme, Gaming, and Boot
    :OSScanFinder
    if defined WinX10 goto BootCleanWindows10Latest
    if defined WinX81 goto BootCleanWindows81
    if defined WinX80 goto BootCleanWindows80
    if defined WinX7 goto BootCleanWindowsSeven
    if defined WinXVista goto BootCleanVista
    if defined WinXXP64 goto BootCleanXP64
    if defined WinXXP32 goto BootCleanXP32
    @echo Couldn't find your Operating System.
    @echo I don't support Win2000, Windows Server 2003/R2/2008/R2/2012/R2
    @echo Try running as administrator. If that doesn't work, please contact me at the address provided inside of the .bat file.
    pause
    exit

And

rem Chrome
if exist "C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe" SET ChromeYes=Defined
rem uTorrent
if exist "%userprofile%\Downloads\utorrent.exe" SET uTorrentYes=Defined

if defined ChromeYes TASKKILL /im Chrome.exe /f
if defined uTorrentYes TASKKILL /im utorrent.exe /f

I'm not asking for someone to optimize my code, just if anything could be written better, or if there is a better alternative to using "IF DEFINED" and "SET " while maintaining readability.

1 个答案:

答案 0 :(得分:1)

可读性是一个抽象概念,因此也是个人偏好的问题。例如,在我看来 ,在你的代码中,你以不同的方式做了两次同样的问题,所以这是不必要的复杂。你可以这样做:

for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "10.0" goto BootCleanWindows10Latest
if "%version%" == "6.3"  goto BootCleanWindows81
if "%version%" == "6.2"  goto BootCleanWindows80
if "%version%" == "6.1"  goto BootCleanWindowsSeven
if "%version%" == "6.0"  goto BootCleanVista
if "%version%" == "5.2"  goto BootCleanXP64
if "%version%" == "5.1"  goto BootCleanXP32

@echo Couldn't find your Operating System.
@echo I don't support Win2000, Windows Server 2003/R2/2008/R2/2012/R2
@echo Try running as administrator. If that doesn't work, please contact me at the address provided inside of the .bat file.
pause
exit

但是,在我看来更短的代码总是更易于阅读和理解,因此它更易于阅读。我会这样编写这段代码:

for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
call Version-%version%
if errorlevel 1 (
   @echo Couldn't find your Operating System.
   @echo I don't support Win2000, Windows Server 2003/R2/2008/R2/2012/R2
   @echo Try running as administrator. If that doesn't work, please contact me at the address provided inside of the .bat file.
)
pause
exit

...并在每个代码段中添加文档:

:Version-10.0  WinX10  BootCleanWindows10Latest
rem Apropriate code here
exit /B

:Version-6.3  WinX81  BootCleanWindows81
rem Apropriate code here
exit /B

... etc, until

:Version-5.1  WinXXP32  BootCleanXP32
rem Apropriate code here
exit /B