草庐IT

windows - Batch Then此时出乎意料

coder 2024-06-18 原文

我有这个代码...

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
  • 它产生的错误如下所示:

感谢任何帮助。

最佳答案

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

关于windows - Batch Then此时出乎意料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31065750/

有关windows - Batch Then此时出乎意料的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 在 Windows 机器上使用 Ruby 进行开发是否会适得其反? - 2

    这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby​​-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub

  3. Vscode+Cmake配置并运行opencv环境(Windows和Ubuntu大同小异) - 2

    之前在培训新生的时候,windows环境下配置opencv环境一直教的都是网上主流的vsstudio配置属性表,但是这个似乎对新生来说难度略高(虽然个人觉得完全是他们自己的问题),加之暑假之后对cmake实在是爱不释手,且这样配置确实十分简单(其实都不需要配置),故斗胆妄言vscode下配置CV之法。其实极为简单,图比较多所以很长。如果你看此文还配不好,你应该思考一下是不是自己的问题。闲话少说,直接开始。0.CMkae简介有的人到大二了都不知道cmake是什么,我不说是谁。CMake是一个开源免费并且跨平台的构建工具,可以用简单的语句来描述所有平台的编译过程。它能够根据当前所在平台输出对应的m

  4. 深度学习部署:Windows安装pycocotools报错解决方法 - 2

    深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal

  5. ruby - 如何在 Ruby 中执行 Windows CLI 命令? - 2

    我在目录“C:\DocumentsandSettings\test.exe”中有一个文件,但是当我用单引号编写命令时`C:\DocumentsandSettings\test.exe(我无法在此框中显示),用于在Ruby中执行命令,我无法这样做,我收到的错误是找不到文件或目录。我尝试用“//”和“\”替换“\”,但似乎没有任何效果。我也使用过系统、IO.popen和exec命令,但所有的努力都是徒劳的。exec命令还使程序退出,这是我不想发生的。提前致谢。 最佳答案 反引号环境就像双引号,所以反斜杠用于转义。此外,Ruby会将空格解

  6. ruby - 错误 : Failed to build gem native extension on Windows - 2

    我在安装“redcarpet”gem时遇到以下错误。它在我friend的机器上安装没有问题。(我想安装它来运行yard)ruby版本:1.9.3命令输出:D:\Learning\Common_POM_FW\SampleProjects>yard[error]:Missing'redcarpet'gemforMarkdownformatting.Installitwith`geminstallredcarpet`D:\Learning\Common_POM_FW\SampleProjects>geminstallredcarpetTemporarilyenhancingPATHtoinc

  7. ruby - 从 Ruby 连接到适用于 Windows Phone 8 的 Microsoft 推送通知服务 - 2

    我们正在开发一个需要推送通知的WP8应用程序。为了测试它,我们使用CURL命令行运行推送通知POST请求,确保它实际连接,使用客户端SSL证书进行身份验证并发送正确的数据。我们确实知道,当我们收到对设备的推送时,这项工作是有效的。这是我们一直用于测试目的的CURL命令:curl--certclient_cert.pem-v-H"Content-Type:text/xml"-H"X-WindowsPhone-Target:Toast"-H"X-NotificationClass:2"-XPOST-d"MytitleMysubtitle"https://db3.notify.live.ne

  8. ruby - 在 Windows 7 上运行 Jekyll - 2

    我在Windows7上运行Jekyll时遇到问题。当我运行时jekyll出现以下错误C:\temp\jekyll\kouphax.github.com>jekyllConfigurationfromC:/temp/jekyll/kouphax.github.com/_config.ymlBuildingsite:C:/temp/jekyll/kouphax.github.com->C:/temp/jekyll/kouphax.github.com/_siteunit-testingYouaremissingalibraryrequiredforTextile.Pleaserun:$[s

  9. ruby - 尝试在 Windows 8 (x64) : Error installing fast-stemmer-1. 0.2.gem 上安装 Jekyll - 2

    我正在尝试使用本手册让Jekyll在Windows8x64上运行:RunningJekyllonWindows我正在使用来自rubyinstaller.org的以下两个下载:ruby2.0.0-p0(x64)DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe所以我根据手册设置了Ruby和DevKit(希望它是更新的版本)。我尝试运行一点HelloWorld.rb脚本,它工作正常,路径变量已设置。然后我尝试运行geminstalljekyll并得到以下输出:geminstalljekyll-outputonpastebin.com问题似乎出在名为f

  10. ruby - 通过 ruby​​2.0.0 在 Windows 上安装 iconv 时出错 - 2

    在Windows上通过ruby​​2.0.0安装iconv时出错通过ruby​​2.0.0在Windows上安装iconv时出错我正在尝试安装iconv,但是当我运行以下命令时:geminstalliconv我的结果:TemporarilyenhancingPathtoincludeDevKit...Buildingnativeextensions.Thiscouldtakeawhile...Error:Errorinstallingiconv:ERROR:Failedtobuildgemnativeextension.C:/Ruby/Ruby200/bin/ruby.exe-r./s

随机推荐