简体   繁体   English

批量然后此时意外

[英]Batch Then was unexpected at this time

I have this code... 我有这个代码......

echo ---- Beginning downloads ----

rem for every plugin with var %%s being the number of the plugin
for /l %%s in (1,1,!plugin_counter!) do (
    echo.
    set /a dl=1
    rem If it's a zip file
    if defined zip_plugin_name[%%s] (
        set zip_plugin_name_=!zip_plugin_name[%%s]!
        set plugin_name_=!zip_plugin_name_!.zip
    rem Otherwise, if it isn't a zip file, we can assume it's a jar because, for now, only .zip's and .jar's are downloaded.
    ) else (
        set plugin_name_=!plugin_name[%%s]!.jar
    )
    set plugin_URL_index_=!plugin_URL_index[%%s]!
    set plugin_full_name=!plugin_URL_index_! / !plugin_name_!
    if defined plugin_disabled_reason[%%s] (
        echo Not downloading !plugin_full_name!. Reason:
        echo !plugin_disabled_reason[%%s]!
        echo Not downloading !plugin_full_name!. Reason:>> %LOGFILE_NAME%
        echo !plugin_disabled_reason[%%s]!>> %LOGFILE_NAME%
        set /a dl=0
    )
    if /i !dl!==1 (
        echo Downloading !plugin_full_name! ...
    )
    rem Try downloading 3 times.
    for /l %%t in (1,1,3) do (
        if /i !dl!==1 (
            if defined alt_url[%%s] (
                dl.vbs "!alt_url[%%s]!" "%OUTPUT_DIR%\!plugin_name_!"
            ) else (
                dl.vbs "https://api.bukget.org/3/plugins/bukkit/!plugin_URL_index_!/%type%/download" "%OUTPUT_DIR%\!plugin_name_!"
            )
            if exist "%OUTPUT_DIR%\!plugin_name_!" (
                set /a dl=0
            ) else (
                rem It doesn't technically retry the third time it would say this.
                if not %%t==3 (
                    echo Error downloading !plugin_full_name!; retrying...
                    echo Error downloading !plugin_full_name!; retrying...>> %LOGFILE_NAME%
                )
            )
            rem If it's a zip
            if defined zip_plugin_name[%%s] (
                rem Try unzipping 3 times
                for /l %%k in (1,1,3) do (
                    if not exist %OUTPUT_DIR%\!plugin_name[%%s]! (
                        echo Unzipping...
                        echo !plugin_full_name! was zipped; unzipping...>> %LOGFILE_NAME%

                        md %zip_temp_dir%

                        rem Unzip the plugin to a temporary directory
                        Call :UnZipFile "%cd%\%zip_temp_dir%\" "%cd%\%OUTPUT_DIR%\!zip_plugin_name_!"

                        :UnZipFile <ExtractTo> <newzipfile>
                        >%zip_vbs_name%  echo Set fso = CreateObject("Scripting.FileSystemObject")
                        >>%zip_vbs_name% echo If NOT fso.FolderExists(%1) Then
                        >>%zip_vbs_name% echo fso.CreateFolder(%1)
                        >>%zip_vbs_name% echo End If
                        >>%zip_vbs_name% echo set objShell = CreateObject("Shell.Application")
                        >>%zip_vbs_name% echo set FilesInZip=objShell.NameSpace(%2).items
                        >>%zip_vbs_name% echo objShell.NameSpace(%1).CopyHere(FilesInZip)
                        >>%zip_vbs_name% echo Set fso = Nothing
                        >>%zip_vbs_name% echo Set objShell = Nothing
                        cscript //nologo %zip_vbs_name%
                        del /f /q %zip_vbs_name%

                        move "%zip_temp_dir%\!zip_plugin_name_!" "%OUTPUT_DIR%\"

                        rem Delete the temporary folder
                        rd /s /q %zip_temp_dir%
                    )
                )
                echo Unzipped.
                echo Unzipped successfully.>> %OUTPUT_DIR%
            )
            rem Delete the old plugin zip file
            del /f /q %OUTPUT_DIR%\!plugin_name_!
        )
    )
    if not exist "%OUTPUT_DIR%\!plugin_name_!" (
        if /i !dl!==1 (
            echo Couldn't download !plugin_full_name!.
            echo Couldn't download !plugin_full_name!.>>%LOGFILE_NAME%
        )
    ) else (
        echo Successfully downloaded !plugin_full_name!.>>%LOGFILE_NAME%
        echo Finished; successful download.
    )
    echo.>>%LOGFILE_NAME%
)

...which, frankly, was ripped out of my program and is out of context. 坦率地说,它被从我的计划中删除,并且脱离了背景。

This is beacuse I feel that this is a syntax error and doesn't really need context -- If it does, let me know. 这是因为我觉得这是一个语法错误,并不真正需要上下文 - 如果是,请告诉我。

A couple of notes: 几个笔记:

  • Yes, I have setlocal EnableDelayedExpansion on. 是的,我有setlocal EnableDelayedExpansion
  • The error it's producing is as shown below: 它产生的错误如下所示:

错误

Any help is appreciated. 任何帮助表示赞赏。

Escape all ) closing parentheses if should be used literally in echo command. 逸出所有)的闭括号是否应该在字面上可以使用echo命令。

Proof (from command prompt): 证明(从命令提示符):

==>(echo x)
x

==>(echo x(y))
) was unexpected at this time.

==>(echo x(y^))
x(y)

Another escaping approach (note that cmd /V:ON enables delayed expansion for command line): 另一种转义方法(注意cmd /V:ON启用命令行的延迟扩展):

==>cmd /V:ON
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

==>set "line=x(y)"

==>(echo !line!)
x(y)

Next .bat code snippet shows some useful escaping practice for batch scripting. 接下来.bat代码片段显示了批处理脚本的一些有用的转义实践。 Maybe not exhaustive, maybe not all necessary: 也许并非详尽无遗,也许并非全部必要:

@ECHO %%  - Percentage Sign: are used to mark three of the four variable types
@ECHO ^^  - Caret: general escape character in batch ^(and in CLI as well^)
  @setlocal disabledelayedexpansion
@ECHO ^!  - Exclamation Mark ^(disabledelayedexpansion^) ^!var^!
  @endlocal
  @setlocal enabledelayedexpansion
@ECHO ^^!  - Exclamation Mark  ^(enabledelayedexpansion^) ^^!var^^! ^(double the caret^)
  @endlocal
@ECHO ^&  - Single Ampersand: used as a command separator
@ECHO ^&^& - Double Ampersand: conditional command separator ^(if %%errorlevel%% EQU 0^)
@ECHO ^|^| - Double Pipe: conditional command separator      ^(if %%errorlevel%% GTR 0^)
@ECHO ^(^) - Parentheses: used to make "code blocks" of grouped commands
@ECHO ^|  - Single Pipe: redirects the std. output of one command
@ECHO                   into the std. input of another
@ECHO ^>  - Single Greater Than: redirects output to either a file or file like device
@ECHO ^>^> - Double Greater than: output will be added to the very end of the file
@ECHO ^<  - Less Than: redirect the contents of a file to the std.input of a command
@echo ^"  - Double Quote: surrounding a string in double quotes escapes
@echo                    all of the characters contained within it
@echo ^   - Space character: if a^^ b==a^^ b @echo match ^(use one caret only^) 
@echo ^@  - At Symbol: be less verbose; e.g. @x.bat should be launched 
@echo                                   by ^^@x.bat ^(use one caret only^)
@echo ^~  - Tilde: Parameter Expansion as in Call subroutines, FOR loops etc.
@echo ^:^: - Double Colon: alternative to "rem" for comments outside of code blocks
@echo ^*  - Asterisk: wildcard matches any number or any characters
@echo ^?  - Question Mark: matches any single character
@echo ^.  - Single dot: represents the current directory
@echo ^.^. - Double dot: represents the parent directory of the current directory
@echo ^\  - Backslash: represent the root directory of a drive dir ^\
@echo ^NUL ^(File like device^): ^NUL is like a bottomless pit
@echo ^CON ^(File like device^): ^CON represents the console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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