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.