Search code examples
batch-filedos

DOS BAT file equivalent to Unix basename command?


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.)


Solution

  • 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

    If the path contains a space, add in "delims=" like so:

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

    output: bar baz

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