Ditya
Ditya

Reputation: 1

How to list file names in a directory sorted numeric in a command prompt window?

I need to dir my files in a folder, but the sorter is not same as displayed in Windows Explorer.

For example if I've files with 112 and 0113 as file names in the folder, dir in console window list them in order 0113 and 112.

How to list file names in a directory sorted numeric in a command prompt window as Windows Explorer does?

Upvotes: 0

Views: 2955

Answers (1)

Mofi
Mofi

Reputation: 49136

This batch code could be helpful to get the file names output sorted numeric. Please read the comment lines carefully because there are some restrictions. The comment lines are the lines starting with command rem.

@echo off
setlocal EnableExtensions

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

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

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
rem into an environment variables array based on the file name.

rem 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

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

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

And see also the Microsoft article about Using command redirection operators for an explanation of 2^>nul which is 2>nul with redirection operator > escaped with ^ to get the redirection applied on execution of command DIR and SET. This redirection suppresses the error messages output by DIR and SET if there is no file matching the file name pattern *.txt in current directory tree.

Upvotes: 1

Related Questions