Search code examples
macoscommand-lineconcatenationcommandbatch-processing

Concat files 1 & 2, 3 & 4, 5 & 6 (and so on) in a dir of 3000+ using command line


Recovered photos off my dad's digital camera card but the individual files themselves are useless. They seem to have been restored at JPEG marker points, so the info about the camera, exif info etc is in the first file (000001.jpg) and the actual photo data in a separate file (000002.jpg)

Having opened them in a HEX editor, and copy/pasted the contents of file 000001.jpg into the very start of file 000002.jpg then saved, file 000002.jpg is now 'correct' and can be opened with Preview/image apps.

I have a directory of 3200+ files I need to repeat this for!

I have used concat and similar commands before so not scared of the terminal, but I can't really get my head around how I'd phrase the command to step and repeat concating 000001 + 000002, 000003 + 000004, 000005 + 000006 and so on.

Any help much appreciated.

Edit: I am on a Mac, but have access to a PC. The answer supplied by Stephan involved moving the files onto a PC and running a batch cmd. It worked, my problem is solved.


Solution

  • Quick and dirty:

    @echo off
    setlocal enabledelayedexpansion
    
    for /l %%a in (1000001,2,1003201) do (
      set "low=%%a"
      set /a "high=%%a+1"
      ECHO debug: low=!low:~1!.jpg, high=!high:~1!.jpg  ; add a command to join them, like the following (UNTESTED!) command:
      ECHO copy /b !low:~1!.jpg + !high:~1!.jpg new_!low!.jpg
    )
    

    You mentioned "more than 3200", so adapt the last number accordingly. Fine for a "one-time" solution; if you plan to do it regularly, there are methods to count the files.

    NOTE: this depends on the first part always having an odd number, and the second part is always "first plus one". A missing odd file should give an error message, a missing even file doesn't give an error, but the resulting file will only have the contents of the odd file.

    Remove the ECHO with the copy command after verifying it's correct.
    The ECHO debug line is just for troubleshooting, you can safely remove it.

    And of course: always work with a copy of your data or have a valid backup.