views:

246

answers:

3

dir /b produces a nice file-only list

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

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, redmont 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.

A: 

You could write your own directory listing executable. It wouldn't take much to whip up something in C#. Use Directory.GetFiles() to retrieve the directory listing, and pass each one into the "GetShortPathName()" Win32 function.

This page has a decent example of how to call GetShortPathName() from C#: http://csharparticles.blogspot.com/2005/07/long-and-short-file-name-conversion-in.html

Jesse Weigert
+1  A: 

Short of dumping dir /x to a text file and parsing it, I'm not sure what to suggest. Do you have the ability to run whatever it is you're doing in code?

It's a more complicated solution, but writing something using perl or another scripting language; or go whole hog and write some code in C#.

I suspect that you're going to get much the same kind of answers that you got on the previous question...

kdmurray
i need this for a friend. he has no php, perl, python, ... on his system ...
Majid
+3  A: 

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
Spire
both commands fail on seeing a parenthesis. if you have milad(1).mp3, and milad(2).mp3, the command produces two items bith as milad and discard anything following it (even the dot and extension: .mp3). whereas dir /x produces MILAD(~1.MP3 and MILAD(~2.MP3
Majid
The problem was caused by long filenames with spaces in them. I've updated my answer with corrections to the commands to fix this.
Spire
Good idea. But it needs a little change. The first one missing extensions, so add extensions and hide the echo command: for /f "usebackq delims=" %I in (`dir /b`) do @echo %~nxsI
brofield
Good catch on the dropped extensions. The echo was really just for demonstration purposes, but it is nicer if it's hidden. I've updated the original post with both of these changes.
Spire
brilliant! thanks a million!
Majid
could you have a look at my other question too? i think i've done it in a very naive way - to be exact, i've patched code by others with a few lines by myself ;-)
Majid