How to show only filenames without extensions using dir command
Asked Answered
R

5

16

I'm trying to list out file names excluding their extension,

How I want it:

File1
File2
File3

How it currently is:

File1.txt
File2.txt
File3.txt

I tried using

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

but it isn't working. I tried it in both a batch file and in command prompt.

Am I using the right command?

Rhettrhetta answered 2/3, 2019 at 21:5 Comment(2)
Solved! Thank you all for your help.Rhettrhetta
If you solved this your own problem without the help of any of the below answers, then consider answering your own way including the way you solved your problem. Else, accept the answer that helped you to solve it.Suntan
K
20

Use FOR and ECHO to achieve this

For example, assuming the extension is always .txt:

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

Instead of using DIR, we are using the FOR command to go through the list and sending each one to ECHO, with the "~n" option inserted into the %f, to cause the extension to be not shown.

An alternative is

FORFILES /c "cmd /c echo @fname"

However with this I get quotation marks around each output filename, which isn't what you want.

If running inside a batch file, you need to double the %'s for variables

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

If you need to handle multiple file extensions

As long the directory doesn't contain any subdirectories whose names have an extension, you can generalise the *.txt to *.*:

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

If you may have some filenames with only an extension

Where the file has an extension but nothing before it, e.g. .gitignore, the resulting empty ECHO command will output an inane message, such as ECHO is on. To avoid this ruining your onward plans, you can filter out lines containing ECHO is, with the FIND command and the /V option:

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

If your local language causes DOS to output something other than ECHO is then this filtering will not work. And it will miss any file that happens to contain ECHO is in the filename.

To search subdirectories too, add '/R' to the 'for'

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

Conclusion

This is all crazy, of course, but this is the agonising price we pay for using Batch language instead of an actual sensible language. I am like an alcoholic, promising to all and sundry that I will never write a line of Batch code again, and then finding myself coming back to do so again, sheepishly.

Kalamazoo answered 2/3, 2019 at 21:12 Comment(5)
This, doesn't achieve what the OP wants: it will skip hidden and system files (about first way) and the second will find also directories. The third way won't work because it should be %%f.Suntan
The last option put a % in front of each output filename, but worked as expected (for my needs) when I removed a % from %%~nfKlansman
Thanks @hshah, I have fixed! On the command line we can just type, "for %f", but in a batch file, it must be "for %%f". double-beep is also correct: the first way misses the hidden and system files, and the second unwantedly captures directories.Kalamazoo
works good but not on subfolderStafani
subfolder searching added at your request @Stafani 8-)Kalamazoo
P
4

It'll be much easier in PowerShell

(Get-ChildItem -File).BaseName

or

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

Get-ChildItem can be replaced with the aliases ls, gci or dir, -File can be changed to -af and ForEach-Object can be replaced with % so the shortest you can go is (ls -af).BaseName

So from cmd you can run either of these to achieve the purpose

powershell -Com "(ls -File).BaseName"
powershell -C (ls^ -File).BaseName
powershell (ls^ -af).BaseName
Piscatelli answered 3/3, 2019 at 11:11 Comment(3)
The examples above assume you're using at least Windows 8, or have specifically installed PowerShell v3+. Using PowerShell 2, which is part of Windows 7 you'd need to use: Get-ChildItem | Where-Object { -Not $_.PSIsContainer } | ForEach-Object { $_.BaseName }. This would mean from a batch file you'd need at least, @PowerShell -NoP "Ls|?{!$_.PSIsContainer}|%%{$_.BaseName}"Ravi
@Ravi thanks for your comment. In that case you can reduce to command to ls *.* |% { $_.BaseName } if folders never have a dot in their names and file always doPiscatelli
Of course you could, but it's important to note, that on any PC, the vast majority of directories aren't named/created by the user, so knowing in advance whether there's any named using a dot is unlikely. Also as you're removing extensions it would be impossible to tell the directories from the files in the resulting list, after all its usually the extension that identifies one from the other.Ravi
G
1

To add to Eureka's answer, the vanilla dir command cannot achieve what you're looking for.

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.

Additionally, as an alternative to the suggestion to use ("*.txt"), if your file list includes multiple extensions you might either exclude different extensions or use *.* to get all files with a . in the name. Play around with that glob to get what you want out of it.

Gascony answered 2/3, 2019 at 22:17 Comment(0)
S
1

This is possible with a dir command and a for loop:

@echo off

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

If you want the full path without the extension, try:

@echo off

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

For cmd one-line:

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

And for the full path without the extension, try:

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

These small programs, loop through all the files in the folder except directories, and echo only the filenames/full paths without the extension.

Suntan answered 3/3, 2019 at 11:5 Comment(0)
J
0
dir -Name -File

This is for PowerShell

Jail answered 13/7, 2021 at 14:43 Comment(3)
dir: invalid option -- 'e'Deguzman
does not suppress the extensions.Dele
did you even try this? More than 2 years after my powershell answer and this still gets it wrongPiscatelli

© 2022 - 2024 — McMap. All rights reserved.