How to sort pdf files by their date

I have about 100 pdf files which are in a folder and are each named as a date ie 21.1.22, 24.5.21 etc

I can't seem to sort them into chronological order when sorting by name or date (as the modified dates don't match the date they cover) Can anybody please advise on the best way to get them in the correct order?

Thanks

Comments

  • You could rename them (a tedious task for a hundred files) to the format yymmdd file name.pdf then a sort on name would be in chronological order.

    There are tools that will bulk rename a set of files but I suspect that setting up the script rules for the task would take longer than doing it manually. 
  • k_man
    k_man Posts: 1,636 Forumite
    1,000 Posts Second Anniversary Name Dropper
    Is the format of the filename consistent?
    E.g. are they all

    SomethingDDMMYY.pdf

    Where the Something is the same, and the filename is always the same length?

    If you are on a Windows PC there are a few file rename utilities available, that can do things like rename multiple files by adding the last 2 characters to the start of the filename etc.

    So you could make these
    YYMMDDSomething
  • Olinda99
    Olinda99 Posts: 2,042 Forumite
    1,000 Posts Third Anniversary Name Dropper
    edited 8 January 2023 at 10:54AM
    yes you need to rename them yymmdd or yyyymmdd
  • jgallcash
    jgallcash Posts: 645 Forumite
    Part of the Furniture 100 Posts Combo Breaker
    Thanks all, I'll bulk rename them all first and then try to sort them that way. I've seen a few utilities to bulk rename
  • Username03725
    Username03725 Posts: 523 Forumite
    Fourth Anniversary 500 Posts Name Dropper
    edited 9 January 2023 at 1:39PM
    In case our friend hasn't yet done this or if others might wish to know a simple way of renaming a number of files in a folder, here's a method we used in a previous job where the client occasionally supplied files that didn't fit the agreed naming format. It assumes some knowledge of Excel and is quite verbose to describe, but doing it is easy.

    The aim is to create a .bat file that will rename the PDFs using the usual DOS ren command, with the format ren old.pdf, new.pdf

    To start, copy the folder of files to somewhere safe as a backup, and leave them well alone. This is your fallback. Then create a folder next to where the original files to be renamed are and copy the PDFs into there. That's your working folder, keep the originals in their folder.

    Open a Command window and do a 
    dir *.pdf > files.txt
    in the working folder to create a list of files. Open files.txt in a text editor with column select mode (Notepad++ is good for this) and edit it to create a block of text that comprises just the file names.

    In Excel in A1 type 
    del *.pdf

    In A2 type 
    copy *.pdf ..\yourtargetfolder
    EDIT: this is wrong, you can work it out though. :)

    This removes all your PDF files in your working folder and copies the originals back in. This way you can keep running the eventual .bat file without having to keep resetting the files if the rename didn't give you what you expected.

    Type
    ren 
    into A3 and drag that to row 103 (or however many files there are), then copy the block of filenames from your text editor into B3, filling down to the last ren row. Type comma space  (, ) into C3 and fill that to the last row.

    In D3 enter a formula that substrings the date components in the original filename into the right order. A compound formula will do the job, this:

    =CONCATENATE(MID(B3,13,2),MID(B3,10,2),MID(B3,8,2),”_“,MID(B3,16,4),".pdf") 

    worked for my example filename "Horse 08-01-23-0001.pdf" creating "230108_0001.pdf". Obvs you can change this how you see fit, I haven't made this work for your specific example. Check that the result is what you require, modify as appropriate until it looks good then autofill that formula in D3 down to the end.

    When you're happy copy the whole lot (Ctl-A, Ctl-C) then Paste Special -> Values into sheet2. Then copy that and paste into your text editor and save it as rename.bat into your working folder.

    In the working folder in the command window run the .bat file. It should delete any PDFs, copy the originals in then rename as per the Excel formula. If it doesn't, repeat the above until it does. Sorted. 
  • Olinda99
    Olinda99 Posts: 2,042 Forumite
    1,000 Posts Third Anniversary Name Dropper
    Just to add - the command 

    dir *.pdf > a.txt /b 

    eliminates the need to edit a.txt
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 350.3K Banking & Borrowing
  • 252.9K Reduce Debt & Boost Income
  • 453.2K Spending & Discounts
  • 243.3K Work, Benefits & Business
  • 597.8K Mortgages, Homes & Bills
  • 176.6K Life & Family
  • 256.4K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.1K Discuss & Feedback
  • 37.6K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.