我是批处理脚本的新手,正在尝试创建一个脚本,这将帮助我随时进行漫长的手动处理。我在批处理脚本中使用了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%
)
im在Windows 10上运行它。尝试出于调试目的禁用@echo off,发现所有指令都在执行,最后{call set / P fo =“ enter folder name:”} <-执行此行。
我希望它创建一个文件夹并在此处git clone我的项目之一。任何帮助表示赞赏。
也要告诉我,无论我在哪里使用它,都必须打电话。
答案 0 :(得分:0)
您遇到的问题与延迟扩展有关。括号内的所有内容都将被立即处理,因此如果没有更多的复杂性,您将无法真正完成您想做的事情。这与 Delayed Expansion 有关。
您有两种选择。您可以选择:
启用 EnableDelayedExpansion 标志并使用“!”而不是您的代码块中的“%”。
OR
正确使用 “通话” 。.与您当前的使用方式不同。调用用于在批处理文件中调用标签。有点像函数。如果您在该标签的末尾使用goto :EOF
,它将像使用真正编程语言的函数一样返回到调用方。
BTW .. 调用 也可以用于调用另一个批处理文件,但是我们现在不再讨论。一次一位。
我将向您展示call方法(我更喜欢而其他人讨厌)。在添加 EnableDelayedExpansion 关键字之前,调用方法是唯一的方法。
:)
`
@echo off
:START
cls
echo 1. Clone a branch from bitbucket
echo 2. Specify the Absolute path of project
set /P choice=" Enter your Choice : "
:: If they didn't pick a valid option, show the question again...
if not "%choice%"=="1" if not "%choice%"=="2" goto :START
:: Otherwise, take them down the right path
if "%choice%"=="1" call :DoBitBucketClone
if "%choice%"=="2" call :DoAbsolutePath
:: goto :EOF here will simply exit the script
goto :EOF
:DoBitBucketClone
set /P fo="enter folder name: "
cd %USERPROFILE%\Desktop\ProjectFolder\%fo%
echo Clone will be created in the following address
cd
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%
cd Project-demo
:: we 'could' just exit here but I am trying to
:: show you how the return works..
goto :EOF
:DoAbsolutePath
set /P pathname="Enter Absolute path of Project-demo : "
echo "Path set to %pathname%"
cd /d %pathname%
:: we 'could' just exit here but I am trying to
:: show you how the return works..
goto :EOF
`
答案 1 :(得分:0)
当您删除@ECHO OFF
时,它会显示 entire if语句,而不仅仅是您正在运行的部分。那只是批处理脚本的限制。
此外,您尝试在if语句中使用也在if语句内设置的变量。由于解析和替换的完成方式,您需要使用延迟扩展(使用!)或创建subroutine
我进行的其他一些更改是:
call
用法以下内容说明了延迟扩展的方法:
@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)