7

In my bat script, I'm calling another script and passing it a string parameter

cscript log.vbs "triggered from folder <foldername> by Eric"

The string parameter as you can see contains the name of the folder from which the script is being called. What's the right way to pass this dynamically insert this folder name to the script?

Berming
  • 1,302
  • 4
  • 18
  • 34

2 Answers2

22

If you want the directory where you're currently at, you can get that from %cd%. That's your current working directory.

If you're going to be changing your current working directory during the script execution, just save it at the start:

set startdir=%cd%

then you can use %startdir% in your code regardless of any changes later on (which affect %cd%).


If you just want to get the last component of that path (as per your comment), you can use the following as a baseline:

    @setlocal enableextensions enabledelayedexpansion
    @echo off
    set startdir=%cd%
    set temp=%startdir%
    set folder=
:loop
    if not "x%temp:~-1%"=="x\" (
        set folder=!temp:~-1!!folder!
        set temp=!temp:~0,-1!
        goto :loop
    )
    echo.startdir = %startdir%
    echo.folder   = %folder%
    endlocal && set folder=%folder%

This outputs:

    C:\Documents and Settings\Pax> testprog.cmd
    startdir = C:\Documents and Settings\Pax
    folder   = Pax

It works by copying the characters from the end of the full path, one at a time, until it finds the \ separator. It's neither pretty nor efficient, but Windows batch programming rarely is :-)

EDIT

Actually, there is a simple and very efficient method to get the last component name.

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

Not an issue for this situation, but if you are dealing with a variable containing a path that may or may not end with \, then you can guarantee the correct result by appending \.

for %%F in ("%pathVar%\.") do set "folder=%~nxF"
dbenham
  • 127,446
  • 28
  • 251
  • 390
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    @Berming, I've added code which will give you the last component of the path. See the update. – paxdiablo Oct 03 '10 at 08:28
  • Just what I needed, thank you very much. I noticed though that when I `endlocal` I can't access the `folder` variable after that, so I took out that line. I figured since I removed that line, I should also remove the `@setlocal enableextensions enabledelayedexpansion` at the top, but removing that line caused problems, so I kept that one. – Berming Oct 03 '10 at 11:25
  • 2
    @Berming, see the update. If you leave off the `endlocal`, it will affect you later. You can _still_ use `setlocal/endlocal` to make sure the delayed expansion works but modify the `endlocal` to `endlocal && set folder=%folder%` so that the `folder` variable escapes. – paxdiablo Oct 04 '10 at 03:42
  • Learned the hard way that @setlocal enableextensions enabledelayedexpansion is really needed – Jan Jan 06 '17 at 13:59
9

Try %~dp0.

Here: http://weblogs.asp.net/whaggard/archive/2005/01/28/get-directory-path-of-an-executing-batch-file.aspx

Isaac
  • 16,458
  • 5
  • 57
  • 81
  • 1
    This gives the folder where the bat script is stored, which may or may not be the same as from where the script is being called, so it doesn't work in my case, because almost always it's being called from a different location that's different from where the script is actually stored. – Berming Oct 03 '10 at 04:47
  • 2
    Right! Sorry. OK, I'll leave this answer here as a reference - someone might find `%~dp0` useful for certain purposes. – Isaac Apr 01 '13 at 06:21