• tal
    link
    fedilink
    English
    arrow-up
    20
    ·
    edit-2
    18 hours ago

    Assuming that the leading number and period is part of the filename:

    1. If the directory-browsing thing supports natural sorting of numbers, you can use that; this will detect numbers within the filename and sort by them. For example, I use emacs’s dired to browse directories and play movies. It can modify the flags passed to ls, and ls supports natural sorting of numbers in filenames with the -v flag. Doing this in dired means C-u s v RET, and the directory will be displayed sorted numerically.

    2. If the directory-browsing thing doesn’t support that, rename to a “0”-padded format such that a lexicographic order has the same ordering as numeric order:

       $ for f in *; do
           n="$(printf %04d ${f%%.*})"
           mv "$f" "$(echo $f|sed s/^[0-9]*/$n/)"
       done 
      

      That way,

       1. Episode_One.mp4
      

      will become

       0001. Episode_One.mp4
      

      and so forth, and so software that can only do lexicographic orderings is happy.

    If the leading number and period isn’t part of the filename, then we need to parse human-language strings.

    $ npm install words-to-numbers
    
    $ for f in *; do
        w="$(echo $f|sed -r "s/^Episode_([^.]*).mp4/\1/")"
        n=$(echo $w|node -e 'wn=require("words-to-numbers"); const l=require("readline").createInterface({input:process.stdin, output:process.stdout, terminal:false}); rl.on("line", l=> {console.log(wn.wordsToNumbers(l));})')
        nf=$(printf %04d $n)
        mv "$f" "$nf. $f"
    done
    

    That’ll rename to the format mentioned above in option 2:

    Episode_One.mp4
    

    will become

    0001. Episode_One.mp4
    

    And lexicographic sorting will work.

    • hemko@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      23
      ·
      21 hours ago

      This is why I love Lemmy. There’s a meme post and then someone writes a 7 page long technical solution like they were paid contributor at stack overflow.

      Never change <3

      • tal
        link
        fedilink
        English
        arrow-up
        5
        ·
        edit-2
        21 hours ago

        The first case is the most-likely for most people, and the simplest to do. Most directory browsers do support numeric sorting.

        In the second case, I provided a Perl program in another comment in the thread that provides a generic way to do this with one command for nearly all files of this sort.

        The third case, where human-language stuff needs to be parsed, true enough, doesn’t just have a button to push.

        • rumschlumpel@feddit.org
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          2 hours ago

          The first case is the most-likely for most people, and the simplest to do. Most directory browsers do support numeric sorting.

          That may be true for terminal applications, but even on Linux most GUI file browsers don’t support that, at least not out of the box.