File: purge_nonexistent_images.bat

package info (click to toggle)
darktable 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 62,864 kB
  • sloc: ansic: 361,898; cpp: 102,446; xml: 19,813; lisp: 14,539; sh: 3,771; javascript: 3,264; perl: 1,925; python: 1,485; ruby: 975; makefile: 543; asm: 46; sql: 38; awk: 21
file content (124 lines) | stat: -rw-r--r-- 3,458 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
::
:: Usage: purge_nonexistent_images [-p]
::        -p  do the purge, otherwise only display nonexistent images
::

@echo off
where.exe /q sqlite3
if %ERRORLEVEL% EQU 1 (
  echo Error: sqlite3.exe is not installed or is not in PATH
  exit 1
)

tasklist.exe | find.exe /I "darktable.exe"
IF %ERRORLEVEL% EQU 0 (
  echo Error: darktable is running, please exit first
  exit 1
)


set DBFILE=%LOCALAPPDATA%\darktable\library.db
set dryrun=1
set library=
set configdir=

:: Remember the script file name for the usage text
set scriptfilename=%~nx0

:: Remember the command line to show it in the end when not purging
if "%*" == "" (
  set commandline=%~nx0
) else (
  set commandline=%~nx0 %*
)

:: Handle command line arguments
:GETOPTS
if /I "%1" == "-h" goto HELP
if /I "%1" == "--help" goto HELP
if /I "%1" == "-l" set "library=%2"& shift
if /I "%1" == "--library" set library="%2" & shift
if /I "%1" == "-c" set "configdir=%2"& shift
if /I "%1" == "--configdir" set "configdir=%2"& shift
if /I "%1" == "-p" set dryrun=0
if /I "%1" == "--purge" set dryrun=0
shift
if not "%1" == "" goto GETOPTS

:: When user sets configdir, it means we should work with library.db in that directory
if not .%configdir%==. (
  set DBFILE=%configdir%\library.db
)

if not .%library%==. (
  set DBFILE=%library%
)

if not exist %DBFILE% (
  echo Error: library file '%DBFILE%' doesn't exist
  exit 1
) else (
  echo The library we are working with is '%DBFILE%'
  echo:
)

set QUERY="SELECT images.id, film_rolls.folder || '\' || images.filename FROM images JOIN film_rolls ON images.film_id = film_rolls.id"

if %dryrun% EQU 0 (
  echo Removing the following nonexistent files:
) else (
  echo The following nonexistent files will be deleted during a non-dry run:
)

set ids=

setlocal EnableDelayedExpansion
for /f "tokens=1* delims=|" %%a in ('sqlite3.exe %DBFILE% %QUERY%') do (
  if not exist %%b (
    echo.  %%b with ID %%a
    if .!ids!==. (
      set ids=%%a
    ) else (
      set ids=!ids!,%%a
    )
  )
)

if %dryrun% EQU 0 (
  echo This is NOT dry run
  for %%a in (images,meta_data) do (
      echo Removing from %%a...
      sqlite3 %DBFILE% "DELETE FROM %%a WHERE id IN (%ids%)"
  )

  for %%a in (color_labels,history,masks_history,selected_images,tagged_images,history_hash,module_order) do (
      echo Removing from %%a...
      sqlite3 %DBFILE% "DELETE FROM %%a WHERE imgid in (%ids%)"
  )

  rem delete now-empty film rolls
  sqlite3 %DBFILE% "DELETE FROM film_rolls WHERE NOT EXISTS (SELECT 1 FROM images WHERE images.film_id = film_rolls.id)"
  sqlite3 %DBFILE% "VACUUM; ANALYZE"
) else (
  echo:
  echo The following now-empty filmrolls will be removed during a non-dry run:
  sqlite3 %DBFILE% "SELECT folder FROM film_rolls WHERE NOT EXISTS (SELECT 1 FROM images WHERE images.film_id = film_rolls.id)"

  echo:
  echo To really remove nonexistent images from the database, run:
  echo %commandline% --purge
)

exit 0

:HELP
echo Remove nonexistent images from darktable's database
echo Usage:   %scriptfilename% [options]
echo:
echo Options:
echo:   -c^|--configdir ^<path^>    path to the darktable config directory in which the library.db file will be used
echo:                            (default: '%configdir%')
echo:   -l^|--library ^<path^>      path to the library database file
echo:                            (default: '%DBFILE%')
echo:   -p^|--purge               actually purge the nonexistent images instead of just finding them
exit 0