如何对按目录分组的文件列表重新排序?

如何对按目录分组的文件列表重新排序?

在 DOS 批处理文件中,如何粗略地重新排序包含手动维护的源文件列表的文本文件,以便下面小示例摘录中列出的那些文件(来自同一目录)再次分组在一起?我不喜欢 DOS sort.exe 以纯字母顺序重新排序它们的方式,其中 ctype.h 和 assert.h 已被 c++ 子目录中列出的文件分隔开。

我更愿意使用纯 Windows XP/DOS(或 Unix)命令来执行此操作 - 或者如果需要,下载不需要安装的现有可执行实用程序,如 tcl 或 perl 脚本。遗憾的是dir /s /b无法从文件中获取输入并尝试将其用作格式过滤器,不确定 ls 是否可以。

在 DOS sort.exe 造成危害后重新排序:

C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ar.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\argz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\assert.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\auto_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\backward_warning.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\binders.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\boost_shared_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\functional_hash.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\hashtable_policy.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\cr_section_macros.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ctype.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\dirent.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envlock.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envz.h

期望输出:

C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ar.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\argz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\assert.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\cr_section_macros.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ctype.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\dirent.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envlock.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\auto_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\backward_warning.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\binders.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\boost_shared_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\functional_hash.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\hashtable_policy.h

答案1

我不认为这是一个非常优雅的解决方案,但它确实有效:

  1. 下载GnuWin32 CoreUtils 包
  2. 将文件解压到可访问的位置。
  3. 使用提供的‘sort’可执行文件如下:

    sort <input file> -t \ -k 9 -k 8 -k 7 -k 6 -k 5 -k 4 -k 3 -k 2 -k 1
    

假设文件的最大深度为 9(路径中 9 个字段用反斜杠字符分隔)。输出为:

C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ar.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\argz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\assert.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\cr_section_macros.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\ctype.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\dirent.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envlock.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\envz.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\auto_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\backward_warning.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\binders.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\boost_shared_ptr.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\functional_hash.h
C:\NXP\LpcXpresso_3.6\Tools\arm-none-eabi\include\c++\hashtable_policy.h

如果路径比示例中提供的路径长,那么您基本上必须添加更多 -k 开关以确保保持所需的排序。

相关内容