问题
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.
回答1:
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
回答2:
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...
回答3:
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
来源:https://stackoverflow.com/questions/684811/how-to-get-around-a-by-design-issue-with-windows-dir