【问题标题】:problems with various CMD commands各种 CMD 命令的问题
【发布时间】:2019-02-12 02:38:41
【问题描述】:

我是 Ninjight,我和写这篇文章的人是同一个人: creating an self-extracting .exe file with iexpress through command line prompt? 这一次,我在尝试批量扩展时遇到了问题, 所以,我做这个只是为了测试,但是......它似乎不起作用我只是得到“错误的语法”。我不擅长解释,所以我会这样结束这篇文章:“有人可以修复我的脚本并告诉我出了什么问题吗?拜托?”

    @echo off
    title ex
    setlocal

    if /i [%~1] == [/?] (
For %%t in (
    "This is the command shell for Omega Kernel."
    "Please credit if used elsewhere."
    "commands:"
    ""
    " /g GOTO"
    "   The g command essentially works as CD in CMD."
    ""
    " /d delete directory"
    "   The d command is none other than rd/rmdir in cmd."
    ""
    " /mk MAKE (Directory)"
    "   mk is just a replacement of mkdir/md."
    ""
    " /mv MOVE"
    "   mv Moves something somewhere. that's all folks."
    ""
    " /c COPY"
    "   cp copies something somewhere, letting you add variables"
    ""
    " /va EXPORT FROM VIRTUAL ARCHIVE"
    "   va exports files from the virtual archive of the server."
    "   how does it work? It downloads the virtual archive, unzips it, copies the file and then deletes the archive."
    ""
    " /win Opens CMD."
    ""
    " /update downloads updated tools."
    ""
    ""
    ""
    "Developed by DaviAwesome, 2012"
    ""
    "2018 -Ninjight_"
) do Echo;%%~t
    exit /b
    )


    if /i [%~1] == [/g] (
    set /p input=Where to go? 
    cd %input%
    exit /b
    )

    ::delete folder
    if /i [%~1] == [/d] (
    set /p rminput=What to remove? 
    rd %rminput%
    exit /b
    )

    ::make folder
    if /i [%~1] == [/mk] (
    set /p mkinput=Where? 
    md %mkinput%
    exit /b
    )

    ::move
    if /i [%~1] == [/mv] (
    set /p mvinput=What to move? 
    set /p mvinput2=Where to?
    move %mvinput% %mvinput2%
    exit /b
    )

    ::copy.
    if /i [%~1] == [/c] (
    set /p copyinput0=Copy with?
    set /p copyinput=What to copy? 
    set /p copyinput2=Where to?
    set /p copyinput3=Variables?
    ::if %copyinput0% == xc (
    ::xcopy %copyinput% %copyinput2% %copyinput3%
    ::)

    ::if %copyinput0% == robo (
    ::  robocopy %copyinput% %copyinput2% %copyinput3%
    ::)

    ::if %copyinput0% == robo (
    ::  copy %copyinput% %copyinput2% %copyinput3%
    ::)
    if %copyinput0% == xc goto xc
    if %copyinput0% == c goto c
    if %copyinput0% == robo goto robo

    exit /b
    )

    :xc
    xcopy %copyinput% %copyinput2% %copyinput3%
    exit /b
    )

    :c
    copy %copyinput% %copyinput2% %copyinput3%
    exit /b
    )

    :robo
    robocopy %copyinput% %copyinput2% %copyinput3%
    exit /b
    )

    ::virtual archive
    if /i [%~1] == [/va] (
    set /p archinput=What to export? 
    set /p archinput2=Where to?
    download http://link.suffix/file.extension file.extension
    move file.extension .\folder\
    exit /b
    )

    ::open cmd
    if /i [%~1] == [/win] (
    cmd
    exit /b
    )

    ::update command replaces the old and outdated UP.bat
    if /i [%~1] == [/update] (
    download http://link.suffix/file.extension file.extension
    move file.extension .\tools\folder
    exit /b
    )

【问题讨论】:

  • 不要在带括号的 cod 块内使用双冒号作为 cmets。
  • 如果删除@echo off,脚本在哪里中断?没有调查它,我猜一个变量没有被设置在某个地方。 (另外,if 语句使用双引号而不是方括号。)
  • 您还在括号内的代码块中定义变量,然后尝试在同一块中使用它们。这需要使用延迟扩展。
  • 因为你注释掉了很多代码,所以你也有不平衡的括号。
  • 另外,您将标签视为函数,因此如果您选择 /va、/win 或 /update,xcopy 无论如何都会运行。

标签: batch-file robocopy xcopy


【解决方案1】:
@echo off
title ex
setlocal

if not "%~1" == "/?" goto :endif
    for %%t in (
        "This is the command shell for Omega Kernel."
        "Please credit if used elsewhere."
        "commands:"
        ""
        " /g GOTO"
        "   The g command essentially works as CD in CMD."
        ""
        " /d delete directory"
        "   The d command is none other than rd/rmdir in cmd."
        ""
        " /mk MAKE (Directory)"
        "   mk is just a replacement of mkdir/md."
        ""
        " /mv MOVE"
        "   mv Moves something somewhere. that's all folks."
        ""
        " /c COPY"
        "   cp copies something somewhere, letting you add variables"
        ""
        " /va EXPORT FROM VIRTUAL ARCHIVE"
        "   va exports files from the virtual archive of the server."
        "   how does it work? It downloads the virtual archive, unzips it, copies the file and then deletes the archive."
        ""
        " /win Opens CMD."
        ""
        " /update downloads updated tools."
        ""
        ""
        ""
        "Developed by DaviAwesome, 2012"
        ""
        "2018 -Ninjight_"
    ) do echo;%%~t
    exit /b
:endif

if /i not "%~1" == "/g" goto :endif
    set /p "input=Where to go? "
    cd /d %input%
    exit /b
:endif

::delete folder
if /i not "%~1" == "/d"  goto :endif
    set /p "rminput=What to remove? "
    rd %rminput%
    exit /b
:endif

::make folder
if /i not "%~1" == "/mk" goto :endif
    set /p "mkinput=Where? "
    md %mkinput%
    exit /b
:endif

::move
if /i not "%~1" == "/mv" goto :endif
    set /p "mvinput=What to move? "
    set /p "mvinput2=Where to? "
    move %mvinput% %mvinput2%
    exit /b
:endif

::copy.
if /i not "%~1" == "/c" goto :endif
    set /p "copyinput0=Copy with? "
    set /p "copyinput=What to copy? "
    set /p "copyinput2=Where to? "
    set /p "copyinput3=Variables? "

    if "%copyinput0%" == "xc" (
        xcopy %copyinput% %copyinput2% %copyinput3%
    )

    if "%copyinput0%" == "robo" (
        robocopy %copyinput% %copyinput2% %copyinput3%
    )

    if "%copyinput0%" == "c" (
        copy %copyinput% %copyinput2% %copyinput3%
    )

    exit /b
:endif

::virtual archive
if /i not "%~1" == "/va" goto :endif
    set /p "archinput=What to export? "
    set /p "archinput2=Where to? "
    download http://link.suffix/file.extension file.extension
    move file.extension .\folder\
    exit /b
:endif

::open cmd
if /i not "%~1" == "/win" goto :endif
    cmd
    exit /b
:endif

::update command replaces the old and outdated UP.bat
if /i not "%~1" == "/update" goto :endif
    download http://link.suffix/file.extension file.extension
    move file.extension .\tools\folder
    exit /b
:endif

机箱[] 更改为""

双引号可能需要的一些字符串和变量。

否定许多if比较以便使用goto :endif 而不是使用括号。 这允许%variables% 扩展而无需 延迟扩展,因为没有括在括号之间。

删除了过时的标签 :xe:c:robo 作为 现在正在使用禁用的代码。

【讨论】:

  • 我明白了……好吧,谢谢!以后我会尽量记住这一点,这样我就不会再犯同样的错误了,谢谢!
  • 您有多个名为 :endif 的标签。总的来说,这是一种非常糟糕的做法,尽管我承认它在这种特殊情况下有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 2020-11-06
  • 1970-01-01
  • 2015-07-03
  • 2011-06-23
相关资源
最近更新 更多