我正在尝试列出不包括其扩展名的文件名,
我多么想要:
File1
File2
File3
目前的情况如何:
File1.txt
File2.txt
File3.txt
我试过用
@echo off
dir /A:-D /B
pause
但它不起作用。我在批处理文件和命令提示符中都尝试了它。
我使用正确的命令吗?
例如:
for %f in ("*.txt") do @echo %~nf
我们使用FOR命令通过列表并将每个命令发送到ECHO而不是使用DIR,并在%f中插入“~n”选项,以使扩展名不显示。
另一种选择是
FORFILES /c "cmd /c echo @fname"
但是有了这个,我在每个输出文件名周围都会得到引号,这不是你想要的。
for %f in ("*.txt") do @echo %%~nf
是的,这很疯狂,但这是我们使用Batch语言而不是任何其他语言付出的痛苦代价(在所有!)。我就像一个酗酒者,向所有人承诺,我再也不会写一行批处理代码了,然后发现自己又回来了,羞怯地再次这样做。
这可以使用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
仅循环没有扩展名的文件名/完整路径。
为了增加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")
的建议的替代方法,如果您的文件列表包含多个扩展名,您可以排除不同的扩展名或使用*.*
来获取名称中包含.
的所有文件。玩弄那个水泡来获得你想要的东西。
在PowerShell中它会更容易
(Get-ChildItem -File).BaseName
要么
Get-ChildItem | ForEach-Object { $_.BaseName }
Get-ChildItem
可以替换为别名ls
,gci
或dir
和ForEach-Object
可以用%
替换
所以从cmd你可以运行其中任何一个来达到目的
powershell -Com "(ls -File).BaseName"
powershell -Com (ls^ -File).BaseName