3

I have a folder like this C:\repos\3333-new-feature. And from that location, in a git bash terminal, I run a bat file:

/c/repo/3333-new-feature
$ myBat.bat

One of the instructions in the bat should use the directory name, like so:

git flow feature start 3333-new-feature

How can I make a variable that is just the current directory's name? For instance git flow feature start %currentDirectory%?

1
  • @PimpJuiceIT User need this string: /c/repo/3333-new-feature, this is possible without using for loop...
    – Io-oI
    Commented May 6, 2020 at 11:49

2 Answers 2

5

You are not clear as to what exactly you are looking for.

If you want the name of the folder where your running batch script resides, then all you need is "%~nx0".

If you want the last folder name in your current directory path, then

for %%F in ("%CD%") do set "folder=%%~nxF"

Note that folder will be undefined if your current directory happens to be the root directory.

Regardless which form you use, the ~nx is a modifier that returns the name and extension of the leaf node of the given path. For more info about modifiers for parameters and FOR variables, enter HELP FOR or HELP CALL from the command line. In both cases the modifiers are explained near the end of the help.

Also note that although %0 will expand to the name of the labeled routine if currently within a CALLed routine, addition of any modifier like %~nx0 will always refer to the batch file. This comes in very handy.

0
0

You can use relative path . with or without /d (directory):

for %f in (.)      // Obs.:  by using "." you don't need "" in (".")

::  or...

for /d %f in (.)   // Obs.:  by using "." you don't need "" in (".")

In command line:

for %F in (.)do set "folder=%~nxF"
for /d %F in (.)do set "folder=%~nxF"

In / file:

for %%F in (.)do set "folder=%%~nxF"

:: Or...
for %%F in (.)do git flow feature start "%%~nxF"

:: -----------------------------------------------

for /d %%F in (.)do set "folder=%%~nxF"

:: Or...
for /d %%F in (.)do git flow feature start "%%~nxF"

Or, you can concatenate / to your path, replace \ to //, also removing : and without using the for loop in one line

In command line or / file

:: concatenate "\" + _current_path + removing ":"  => : => /c\repo\3333-new-feature
set "_path=/%cd::=%" 

:: replace any "\" in C:\repos\3333-new-feature to "/"  => "\" => /c/repo/3333-new-feature
git flow feature start %_path:\=/%   

:: the same in one line
set "_path=/%cd::=%" && cmd/v/c "git flow feature start !_path:\=/!"

:: the same in one line if any folder have space/special character, use dobblequotes in "path"
set "_path=/%cd::=%" && cmd /q /v /s /c "git flow feature start "!_path:\=/!""

:: in command line:
@set "_path=/%cd::=%" && cmd /q /v /s /r "@echo\git flow feature start %_path:\=/%"

You can also try directly using . considering it is the current Directory:

git flow feature start .
cd /d "C:\repos\3333-new-feature" && git flow feature start .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .