I have some files with names 93112.txt, 93111.txt, 93110.txt etc.
I want to put them into a single combined file such that
the largest number file goes in first, then the next smaller
…and finally the smallest.I need a short DOS command to do it.Any pointers?
Answer
Try this:
dir /b | sort /r > sorted.txt
dir /b
prints only the item names in a directory.|
pipes the output ofdir /b
tosort
.sort /r
preforms a reverse (descending) natural sort of the item names.> sorted.txt
redirects the standard output ofsort
to a text file (sorted.txt
).
Notes
-
Unlike
sort
,dir
outputs item names in lexographical order, which can affect items with numbers in their names (e.g.1, 2, 12, 22
is sorted as1, 12, 2, 22
). -
For your use case, it is likely undesirable to use
dir /b
by itself (withoutsort
). -
dir /b
includes all file and subdirectory names in a directory. -
Any output text file (e.g.
sorted.txt
) will be created beforedir /b
is executed. -
To avoid having e.g.
sorted.txt
included insorted.txt
, run these commands outside the directory you wish to sort and specify a path to the directory indir /b
(i.e. usedir /b "C:\path\to\folder"
in place ofdir /b
). -
You can use
.
as a shorthand reference to the current directory to avoid typing e.g.C:\long\path\to\current\directory
.
I need to append the files in the descending order of names.
You will need a second command to loop through your files and concatenate them after you have produced your list of sorted names (e.g. sorted.txt
). As an example:
FOR /F "tokens=*" %i IN (sorted.txt) DO type "C:\path\to\folder\%i" >> appended_items.txt
For batch files, use %%
rather than %
(which is what you would use at the command line). Note that >>
is used with the type command to append to an existing file (e.g. appended_items.txt
).
Attribution
Source : Link , Question Author : ravi , Answer Author : Anaksunaman