I am writing a simple Windows batch script to (git) clone repositories. In it, I have an if
statement that does not consistently branch:
1 @echo off
2 REM git clone a repo from M:\repo and the checkout the dev branch
3
4 if [%1] EQU [] (
5 echo Usage:
6 echo clone.bat [name_of_repo] [optional: destination_path]
7 goto EXIT
8 )
9
10 set dest_path="."
11 set repo=%1%
12
13 if NOT [%2] EQU [] (
14 set dest_path=%2%
15 )
16
17 if NOT EXIST M:\repo\%repo% (
18 echo Error: repository %repo% does not exist.
19 goto EXIT
20 )
21
22 if NOT EXIST %dest_path% (
23 echo Info: destination %dest_path% does not exist.
24 set /p ans="Create path (Y/n)? "
25 if "%ans%" == "Y" (
26 goto RUN
27 )
28 echo Path not created. Done.
29 goto EXIT
30 )
31
32 :RUN
33 REM test that parameters are set
34 echo %dest_path%
35 echo %repo%
36
37 :EXIT
(I inserted the line numbers to help the discussion. I hope they don't get in the way)
The script is very simple:
- Lines 4-8 : check that at least one argument is provided to the script
- Lines 10-15: set the name of the repository, and the optional destination path.
- Lines 17-20: exit if the repo doesn't exist
- Lines 22-30: are supposed to check that the destination path exists, and then prompt the user to create it, if it does not. This is where the problem is.
On line 24, set /p ans="Create path (Y/n)? "
, I prompt to create the path and set the user's response to variable ans
.
Then, if the response is "Y", the script would1 create the path and then go to RUN
, otherwise it should exit.
When I run the script repeatedly as-is, I get this:
bcf@AVA-411962-1 E:\
$ clone gpsrx fake_path
Info: destination fake_path does not exist.
Create path (Y/n)? Y
Path not created. Done.
bcf@AVA-411962-1 E:\
$ clone gpsrx fake_path
Info: destination fake_path does not exist.
Create path (Y/n)? Y
fake_path
gpsrx
bcf@AVA-411962-1 E:\
$
I have tried many variations of the if
statement. And, I have played with the setlocal EnableDelayedExpansion
/setlocal DisableDelayedExpansion
nonsense.
Can anyone point out what the problem is?
1I haven't added the mkpath %dest_path%
yet (which would go between lines 25 and 25), since I don't want the script to actually do anything until it is working properly.