Author Topic: Crazy Windows batch file: delete jpg from raw+jpg pair  (Read 22536 times)

Offline devenh

  • Sr. Member
  • ****
  • Posts: 435
    • View Profile
Crazy Windows batch file: delete jpg from raw+jpg pair
« on: February 02, 2009, 06:49:56 PM »

If you have a lot of images on your system, you know that managing the files and structure can be challenging and time consuming. 

For example, new tools can make old workflow practices obsolete.  For instance, PM 4.6 has the ability to save (optionally with crop) downsized jpgs directly from a raw file.  To do so previously, one had to have a jpg of the raw file.  As a result you may now have a lot of unnecessary jpg files on your system.  To purge these files, you can use the following batch file which I call delmatchingjpg.bat.

Usual disclaimers: Use at your own risk.  No warranty whatsoever!!

The batch file looks for nef+jpg pairs and if it finds a pair, it will just list or display the jpg filename.  To actually delete the matching jpg files, you must include the command line argument DELETE (not case sensitive).

To run the batch file, open a command window, go to the top directory you want to process, type "delmatchingjpg", and the batch file will process/traverse/recurse all subdirectories below the current directory and display the jpg filenamess which have a corresponding raw file.

To display help, type "delmatchingjpg /?".

The batch file does call CHOICE (a batch command) which was included in the command language, was taken out for a while, and now is back in I believe Windows 2003 and Vista.  In short, if you get an error on the line containing CHOICE, you will have to install it from the Windows Resource Kit or download it from the web (search for choice.exe and install it in your Windows directory).  XP users will have to do this.

To use it with Canon raw files, just change the _raw variable to CRW.

If you decide to try the batch file, I would strongly suggest doing so first against a backup copy of your data.  I have only tested under Windows XP.

Tip: if you just type delmatchingjpg, all the matching jpg files will be displayed (but not deleted), but they will scroll by pretty quickly.  You may want to try this variant:

delmatchingjpg > files.txt

This will send the output to files.txt and you can open this file in notepad to review all the matching files.

To actually delete the matching jpg files, type "delmatchingjpg DELETE" and you'll receive a prompt like:

Delete d:\photos\landscape\canyon_1234.jpg (Yes, No, Cancel, All)?

You can reply Yes, No, Cancel, or All.  Only the first character is necessary.

If you reply All, the the batch files continues silently without displaying the files.

Comments, suggestions, and especially improvements are welcomed.

If you have a useful batch file you'd like to share, I would encourage you to do so.

Deven


@ECHO OFF
SETLOCAL
SET _bytes=0
SET _count=0
SET _raw=NEF
SET _cmd=%1
SET _choice=0
IF NOT DEFINED _cmd SET _cmd=blank
IF %_cmd%==? GOTO :help
IF %_cmd%==/? GOTO :help
FOR /R %%G IN (*.jpg) DO CALL :process %%G
GOTO :total

:process
 IF %_choice%==3 EXIT /B
 SET _findname=%1
 SET _findname=%_findname:~0,-4%.%_raw%
 IF NOT EXIST %_findname% EXIT /B
 IF /I NOT "%_cmd%"=="DELETE" GOTO :report %1
 IF NOT %_choice%==4 CALL :ask %1
 IF %_choice%==2 EXIT /B
 IF %_choice%==3 ECHO Cancelling, please wait ... && EXIT /B
 CALL :sum %1
 DEL %1
 EXIT /B

:ask
 CHOICE /CYNCA /N Delete %1 (Yes, No, Cancel, All)?
 SET _choice=%ERRORLEVEL%
 EXIT /B

:report
 CALL :sum %1
 ECHO Found %1
 EXIT /B

:sum
 SET /A _bytes+=%~z1/1000
 SET /A _count+=1
 EXIT /B

:total
 IF %_bytes%==0 (ECHO No files processed) ELSE (ECHO %_count% matching files, %_bytes%KB)
 GOTO :eof

:help
 ECHO.
 ECHO delmatchingjpg [DELETE]
 ECHO.
 ECHO Batch file deletes all jpg files that have a corresponding nef file in the
 ECHO       same directory.  To use a different raw file type, edit the _raw
 ECHO       variable in this batch file.
 ECHO.
 ECHO DELETE include this command to actually delete jpg files, otherwise just
 ECHO        list the files that have a corresponding nef file.
 ECHO.
 ECHO !! No warranty expressed or implied !!
 ECHO !! Use at your own risk             !!

Offline Brendan

  • Sr. Member
  • ****
  • Posts: 358
    • View Profile
Re: Crazy Windows batch file: delete jpg from raw+jpg pair
« Reply #1 on: February 08, 2009, 06:29:22 AM »
hi!

this should be integrated in PM.
I asked for this already. Its not a good solution to change the raw/jpg display behaviour every time in the preferences.

Brendan
using the latest version of PM on win10

Offline JAS Photo

  • Sr. Member
  • ****
  • Posts: 394
    • View Profile
    • JAS Photo LLC
Re: Crazy Windows batch file: delete jpg from raw+jpg pair
« Reply #2 on: July 18, 2012, 06:29:51 PM »
Was this ever added to PM? - If not I wish you would?
Joe Sorrentino
JAS Photo LLC
http://www.JASPhoto.com

Offline JAS Photo

  • Sr. Member
  • ****
  • Posts: 394
    • View Profile
    • JAS Photo LLC
Re: Crazy Windows batch file: delete jpg from raw+jpg pair
« Reply #3 on: October 27, 2013, 05:28:42 AM »
Hi Devenh - I LOVE this option to remove matching JPG's - I did some a while back and just trid to do some older directories but now I get an error  ERROR: invalid argument/option - '/cynca'- type choice /? for usage.

I'm using Win 7 but I think that is what I used last time.

I still wish PM would add this as a feature!

Thanks
Joe
Joe Sorrentino
JAS Photo LLC
http://www.JASPhoto.com

Offline devenh

  • Sr. Member
  • ****
  • Posts: 435
    • View Profile
Re: Crazy Windows batch file: delete jpg from raw+jpg pair
« Reply #4 on: October 27, 2013, 10:00:38 AM »
For the batch file to work with newer versions of Windows, you'll need to change

CHOICE /CYNCA /N Delete %1 (Yes, No, Cancel, All)?

to

CHOICE /C YNCA /N /M "Delete %1 (Yes, No, Cancel, All)?"

I only tested this under Windows 8, but it should work for 7.

Deven


Here is the revised version:

Code: [Select]
@ECHO OFF
SETLOCAL
SET _bytes=0
SET _count=0
SET _raw=NEF
SET _cmd=%1
SET _choice=0
IF NOT DEFINED _cmd SET _cmd=blank
IF %_cmd%==? GOTO :help
IF %_cmd%==/? GOTO :help
FOR /R %%G IN (*.jpg) DO CALL :process %%G
GOTO :total

:process
 IF %_choice%==3 EXIT /B
 SET _findname=%1
 SET _findname=%_findname:~0,-4%.%_raw%
 IF NOT EXIST %_findname% EXIT /B
 IF /I NOT "%_cmd%"=="DELETE" GOTO :report %1
 IF NOT %_choice%==4 CALL :ask %1
 IF %_choice%==2 EXIT /B
 IF %_choice%==3 ECHO Cancelling, please wait ... && EXIT /B
 CALL :sum %1
 DEL %1
 EXIT /B

:ask
 CHOICE /C YNCA /N /M "Delete %1 (Yes, No, Cancel, All)?"
 SET _choice=%ERRORLEVEL%
 EXIT /B

:report
 CALL :sum %1
 ECHO Found %1
 EXIT /B

:sum
 SET /A _bytes+=%~z1/1000
 SET /A _count+=1
 EXIT /B

:total
 IF %_bytes%==0 (ECHO No files processed) ELSE (ECHO %_count% matching files, %_bytes%KB)
 GOTO :eof

:help
 ECHO.
 ECHO delmatchingjpg [DELETE]
 ECHO.
 ECHO Batch file deletes all jpg files that have a corresponding nef file in the
 ECHO       same directory.  To use a different raw file type, edit the _raw
 ECHO       variable in this batch file.
 ECHO.
 ECHO DELETE include this command to actually delete jpg files, otherwise just
 ECHO        list the files that have a corresponding nef file.
 ECHO.
 ECHO !! No warranty expressed or implied !!
 ECHO !! Use at your own risk             !!