<http://www.netikka.net/tsneti/info/tscmd022.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Sat 18-Sep-2010 16:25:52

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



22} How do I capture the current folder name into a variable?

In XP this is a no-brainer because %CD% returns the current directory string. Thus, for example
  @echo off & setlocal enableextensions
  echo %cd%
  echo %cd:~0,2%
  for /f "tokens=* delims=" %%f in ('echo %cd%') do (
    echo %%~df %%~pf %%~nf)
  endlocal & goto :EOF

The output might be e.g.   C:\_D\TEST>cmdfaq
  C:\_D\TEST
  C:
  C: \_D\ TEST

As a demonstration of the current folder usage consider the following little snippet
  @echo off & setlocal enableextensions
  set currdir_=%cd%
  pushd %currdir_%
  cd
  c:
  cd \Documents and Settings
  cd
  popd
  cd
  endlocal & goto :EOF

The ouput could be e.g.   C:\_D\TEST>cmdfaq
  C:\_D\TEST
  C:\Documents and Settings
  C:\_D\TEST

or

  @echo off & setlocal enableextensions
  set currdir_=%cd%
  pushd %currdir_%
  cd
  cd /d C:\Documents and Settings
  cd
  popd
  cd
  endlocal & goto :EOF

Note that the quote (") delimiters are not needed in here and have hence been omitted. They could have been used, of course.

Also note that the following two expressions mean diffirent things
  @echo off & setlocal enableextensions
  echo %cd%
  echo %~dp0
  endlocal & goto :EOF

The output might be e.g.
  C:\_M>c:\_d\test\cmdfaq
  C:\_M
  c:\_D\TEST\
The former (C:\_M) is the current folder, the latter (c:\_D\TEST\) the folder of the script called.

References/Comments:
  hh ntcmds.chm::/ntcmds_shelloverview.htm
  hh ntcmds.chm::/chdir.htm