-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmdbcheck.sh
More file actions
319 lines (285 loc) · 9.73 KB
/
mdbcheck.sh
File metadata and controls
319 lines (285 loc) · 9.73 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash
# mdbcheck.sh - Scan directory for .mdb and .accdb files and report JET version and migration status
# Usage: ./mdbcheck.sh [options] [DIR]
# Options:
# -r, --recursive Search recursively (scan subdirectories)
# -c, --csv Write CSV (Path,JETVersion,Status) and print CSV rows to screen
# -C, --csvonly Write CSV only (no table output); show progress dots on the screen
# -h, --help Show this help message
# Default: non-recursive (only top-level files in DIR)
usage() {
cat <<EOF
Usage: $0 [options] [DIR]
Options:
-r, --recursive Search recursively (scan subdirectories)
-c, --csv Write CSV (Path,JETVersion,Status) and print CSV rows to screen
-C, --csvonly Write CSV only (no table output); show progress dots on the screen
-h, --help Show this help message
Default: non-recursive (only top-level files in DIR)
If DIR is not specified, the current directory is used.
EOF
}
# Default: non-recursive
RECURSIVE=0
# CSV mode: 0 = table, 1 = csv to file+screen, 2 = csvonly
CSV_MODE=0
CSV_FILE="mdbcheck.csv"
# Parse options
while [[ $# -gt 0 ]]; do
case "$1" in
-r|--recursive)
RECURSIVE=1
shift
;;
-c|--csv)
CSV_MODE=1
# optional filename after -c; if the next token ends with .csv, treat it as filename, otherwise treat it as DIR
if [[ -n "${2:-}" && "${2}" == *.[cC][sS][vV] ]]; then
CSV_FILE="$2"
shift 2
elif [[ -n "${2:-}" && "${2:0:1}" != "-" ]]; then
DIR="$2"
shift 2
break
else
shift
fi
;;
--csv=*)
CSV_MODE=1
val="${1#--csv=}"
if [[ "${val}" == *.[cC][sS][vV] ]]; then
CSV_FILE="$val"
shift
else
DIR="$val"
shift
break
fi
;;
-C|--csvonly)
CSV_MODE=2
# optional filename after -C; if the next token ends with .csv, treat it as filename, otherwise treat it as DIR
if [[ -n "${2:-}" && "${2}" == *.[cC][sS][vV] ]]; then
CSV_FILE="$2"
shift 2
elif [[ -n "${2:-}" && "${2:0:1}" != "-" ]]; then
DIR="$2"
shift 2
break
else
shift
fi
;;
--csvonly=*)
CSV_MODE=2
val="${1#--csvonly=}"
if [[ "${val}" == *.[cC][sS][vV] ]]; then
CSV_FILE="$val"
shift
else
DIR="$val"
shift
break
fi
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-* )
echo "Unknown option: $1"
usage
exit 1
;;
*)
DIR="$1"
shift
break
;;
esac
done
# Directory to scan (default: current directory)
DIR="${DIR:-.}"
# Normalize DIR (remove trailing slash except when DIR == "/")
if [[ "$DIR" != "/" ]]; then
DIR="${DIR%/}"
fi
# Check if mdb-ver (mdbtools) is installed
if ! command -v mdb-ver >/dev/null 2>&1; then
echo "Error: mdbtools (mdb-ver) is not installed. Please install mdbtools before running this script."
exit 1
fi
# Helper: abbreviate directory to keep first two and last two components when too long
abbrev_dir() {
local path="$1"
local maxlen="$2"
# remove trailing slash for consistency
path="${path%/}"
# if short enough, return as-is
if (( ${#path} <= maxlen )); then
printf '%s' "$path"
return
fi
# detect leading slash (absolute path)
local abs_prefix=""
if [[ "$path" == /* ]]; then
abs_prefix="/"
path="${path#/}"
fi
# split path into parts
IFS='/' read -ra parts <<< "$path"
local n=${#parts[@]}
# if too few parts, fallback to showing last chars
if (( n <= 4 )); then
local res="${abs_prefix}${path}"
if (( ${#res} <= maxlen )); then
printf '%s' "$res"
return
else
# fallback: show truncated tail
printf '%s' "...${res: -$((maxlen-3))}"
return
fi
fi
# build abbreviation keeping first two and last two components
local first1="${parts[0]}"
local first2="${parts[1]}"
local last2="${parts[n-2]}"
local last1="${parts[n-1]}"
local abbreviated="${first1}/${first2}/.../${last2}/${last1}"
if [[ -n "$abs_prefix" ]]; then
abbreviated="${abs_prefix}${abbreviated}"
fi
if (( ${#abbreviated} <= maxlen )); then
printf '%s' "$abbreviated"
return
fi
# if still too long, do balanced truncation of the abbreviated string
local remain=$((maxlen - 3))
local leftlen=$(( remain / 2 ))
local rightlen=$(( remain - leftlen ))
local left_part="${abbreviated:0:leftlen}"
local right_part="${abbreviated: -$rightlen}"
printf '%s' "${left_part}...${right_part}"
}
# CSV escaping helper: double any internal double-quotes and wrap in quotes
csv_escape() {
local s="$1"
s="${s//\"/\"\"}"
printf '"%s"' "$s"
}
# If CSV mode, initialize CSV file header
if [[ $CSV_MODE -ne 0 ]]; then
printf 'Path,File,JETVersion,Status\n' > "$CSV_FILE"
fi
# Print table header unless in csvonly mode (csv mode prints table to screen as normal)
if [[ $CSV_MODE -ne 2 ]]; then
printf "%-60s %-12s %-12s\n" "File" "JET Version" "Status"
printf "%-60s %-12s %-12s\n" "------------------------------------------------------------" "----------" "------------"
fi
MAX_FILE_WIDTH=60
# Processing loop - non-recursive vs recursive
if [[ $RECURSIVE -eq 0 ]]; then
# Non-recursive
# Print the directory header for table output unless in csvonly
if [[ $CSV_MODE -ne 2 ]]; then
display_dir=$(abbrev_dir "$DIR" $MAX_FILE_WIDTH)
printf "%-60s %-12s %-12s\n" "$display_dir" "" ""
fi
find "$DIR" -maxdepth 1 -type f \( -iname "*.mdb" -o -iname "*.accdb" \) -print0 | while IFS= read -r -d '' file; do
# Build full absolute path for the file
fullpath=$(cd "$(dirname "$file")" 2>/dev/null && printf "%s/%s" "$(pwd -P)" "$(basename "$file")" || printf '%s' "$file")
# Run mdb-ver and capture output
jetver=$(mdb-ver "$file" 2>&1)
rc=$?
if [[ $rc -ne 0 ]]; then
status="Unsupported"
jetver="Error"
else
case "$jetver" in
*ACE16*) status="OK" ;;
*ACE15*) status="OK" ;;
*ACE14*) status="OK" ;;
*ACE12*) status="OK" ;;
*JET4*) status="OK" ;;
*JET3.5*) status="OK" ;;
*JET3*) status="MDBTOOLS" ;;
*) status="Unsupported" ;;
esac
fi
# If CSV mode (either csv or csvonly) write CSV row
if [[ $CSV_MODE -ne 0 ]]; then
filename="$(basename "$file")"
csv_line="$(csv_escape "$fullpath"),$(csv_escape "$filename"),$(csv_escape "$jetver"),$(csv_escape "$status")"
printf '%s\n' "$csv_line" >> "$CSV_FILE"
fi
# Screen output: print table row unless csvonly
if [[ $CSV_MODE -ne 2 ]]; then
printf "%-60s %-12s %-12s\n" "$(basename "$file")" "$jetver" "$status"
fi
# csvonly: indicate progress with a dot
if [[ $CSV_MODE -eq 2 ]]; then
printf '.' >&2
fi
done
# For csvonly, end the progress line
if [[ $CSV_MODE -eq 2 ]]; then
printf '\n' >&2
fi
else
# Recursive
prev_dir=""
find "$DIR" -type f \( -iname "*.mdb" -o -iname "*.accdb" \) -print0 | while IFS= read -r -d '' file; do
file_dir=$(dirname "$file")
if [[ "$file_dir" != "$prev_dir" ]]; then
# compute absolute directory path for display
abs_file_dir=$(cd "$file_dir" 2>/dev/null && pwd -P || printf '%s' "$file_dir")
# shorten using the abbrev_dir function and print header only if not csvonly
if [[ $CSV_MODE -ne 2 ]]; then
display_dir=$(abbrev_dir "$abs_file_dir" $MAX_FILE_WIDTH)
printf "%-60s %-12s %-12s\n" "$display_dir" "" ""
fi
prev_dir="$file_dir"
fi
jetver=$(mdb-ver "$file" 2>&1)
rc=$?
if [[ $rc -ne 0 ]]; then
status="Unsupported"
jetver="Error"
else
case "$jetver" in
*ACE16*) status="OK" ;;
*ACE15*) status="OK" ;;
*ACE14*) status="OK" ;;
*ACE12*) status="OK" ;;
*JET4*) status="OK" ;;
*JET3.5*) status="OK" ;;
*JET3*) status="MDBTOOLS" ;;
*) status="Unsupported" ;;
esac
fi
# Write CSV row if requested
if [[ $CSV_MODE -ne 0 ]]; then
fullpath=$(cd "$(dirname "$file")" 2>/dev/null && printf "%s/%s" "$(pwd -P)" "$(basename "$file")" || printf '%s' "$file")
filename="$(basename "$file")"
csv_line="$(csv_escape "$fullpath"),$(csv_escape "$filename"),$(csv_escape "$jetver"),$(csv_escape "$status")"
printf '%s\n' "$csv_line" >> "$CSV_FILE"
fi
# Screen output: print table row unless csvonly
if [[ $CSV_MODE -ne 2 ]]; then
printf "%-60s %-12s %-12s\n" "$(basename "$file")" "$jetver" "$status"
fi
# csvonly: progress indicator
if [[ $CSV_MODE -eq 2 ]]; then
printf '.' >&2
fi
done
if [[ $CSV_MODE -eq 2 ]]; then
printf 'DONE!\n' >&2
fi
fi