Batch file 批处理文件Scipt在一个go in if语句中执行所有指令

Batch file 批处理文件Scipt在一个go in if语句中执行所有指令,batch-file,Batch File,我是批处理脚本的新手,正在尝试创建一个脚本,它将帮助我在一次运行中设置一个长的手动过程。我在批处理脚本中使用了if-else语句,但所有指令都是一次性执行的,我只给出了1作为输入。代码如下所述 call echo 1. Clone a branch from bitbucket call echo 2. Specify the Absolute path of project set /P choice=" Enter your Choice : " if %choice%==1 (

我是批处理脚本的新手,正在尝试创建一个脚本,它将帮助我在一次运行中设置一个长的手动过程。我在批处理脚本中使用了if-else语句,但所有指令都是一次性执行的,我只给出了1作为输入。代码如下所述

call echo 1. Clone a branch from bitbucket
call echo 2. Specify the Absolute path of project
set /P choice=" Enter your Choice : "
if %choice%==1 ( 
    call set /P fo="enter folder name: " 
    call cd %USERPROFILE%\Desktop\ProjectFolder\%fo%
    echo Clone will be created in the following address
    call cd
    call set /P gitUserName="Enter bitbucket UserName : " 
    call set /P branch=" Enter Branch Name : "
    call echo Git User : %gitUserName%
    call echo Branch Name : %branch%
    call git clone https://%gitUserName%@bitbucket.org/MyProject/Project-demo.git -b %branch%
    call cd Project-demo
) else (
    call set /P pathname = "Enter Absolute path of Project-demo : "
    cd %pathname%
    )

我在Windows10上运行它。尝试禁用@echo off进行调试,发现所有指令都在执行,最后{call set/P fo=“enter folder name:”}

您遇到的问题与延迟扩展有关。你父母内部的一切都是一次处理的,所以你不可能真正做你想做的事情而没有更多的复杂性。这与我的工作有关

你有两个选择。。您可以:

启用EnableDelayedExpansion标记并在块中使用“!”而不是“%”

正确使用“呼叫”。。与您当前的使用不同。Call用于调用批处理文件中的标签。。有点像函数。如果在标签末尾使用
goto:EOF
。。它将像真正编程语言中的函数一样返回给调用方

顺便说一句call也可以用来调用另一个批处理文件,但我们现在不讨论这个问题。一次一点

我将向您展示call方法(我更喜欢,其他人也不喜欢)。在添加EnableDelayedExpansion关键字之前,call方法是执行此操作的唯一方法

:)

`


`

当您删除
@ECHO OFF
时,它将显示整个if语句,而不仅仅是您正在运行的部分。这只是批处理脚本的一个限制

此外,您正在尝试在if语句中使用也在if语句中设置的变量。由于解析和替换的方式,您需要使用延迟扩展(使用!)或创建一个
子例程

我所做的其他一些更改包括:

  • 检查目标目录是否存在。如果没有,那就做吧
  • 显式检查else case是否为2,并为除1或2之外的任何内容添加错误案例
  • 将路径用引号括起来
  • 删除了不必要的
    call用法
  • 添加了git克隆失败的错误检查
  • 以下说明了延迟扩展的一种方法:

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    
        echo 1. Clone a branch from bitbucket
        echo 2. Specify the Absolute path of project
    
        set /P choice=" Enter your Choice : "
        if %choice%==1 (
            set /P fo="enter folder name: "
    
            SET "DEST=!USERPROFILE!\Desktop\ProjectFolder\!fo!"
            IF NOT EXIST "!DEST!" mkdir "!DEST!"
    
            cd "!DEST!"
            echo Clone will be created in the following address
            echo !DEST!
    
            set /P gitUserName="Enter bitbucket UserName : " 
            set /P branch=" Enter Branch Name : "
    
            echo Git User    : !gitUserName!
            echo Branch Name : !branch!
            git clone https://!gitUserName!@bitbucket.org/MyProject/Project-demo.git -b !branch!
            if errorlevel 1    EXIT /B 1
    
            cd Project-demo
    
        ) else if %choice%==2 (
            set /P pathname = "Enter Absolute path of Project-demo : "
            cd !pathname!
    
        ) else (
            ECHO:
            ECHO ERROR: Unexpected choice '%choice%'
            ECHO:
        )
    
    (ENDLOCAL
     EXIT /B 0)
    

    为什么要对这些行使用
    call
    ?你根本不需要它。从你的代码中删除
    call
    的每次使用<代码>调用用于从正在运行的批处理文件中调用另一批处理文件。在命令提示符下键入
    call/?
    ——当它说从一个批处理程序调用另一个批处理程序时,这一点非常清楚。注意,一个批处理文件与另一个批处理文件不同。
    set/P pathname=“…”
    将分配一个名为
    pathname
    +空格的变量,因此请删除
    =
    -符号周围的空格。。。
    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    
        echo 1. Clone a branch from bitbucket
        echo 2. Specify the Absolute path of project
    
        set /P choice=" Enter your Choice : "
        if %choice%==1 (
            set /P fo="enter folder name: "
    
            SET "DEST=!USERPROFILE!\Desktop\ProjectFolder\!fo!"
            IF NOT EXIST "!DEST!" mkdir "!DEST!"
    
            cd "!DEST!"
            echo Clone will be created in the following address
            echo !DEST!
    
            set /P gitUserName="Enter bitbucket UserName : " 
            set /P branch=" Enter Branch Name : "
    
            echo Git User    : !gitUserName!
            echo Branch Name : !branch!
            git clone https://!gitUserName!@bitbucket.org/MyProject/Project-demo.git -b !branch!
            if errorlevel 1    EXIT /B 1
    
            cd Project-demo
    
        ) else if %choice%==2 (
            set /P pathname = "Enter Absolute path of Project-demo : "
            cd !pathname!
    
        ) else (
            ECHO:
            ECHO ERROR: Unexpected choice '%choice%'
            ECHO:
        )
    
    (ENDLOCAL
     EXIT /B 0)