C:\_G\WWW\~ELISANET\INFO\tscmd022.html
<https://www.salminet.fi/info/tscmd022.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 30-Nov-2018 10:52:27

 
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 [You can't use this unless you have the ntcmds.chm file]
  hh ntcmds.chm::/chdir.htm



[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd022.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd022
https://www.salminet.fi/info/tscmd022.html
file:///c:/_g/www/~elisanet/info/tscmd022.html