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

只愿长相守 提交于 2020-01-17 12:31:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!