2016-09-03 15 views
2

如何在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並想知道是否有辦法不返回完整路徑,但只有文件名如上所示。謝謝!

+3

你得到的結果是*正常*。這是你得到的文件的正確順序。如果您希望它們按嚴格的數字順序排序,則需要更改文件名以使數字具有相同的數字位數(file01.txt,file02.txt,file03.txt)。 –

+0

''powershell「gci | sort @ {Expression = {[int] [RegEx] :: Match($ _,'\ d +')。Value}} |%{$ _。Name}」'加倍批處理腳本中的'%'符號。 – rojo

+0

'dir/b | powershell「$ input | sort @ {Expression = {[int] [RegEx] :: Match($ _,'\ d +')。Value}}」'也可以。 – rojo

回答

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=" %%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 operators2^>nul的解釋是2>nul與重定向操作>^逃到得到重定向適用於執行命令DIRSET。如果沒有與當前目錄樹中的文件名模式* .txt相匹配的文件,則此重定向將取消DIRSET輸出的錯誤消息。

+0

這是一整套代碼以查看目錄列表。 AFAICT,海報使用'dir'命令詢問是否這樣做。 –

+0

@KenWhite你可能想添加一個答案「這是不可能與'dir'」。我敢肯定,它會被接受並收到許多讚美詞...(並且順便說一句:如果你忽略了REM語句,那不是很多代碼) – Stephan