0

I have multiple tiff images ordered from 1 to 246. I converted them to a PDF file using imagemagick with convert *.tif out.pdf. However the order of the pages inside the pdf file differs; (for example, page 2 is image 10 and so on). When I run dir or ls I get un-ordered files like the following:

21/06/2018  16:15    <DIR>          .
21/06/2018  16:15    <DIR>          ..
21/06/2018  15:51             2.056 1.tif
21/06/2018  15:51            98.710 10.tif
21/06/2018  15:51            96.492 100.tif
21/06/2018  15:51            44.064 101.tif
21/06/2018  15:51            70.960 102.tif
21/06/2018  15:51            85.254 103.tif
21/06/2018  15:51            92.750 104.tif
21/06/2018  15:51            87.672 105.tif
21/06/2018  15:51            90.284 106.tif
21/06/2018  15:51            79.532 107.tif
21/06/2018  15:51            85.956 108.tif
21/06/2018  15:51            22.452 109.tif
21/06/2018  15:51            91.796 11.tif
(...)

What i want:

21/06/2018  16:15    <DIR>          .
21/06/2018  16:15    <DIR>          ..
21/06/2018  15:51             2.056 1.tif
21/06/2018  15:51            98.710 2.tif
21/06/2018  15:51            96.492 3.tif
21/06/2018  15:51            44.064 4.tif
21/06/2018  15:51            70.960 5.tif
21/06/2018  15:51            85.254 6.tif
21/06/2018  15:51            92.750 7.tif
21/06/2018  15:51            87.672 8.tif
21/06/2018  15:51            90.284 9.tif
21/06/2018  15:51            98.710 10.tif
21/06/2018  15:51            91.796 11.tif
(...)

How can I sort the files in numerical order?

  • 1
    They are in alphabetical order. You want them in numerical order. – TRiG Jun 21 '18 at 13:47
  • @TRiG Yes, how can i do that ? – ahmelq Jun 21 '18 at 13:48
  • Haven't a notion, as I don't use Windows. But you could edit your question to ask the correct question. – TRiG Jun 21 '18 at 13:55
  • You need to rename files in 3 digit name. For example 1.tif would be 001.tif. In that case files will automatically be in order. – Sandeep Jun 21 '18 at 14:03
  • There is one answer, but it uses freeware: TCC/LE is an enhanced replacement for cmd. There are many enhancements, one of which is that the dir defaults to sorting in the order you want. I use it whenever I work on Windows, and it implements many of the features of a Linux shell, but in keeping with cmd syntax. – AFH Jun 21 '18 at 14:42
3

Use following batch file to rename files to use 3 digit name. Then you can use default sorting order of windows to make your PDF file correct.

@echo off
setlocal enableextensions enabledelayedexpansion
for %%f in (*.tif) do (
    set FileName=%%~nf
    set FileName=00!FileName!
    set FileName=!FileName:~-3!
    set FileName=!FileName!%%~xf
    ren "%%f" "!FileName!"
)

On executing above batch file, file name would be changed to 001.tif, 002.tif, 020.tif... etc. imagemagick will use default sort order and will generate correct PDF.

3

Do you care for some powershell? Like, eg.:

$D = get-childitem
$B = foreach ($F in $D) {
  "{0,30}" -f  $F.Name
}
$B | sort

producing:

PS C:\temp\test> c:\temp\numdir.ps1
                     1.txt
                     2.txt
                    10.txt
                   100.txt

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.