如何正常排序?

时间:2016-09-03 17:38:29

标签: windows sorting batch-file cmd

如何在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,并且想知道是否有办法不返回完整路径,但只有文件名如上所示。谢谢!

1 个答案:

答案 0 :(得分:0)

此批处理代码可能有助于将文件名数字排序到文件名列表文件中。由于存在一些限制,请仔细阅读注释行。注释行是以命令rem开头的行。

@echo off
setlocal EnableExtensions

rem Delete the list file with the sorted file names in current directory
rem if this file exists already from a previous run of this batch file.
set "SortedFileNamesListFile=sorted.txt"
if exist "%SortedFileNamesListFile%" del "%SortedFileNamesListFile%"

rem Get recursive from current directory all *.txt file names with full path
rem not enclosed in double quotes sorted alphabetically and not numeric as
rem wanted and pass them to subroutine AddToFileList enclosed in double quotes.
for /F "delims=" %%I in ('dir *.txt /A-D /B /ON /S 2^>nul') do call :AddToFileList "%%I"

rem The file names are now in an environment variables list. Output this
rem file names list and redirect the file name with path to the list file.
rem The split in environment variable and file name with path works only
rem if the file name does not contain itself an equal sign.
for /F "tokens=1* delims==" %%I in ('set FileList[ 2^>nul') do echo %%J>>"%SortedFileNamesListFile%"

rem Delete all local environment variables and restore previous
rem environment with the initial list of environment variables.
endlocal

rem Exit batch processing to avoid an unwanted fall through to the
rem subroutine AddToFileList.
exit /B


rem The subroutine AddToFileList is for adding each file found into an
rem environment variables array based on the file name. As the variable
rem names contain just file name without path and file number at end of
rem the file name if there is one at all, the array can't hold multiple
rem files with same file name in different directories. In other words
rem each file in complete directory tree must be unique in entire tree.

rem This restriction must be taken into account or the batch code is
rem enhanced to work directory by directory to avoid unwanted removal
rem of files with same file name in different directories.

rem And the array works only for files with up to 5 digits in file number,
rem i.e. for file numbers in range 0 to 99999. That should be enough.

:AddToFileList
rem Get just the file name without path and without file extension.
set "FileName=%~n1"
rem In case of name of file has only 1 point and no characters left
rem this point, the file name is the point and the file extension.
rem Such file names are not common on Windows, but exist often on *nix.
if "%FileName%" == "" set "FileName=%~x1"
set "FileNumber="

:GetFileNumber
for /F "delims=0123456789" %%I in ("%FileName:~-1%") do goto AddFileToArray
set "FileNumber=%FileName:~-1%%FileNumber%"
set "FileName=%FileName:~0,-1%"
if not "%FileName%" == "" goto GetFileNumber

:AddFileToArray
set "FileNumber=00000%FileNumber%"
set "FileNumber=%FileNumber:~-5%"
set "FileList[%FileName%_%FileNumber%]=%~1"

rem Exit the subroutine and continue in FOR loop in main batch code block.
goto :EOF

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /?
  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

另请参阅Microsoft文章Using command redirection operators,了解2^>nul 2>nul的解释,>重定向运算符^ResourceDictionary一起转义以获取重定向用于执行命令 DIR SET 。如果没有与当前目录树中的文件名模式* .txt匹配的文件,则此重定向会抑制 DIR SET 输出的错误消息。