此批處理代碼可能有助於獲取數字排序到文件名列表文件中的文件名。由於有一些限制,請仔細閱讀註釋行。註釋行是以命令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 operators爲2^>nul
的解釋是2>nul
與重定向操作>
與^
逃到得到重定向適用於執行命令DIR和SET。如果沒有與當前目錄樹中的文件名模式* .txt相匹配的文件,則此重定向將取消DIR和SET輸出的錯誤消息。
你得到的結果是*正常*。這是你得到的文件的正確順序。如果您希望它們按嚴格的數字順序排序,則需要更改文件名以使數字具有相同的數字位數(file01.txt,file02.txt,file03.txt)。 –
''powershell「gci | sort @ {Expression = {[int] [RegEx] :: Match($ _,'\ d +')。Value}} |%{$ _。Name}」'加倍批處理腳本中的'%'符號。 – rojo
'dir/b | powershell「$ input | sort @ {Expression = {[int] [RegEx] :: Match($ _,'\ d +')。Value}}」'也可以。 – rojo