如何仅在Windows命令行中获取没有路径的文件名?


31
for /r %f in (*) do echo %f

给出输出文件名以及整个路径

\path\to\dir\<filename>

<filename>没有路径,我如何才能得到?我需要使用“文件名”字符串。

另外,一旦获取文件名,是否可以执行以下操作?

for /r %%f in (*) do (
echo "blah blah blah 'filename'" >> blahblah_filename.txt
)

3
为了将来参考,Microsoft提供了联机Windows XP-命令行参考AZ,它仍然大部分适用于更高版本。您接受的答案中的文档位于for命令部分的结尾。
martineau 2012年

@martineau:不幸的是,在线“ Windows XP-命令行参考AZ”页面不再在线(由Microsoft删除),并且该文档的替代方法还不可用(据我所知,在' %〜'参数文档)
PapaAtHome

@PapaAtHome:幸运的是,仍然可以通过web.archive.org的Wayback机器查看命令行参考。
martineau '18年

Answers:


54

使用%~nxf<filename>.<extension>

是的,您可以执行以下操作:

for /r %%f in (*) do (
echo "blah blah blah '%%~nxf'" >> blahblah_%%~nxf.txt
)

for /?

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

好答案。知道如何在完整路径中搜索子字符串吗?例如,我想知道c:\ code \ myapp \ .git \ somefolder是否包含文本“ .git”。
戴尔·巴纳德

1
您可以通过将路径通过管道传递到findor findstr命令中来执行文本或正则表达式搜索,例如echo %%f | find "\.git\" > NULor echo %%f | findstr /R /C:"\\\.git\\" /C:"\\\.git\>" > NUL,然后使用if命令检查错误级别。(findstr regex非常有限,因此这是我对强制执行前导\和[ \字符串的结尾或结尾] 的尝试)。请注意,这if errorlevel可能会导致失败,并且您可能无法if %errorlevel%进入循环,因此您可能需要SetLocal EnableDelayedExpansion先执行然后再执行if !errorlevel!
鲍勃

@DaleBarnard另外,考虑使用PowerShell- 减轻痛苦得多,而且结果可能更一致。
鲍勃

等等等等使我感到困惑,它应该代表什么?文件的完整路径和提取的文件名如何适合此答案?
thebunnyrules

%~nxf正如我在第一句话中所说,@ thebunnyrules 是文件名。在批处理文件中使用它时,您需要将它加倍%,使其成为%%~nxf。在blah blah blah刚刚采取的例子中的问题和更换filename同位%%~nxf替换-的blahs为刚刚填料,并没有特别的意义。
鲍勃”

0

我尚无法发表评论,但我认为应注意以下几点,以免造成混淆。

虽然f可以用作FOR变量名,但最好使用另一个字母,以避免将其与格式字母f(如Bob的回答所述,返回完整路径名)混淆,或者至少使用大写字母F

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.