如何在 Windows 命令行中进行不同的排序? 我浏览了几个网站,包括这个 http://ss64.com/nt/dir.html ,但我看不到按我想要的方式排序的方法。 目前我正在使用:
dir *.txt /s /o:d /b >> "sorted.txt"
目前是这样排序的:
file1.txt
file10.txt
file11.txt
...
file2.txt
file20.txt
file21.txt
我希望它像这样“正常”排序:
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
另外我正在使用/s 并且想知道是否有一种方法可以不返回完整路径,而只返回文件名,如上所示。谢谢!
最佳答案
Windows CMD 排序为 1>10>2>20>3>4>50>6
你正在寻找的是所谓的“自然排序”,
我正在使用一个非常基本但设计良好的算法,
用 JavaScript 编写,
因为它本身支持 UTF-8 并与 NodeJS 一起使用,
它可以非常轻松地读取、修改和写入文件。
这是自然排序的基本算法:
function natural_compare(a, b){
var ax=[], bx=[];
a.replace(/(\d+)|(\D+)/g, function(_, $1, $2){ ax.push([$1 || Infinity, $2 || ""]); });
b.replace(/(\d+)|(\D+)/g, function(_, $1, $2){ bx.push([$1 || Infinity, $2 || ""]); });
while(ax.length > 0 && bx.length > 0){
var an, bn, nn;
an = ax.shift();
bn = bx.shift();
nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
if(nn) return nn;
}
return ax.length - bx.length;
}
然后将它应用于数组的排序方法,
作为默认排序行为的替代方法。
- >
将其保存到名为“index.js”的文件中:
"use strict"; function natural_compare(a, b){ var ax=[], bx=[]; a.replace(/(\d+)|(\D+)/g, function(_, $1, $2){ ax.push([$1 || Infinity, $2 || ""]); }); b.replace(/(\d+)|(\D+)/g, function(_, $1, $2){ bx.push([$1 || Infinity, $2 || ""]); }); while(ax.length > 0 && bx.length > 0){ var an, bn, nn; an = ax.shift(); bn = bx.shift(); nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]); if(nn) return nn; } return ax.length - bx.length; } const FS = require("fs") ,PATH = require("path") ,resolve = function(path){ path = path.replace(/\"/g,""); path = path.replace(/\\+/g,"/"); path = PATH.resolve(path); path = path.replace(/\\+/g,"/"); path = path.replace(/\/\/+/g,"/"); return path; } ,ARGS = process.argv.filter(function(s){return false === /node\.exe/i.test(s) && false === /index\.js/i.test(s);}).map(function(s){return s.replace(/\"/gm,"");}) ,FILE_IN = resolve(ARGS[0]) ,FILE_IN_PARTS = PATH.parse(FILE_IN) ,FILE_OUT = resolve( FILE_IN_PARTS.dir + "/" + FILE_IN_PARTS.name + "_sorted_uniqued" + FILE_IN_PARTS.ext ) ; var tmp = new Object(null) ,content = FS.readFileSync(FILE_IN, {encoding:"utf8"}) //raw text-content .replace(/[\r\n]+/gm, "\n").replace(/\n+/gm, "\n") //unify newline character, single newline the most. .split("\n").filter(function(s){return s.length > 1;}) //no empty lines. whitespace is considered a perfectly valid content. if you do wish to remove all-whitespace lines and empty lines pre-include this line before this one: .replace(/^\s*/g, "").replace(/(\s*$|^\s*)/gm, "") ; //-------------------------------------------------unique content.forEach(function(s){ tmp[1] = 1; }); content = Object.keys(tmp); tmp = undefined; //--------------------------------------------------------- content = content.sort(natural_compare) .join("\r\n") ; FS.writeFileSync(FILE_OUT, content, {flag:"w", encoding:"utf8"}); //overwrite
- > 从 https://nodejs.org/download/nightly/v17.0.0-nightly2021100628f711b552/win-x86/node.exe 下载单个服务的 `node.exe` .
- >
将其添加到名为“sort.cmd”的文件中:
@echo off chcp 65001 1>nul 2>nul call "%~sdp0node.exe" "%~sdp0index.js" %* exit /b %ErrorLevel%
- > 将所有 3 个文件放在您喜欢的任何文件夹中, 当您将任何文件拖放到 `sort.cmd` 上时, 您将获得一个新文件,其文本内容已排序(且唯一)。
结果文件的行将使用 Windows EOL (CR+LF) 连接。
几年来它对我来说非常有效,
跳过所有其他批处理文件特定的怪癖(..CMD 处理原始 Unicode.. Brrrr...😱)
它也是一个独立于操作系统的解决方案,
因为你真的不需要 cmd 文件来运行 NodeJS 脚本,
只需将文件路径指定为 NodeJS 的参数,
并下载您的操作系统版本的 NodeJS
(如果您愿意,还可以更改 EOL 字符)。
您可以在以下位置寻找其他解决方案:
https://github.com/search?q=natural+sort+batch+file&type=Repositories
但是没有很多客户就绪的解决方案。
关于windows - 如何正常排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39309634/