Windows 'dir' 命令,按名称排序 AND <something>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/242913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows 'dir' command, Order By Name AND <something>
提问by ian_scho
We can see in a directory files ordered by Name in Windows Explorer.
我们可以在 Windows 资源管理器中按名称排序的目录中看到文件。
If I try the same thing in the Windows command prompt it orders by name differently - correctly:
如果我在 Windows 命令提示符下尝试相同的操作,它会按名称不同排序 -正确:
dir *.jpg /ON /B
cubierta.jpg
pag00.jpg
pag06.jpg
pag08.jpg
pag09.jpg
pag100.jpg
pag101.jpg
pag102.jpg
pag103.jpg
pag104.jpg
pag105.jpg
pag106.jpg
pag107.jpg
pag108.jpg
pag109.jpg
pag11.jpg, etc, etc, etc, ...
Is there a way to get dir
to order by Name where it reads the numbers as a human would do?
有没有办法dir
按名称排序,它会像人类一样读取数字?
回答by Doug L.
Your best option (if possible) is to add enough leading zeros (two, in this case) to your smaller numbers so that the sort does come out as expected.
您最好的选择(如果可能)是在较小的数字中添加足够的前导零(在本例中为两个),以便按预期进行排序。
回答by R?mulo Ceccon
Windows Explorer uses a special sorting function, StrCmpLogicalW, which is not used by cmd.exe, hence the differences in the output. You could write your own sorting program with it, though.
Windows 资源管理器使用特殊的排序函数StrCmpLogicalW,cmd.exe不使用该函数,因此输出中存在差异。不过,您可以用它编写自己的排序程序。
You might also be interested in Michael Kaplan's entries on thissubject(from Raymond Chen's suggestion box).
回答by ian_scho
He he he. Do you all want to know my solution?
他他他。你们都想知道我的解决方案吗?
I'm using a program called ImageMagick that converts my 200MB PDF files to JPG. At the end of the lengthy process it serializes all of the files in numerical order... So basically the file creation date increases in value slightly as each file is being written to the hard drive. Thus either of the following two batch file commands will write the files in 'correct' order:
我正在使用一个名为 ImageMagick 的程序,它将我的 200MB PDF 文件转换为 JPG。在漫长的过程结束时,它按数字顺序序列化所有文件......所以基本上文件创建日期的值随着每个文件被写入硬盘驱动器而略有增加。因此,以下两个批处理文件命令中的任何一个都将以“正确”的顺序写入文件:
dir *.jpg /ODN /B > files.txt
for /f "tokens=*" %%a in ('dir *.jpg /ODN /B') do (
echo ^<page^>%%a^</page^> >>pages.xml.fragment
)
Thus the dir *.jpg /OD
command orders the directory content by ascending (creation?) date, and we can completely ignore the actual file name.
因此该dir *.jpg /OD
命令按升序(创建?)日期对目录内容进行排序,我们可以完全忽略实际的文件名。
回答by phuclv
Currently there are no built-in command line tools that can sort naturally. If you want please vote for the featureto make Microsoft change their mind
目前没有可以自然排序的内置命令行工具。如果您愿意,请为该功能投票以让 Microsoft 改变主意
This however can be simulated with a regex replacement. On Windows you can use Jscript and VBS to do that. But the easiest way is using powershell. You can call it from cmd.exe like this
然而,这可以用正则表达式替换来模拟。在 Windows 上,您可以使用 Jscript 和 VBS 来做到这一点。但最简单的方法是使用powershell。您可以像这样从 cmd.exe 调用它
powershell -Command "(Get-ChildItem | Sort-Object { [regex]::Replace($_.Name, '\d+', { $args[0].Value.PadLeft(20) }) }).Name"
Of course you'll need to change the number in PadLeft(20)
if your files contains a longer series of digits
当然,PadLeft(20)
如果您的文件包含更长的数字序列,则需要更改数字
How to sort by file name the same way Windows Explorer does?
如何以与 Windows 资源管理器相同的方式按文件名排序?
A native batch solution can be found in Naturally Sort Files in Batchif your files have only a single number at the end
如果您的文件末尾只有一个数字,则可以在“在批处理中自然排序文件”中找到本机批处理解决方案
@echo off
setlocal enabledelayedexpansion
for %%a in (*.txt) do (
set num=00000000000000000000%%a
set num=!num:~-20!
set $!num!=%%a
)
for /f "tokens=1,* delims==" %%a in ('set ##代码##') do echo %%b
回答by phuclv
4NT/TCC/TC from JPSoftuse natural sort order by default; alternately, if you have access to a GNU "sort" program, such as the Cygwin tools, you can do something like "ls -1 | /bin/sort -g" or "dir /b | \cygwin\bin\sort -g"; that will give you the filenames in natural sort order.
JPSoft 的4NT/TCC/TC默认使用自然排序;或者,如果您可以访问 GNU“排序”程序,例如Cygwin 工具,则可以执行诸如“ls -1 | /bin/sort -g”或“dir /b | \cygwin\bin\sort -”之类的操作G”; 这将按自然排序顺序为您提供文件名。
If you are constrained to tools that are native to windows, you can try the Windows Scripting toolsand do something like use a FileSystemObject to get the filenames , and regular expressions to extract the numeric part of the filenames to use as keys in a Dictionary object. I'll leave the actual coding as an exercise to the reader :)
如果您受限于 Windows 原生的工具,您可以尝试使用Windows 脚本工具并执行一些操作,例如使用 FileSystemObject 获取文件名,以及使用正则表达式提取文件名的数字部分以用作 Dictionary 对象中的键. 我将把实际的编码作为练习留给读者:)
回答by Biri
No, there's no way to do this. Windows Explorer uses a different approach to handle this.
不,没有办法做到这一点。Windows 资源管理器使用不同的方法来处理此问题。