我有这段代码......
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%
)
......坦率地说,它被从我的计划中删除,并且脱离了背景。
这是因为我觉得这是一个语法错误而且确实需要上下文 - 如果确实如此,请告诉我。
几点说明:
setlocal EnableDelayedExpansion
。
感谢任何帮助。
答案 0 :(得分:3)
Escape所有)
右括号,如果应在echo
命令中使用。
证明(来自命令提示符):
==>(echo x)
x
==>(echo x(y))
) was unexpected at this time.
==>(echo x(y^))
x(y)
另一种转义方法(注意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)
下一个.bat
代码段显示了批处理脚本的一些有用的转义练习。也许并非详尽无遗,也许并非全部必要:
@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