【问题标题】:How to show only filenames without extensions using dir command如何使用 dir 命令仅显示不带扩展名的文件名
【发布时间】:2019-03-02 21:05:15
【问题描述】:

我正在尝试列出不包括扩展名的文件名,

我想要它:

File1
File2
File3

目前的情况:

File1.txt
File2.txt
File3.txt

我尝试过使用

@echo off
dir /A:-D /B
pause

但它不起作用。我在批处理文件和命令提示符下都试过了。

我是否使用了正确的命令?

【问题讨论】:

  • 解决了!谢谢大家的帮助。
  • 如果您自己解决了这个问题没有以下任何答案的帮助,那么请考虑以您自己的方式回答,包括您解决问题的方式。否则,请接受帮助您解决问题的答案。

标签: windows batch-file command-prompt


【解决方案1】:

使用 FOR 和 ECHO 来实现这一点

例如,假设扩展名总是.txt

for %f  in ("*.txt") do @echo %~nf

我们不使用 DIR,而是使用 FOR 命令遍历列表并将每一个发送到 ECHO,并在 %f 中插入“~n”选项,以使扩展名不显示。

另一种选择是

FORFILES /c "cmd /c echo @fname"

但是,我在每个输出文件名周围加上引号,这不是您想要的。

如果在批处理文件中运行,则需要将变量的 % 加倍

for %%f in ("*.txt") do @echo %%~nf

如果您需要处理多个文件扩展名

只要目录不包含任何名称带有扩展名的子目录,您可以将*.txt概括为*.*

for %f in ("*.*") do @echo %~nf

如果您可能有一些没有扩展名的文件

文件有扩展名但之前没有任何内容,例如.gitignore,结果为空的ECHO 命令将输出一个空消息,例如ECHO is on. 为避免这种情况,您可以使用FIND 命令和/V 选项过滤掉包含ECHO is 的行:

for %f in ("*.*") do @echo %~nf | find /v "ECHO is"

当然,如果您的本地语言导致 DOS 输出 ECHO is 以外的内容,则此过滤将不起作用。它会遗漏任何文件名中恰好包含ECHO is 的文件。

要搜索子目录,请在“for”中添加“/R”

for /R %f in ("*.png") do @echo %~nf | find /v "ECHO is"

结论

这太疯狂了,但这是我们为使用 Batch 语言而不是任何其他语言(根本!)而付出的痛苦代价。我就像一个酒鬼,向所有人承诺,我永远不会再写一行 Batch 代码,然后发现自己又回来这样做了,不好意思。

【讨论】:

  • 这并没有达到OP想要的效果:它会跳过隐藏文件和系统文件(大约第一种方式),第二种方式也会找到目录。第三种方法行不通,因为它应该是%%f
  • 最后一个选项在每个输出文件名前面放一个 %,但是当我从 %%~nf 中删除一个 % 时,它可以正常工作(满足我的需要)
  • 谢谢@hshah,我已经修好了!在命令行中,我们可以只键入“for %f”,但在批处理文件中,它必须是“for %%f”。双哔声也是正确的:第一种方式错过了隐藏文件和系统文件,第二种方式不必要地捕获了目录。
  • 效果很好,但不适用于子文件夹
  • 应您的要求添加子文件夹搜索@White 8-)
【解决方案2】:

在 PowerShell 中会容易得多

(Get-ChildItem -File).BaseName

Get-ChildItem | ForEach-Object { $_.BaseName }

Get-ChildItem可以替换为别名lsgcidirForEach-Object可以替换为%

因此,您可以从 cmd 运行其中任何一个来达到目的

powershell -Com "(ls -File).BaseName"
powershell -C (ls^ -File).BaseName
powershell (ls^ -af).BaseName

【讨论】:

  • 以上示例假设您至少使用 Windows 8,或者已专门安装 PowerShell v3+。使用 PowerShell 2,它是您需要使用的 Windows 7 的一部分:Get-ChildItem | Where-Object { -Not $_.PSIsContainer } | ForEach-Object { $_.BaseName }。这意味着您至少需要一个批处理文件,@PowerShell -NoP "Ls|?{!$_.PSIsContainer}|%%{$_.BaseName}"
  • @Compo 感谢您的评论。在这种情况下,如果文件夹的名称中从来没有点并且文件总是这样做,您可以将命令减少到 ls *.* |% { $_.BaseName }
  • 当然可以,但重要的是要注意,在任何 PC 上,绝大多数目录都不是由用户命名/创建的,所以提前知道是否有任何使用点命名的目录不太可能。此外,当您要删除扩展名时,不可能从结果列表中的文件中分辨出目录,毕竟它通常是标识一个与另一个的扩展名
【解决方案3】:

这可以通过dir 命令和for 循环实现:

@echo off

for /F "delims= eol=" %%A IN ('dir /A-D /B') do echo %%~nA

如果您想要不带扩展名的完整路径,请尝试:

@echo off

for /F "delims= eol=" %%A IN ('dir /A-D /B') do echo %%~dpnA

对于 cmd 一行:

for /F "delims= eol=" %A IN ('dir /A-D /B') do echo %~nA

对于没有扩展名的完整路径,请尝试:

for /F "delims= eol=" %A IN ('dir /A-D /B') do echo %~dpnA

这些小程序,会遍历文件夹中除目录以外的所有文件,echo 只循环不带扩展名的文件名/完整路径。

【讨论】:

    【解决方案4】:

    添加到 Eureka 的答案中,vanilla dir 命令无法实现您正在寻找的内容。

    C:\Users\jacob>dir /?
    Displays a list of files and subdirectories in a directory.
    
    DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
      [/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
    
      [drive:][path][filename]
                  Specifies drive, directory, and/or files to list.
    
      /A          Displays files with specified attributes.
      attributes   D  Directories                R  Read-only files
                   H  Hidden files               A  Files ready for archiving
                   S  System files               I  Not content indexed files
                   L  Reparse Points             -  Prefix meaning not
      /B          Uses bare format (no heading information or summary).
      /C          Display the thousand separator in file sizes.  This is the
                  default.  Use /-C to disable display of separator.
      /D          Same as wide but files are list sorted by column.
      /L          Uses lowercase.
      /N          New long list format where filenames are on the far right.
      /O          List by files in sorted order.
      sortorder    N  By name (alphabetic)       S  By size (smallest first)
                   E  By extension (alphabetic)  D  By date/time (oldest first)
                   G  Group directories first    -  Prefix to reverse order
      /P          Pauses after each screenful of information.
      /Q          Display the owner of the file.
      /R          Display alternate data streams of the file.
      /S          Displays files in specified directory and all subdirectories.
      /T          Controls which time field displayed or used for sorting
      timefield   C  Creation
                  A  Last Access
                  W  Last Written
      /W          Uses wide list format.
      /X          This displays the short names generated for non-8dot3 file
                  names.  The format is that of /N with the short name inserted
                  before the long name. If no short name is present, blanks are
                  displayed in its place.
      /4          Displays four-digit years
    
    Switches may be preset in the DIRCMD environment variable.  Override
    preset switches by prefixing any switch with - (hyphen)--for example, /-W.
    

    此外,作为使用("*.txt") 的建议的替代方案,如果您的文件列表包含多个扩展名,您可以排除不同的扩展名使用*.* 获取所有带有@987654325 的文件@ 在名字里。玩弄那个 glob 以获得你想要的东西。

    【讨论】:

      【解决方案5】:

      dir -Name -File

      这是用于 PowerShell 的

      【讨论】:

      • dir: invalid option -- 'e'
      • 不禁止扩展。
      猜你喜欢
      • 2019-12-01
      • 2018-01-11
      • 1970-01-01
      • 2010-11-24
      • 2011-02-20
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多