通过 DOS 命令获取当前文件夹名称?

通过 DOS 命令获取当前文件夹名称?

是否可以使用 DOS 命令获取当前文件夹名称(而不是当前目录路径)?如果可以,如何获取?

我最接近的是这个但它没有做到:

for /f "delims=\" %%a in ("%CD%") do set CURR=%%a
echo.DIR: %CURR%

注意:上述尝试是我尝试标记字符串并将最后一个标记设置为 CURR 变量。

答案1

我发现的最短路径:

for %I in (.) do echo %~nxI

或者在 .bat 脚本中:

for %%I in (.) do echo %%~nxI

或者在.bat 中获取变量的值。

for %%I in (.) do set CurrDirName=%%~nxI
echo %CurrDirName%

解释:http://www.robvanderwoude.com/ntfor.php

nx 仅表示文件名和扩展名

答案2

如果您想知道批处理文件的当前位置(并且您的 Windows 不是非常古老的版本),请for /?在“DOS 框”窗口中输入。向下滚动。阅读。

你会发现,你现在可以阅读(从之内批处理文件中的这些变量:

%0      - as the name how this batchfile was called
%~d0    - as the drive letter where this batchfile is located ('\\' in case of share)
%~p0    - as path (without the drive letter) where this batchfile is located
%~n0    - as filename (without suffix) of this batchfile
%~x0    - as filename's suffix (without filename) of this batchfile
%~a0    - as this batchfile's file attributes
%~t0    - as this batchfile's date+time
%~z0    - as this batchfile's filesize
%~dpnx0 - as this batchfile's fully qualified path+filename
[... and then some more ...]

这适用于许多情况。假设批处理文件名为mytest.bat。您可以用不同的方式调用它:

  1. ..\..\to\mytest.bat ...........................(相对路径)
  2. d:\path\to\mytest.bat........................... (完整路径)
  3. \\fileserver\sharename\mytest.bat...(远程共享上的路径)

...并且您将始终在变量中获得正确的值。

答案3

我个人很喜欢汤姆的回答,直到它在目录名中遇到了点问题。给了我一个提示:

for /f "delims=\" %%a in ("%cd%") do echo topmost dir: %%~nxa

答案4

您可以将当前目录放入变量中。一行代码:

set a=%cd%

检查

echo %a%

相关内容