Search code examples
windowsdoslong-filenames

How to get around a 'by design' issue with Windows dir?


dir /b produces a nice file-only list

dir /x produces a detailed listing (date, time, size, longname, shortname)

However, if you combine the two (i. e. dir /b /x) the /x switch gets ignored. this behavior as per this page is by design.

So if you ask for a simple list containing only of shortnames of files, Redmond (Microsoft) says it is against the rules of heaven to give it to you.

How could one get around this 'by design' issue?

many thanks in advance

p.s. this is to help me achieve something explained in this question. five kind friends have posted answers to whom i am very grateful, but non of the answers helped me get what i want.


Solution

  • Try this:

    for /f "usebackq delims=" %X in (`dir /b`) do @echo %~nsxX
    

    Or, if you want fully-qualified path names:

    for /f "usebackq delims=" %X in (`dir /b`) do @echo %~fsX
    

    For more information, see the for help:

    for /?
    

    Note that if you use these commands in a batch file, you'll need to double up the % signs. For example:

    for /f "usebackq delims=" %%X in (`dir /b`) do @echo %%~nsxX