DOS BAT file equivalent to Unix basename command?

Jonathan Leffler picture Jonathan Leffler · Aug 8, 2010 · Viewed 46.8k times · Source

Is there an easy way to get to the basename (file name without extension) of a DOS file name using the DOS BAT command language?

I agree: format c:\ is probably a good start, followed by a bootable Linux CD (assuming these antique machines have a CD reader - not a given). But let's pretend that we only have DOS... (That means: not Windows - not even Windows 3.1, let alone Windows 95, 98, NT, ME, XP, Vista, 7, etc.)

Answer

hobodave picture hobodave · Aug 8, 2010

For command-line

for /F %i in ("c:\foo\bar.txt") do @echo %~ni

For .bat Files

for /F %%i in ("c:\foo\bar.txt") do @echo %%~ni

output: bar

p.s. if path contains space, use @ciantic's version.

(Further Reading: http://www.computerhope.com/forhlp.htm )