OBIETTIVO
Devo caricare circa 800 GB da un HDD meccanico (installato nel mio iMac, in configurazione Dual Drive con un SSD) al server OneDrive (OneDrive for Business). Purtroppo dal momento che il mio iMac non supporta macOS Mojave e che non ho intenzione di "duplicare" gli 800 GB di files fisicamente nella cartella OneDrive (presente su SSD) facendo il classico "caricamento da client" devo necessariamente caricare i files da browser (Chrome).
Fin qui tutto okay, con le dovute precauzioni: il browser potrebbe crashare ed essere più instabile nella connessione.
Sorvoliamo per un attimo questo problema ed arriviamo a quello più serio e già riscontrato:
PROBLEMA
Il caricamento da browser di OneDrive (ma credo anche da client) non accetta i files con i seguenti caratteri:
" * : < > ? / \ | [OneDrive Standard]
~ " # % & * : < > ? / \ { | } [OneDrive Server]
fonte: Microsoft
Come ben potete immaginare, dovendo fare il backup di un intero HDD sul cloud, ci sono migliaia di files con caratteri non validi (invece accettati dal Finder del Mac). Non è pensabile andarli a sostituire uno ad uno e, quindi, ho cercato online uno script (da inserire in Automator) che sostituisca i caratteri incriminati con caratteri accettati.
SOLUZIONE PARZIALE
Lo script trovato è quello sotto spoiler, tenendo a mente che la fonte da cui l'ho preso riporta una formattazione diversa (probabilmente corretta) ed i colori (grigio e rosso per renderne più chiara la lettura).
Codice: Seleziona tutto
#!/usr/bin/env bash
# The following characters are considered impermissible in a basename:
#
# - Left Square Bracket: [
# - Right Square Bracket: ]
# - Left Parenthesis: (
# - Reverse Solidus: \
# - Colon: :
# - Quotation Mark "
# - Single Quotation Mark '
# - Asterisk *
# - Question Mark ?
# - Less-than Sign <
# - Greater-than Sign >
# - Vertical Line |
# - Plus Sign +
# - Space Character
# - UnderScore _
#
# 1. Sed is utilized for character replacement therefore characters listed
# in the bracket expression [...] must be escaped as necessary.
# 2. Any forward slashes `/` in the basename are substituted by default with
# a Colon `:` at the shell level - so it's unnecessary to search for them.
#
declare -r IMPERMISSIBLE_CHARS="[][()\\:\"'*?<>|+_ ]"
declare -r REPLACEMENT_STRING="-"
# Obtain the POSIX path of each selected item in the `Finder`. Input must
# passed to this script via a preceding `Get Selected Finder Items` action
# in an Automator Services workflow.
declare selected_items=("$@")
declare -a sorted_paths
declare -a numbered_paths
# Prefix the POSIX path depth level to itself to aid sorting.
for ((i = 0; i < "${#selected_items[@]}"; i++)); do
numbered_paths+=("$(echo "${selected_items[$i]}" | \
awk -F "/" '{ print NF-1, $0 }')")
done
# Sort each POSIX path in an array by descending order of its depth.
# This ensures deeper paths are renamed before shallower paths.
IFS=$'\n' read -rd '' -a sorted_paths <<< \
"$(printf "%s\\n" "${numbered_paths[@]}" | sort -rn )"
# Logic to perform replacement of impermissible characters in a path basename.
# @param: {Array} - POSIX paths sorted by depth in descending order.
renameBaseName() {
local paths=("$@") new_basename new_path
for path in "${paths[@]}"; do
# Remove numerical prefix from each $path.
path="$(sed -E "s/^[0-9]+ //" <<< "$path")"
# Replaces impermissible characters in path basename
# and subsitutes uppercase characters with lowercase.
new_basename="$(sed "s/$IMPERMISSIBLE_CHARS/$REPLACEMENT_STRING/g" <<< \
"$(basename "${path}")" | tr "[:upper:]" "[:lower:]")"
# Concatenate original dirname and new basename to form new path.
new_path="$(dirname "${path}")"/"$new_basename"
# Only rename the item when:
# - New path does not already exist to prevent data loss.
# - New basename length is less than or equal to 255 characters.
if ! [ -e "$new_path" ] && [[ ${#new_basename} -le 255 ]]; then
mv -n "$path" "$new_path"
fi
done
}
renameBaseName "${sorted_paths[@]}"
QUESITO
Qualcuno sa aiutarmi a modificare lo script, andando a contrassegnare come "caratteri proibiti" solo quelli indicati da Microsoft in rosso?
Purtroppo sono una capra e non saprei farlo da solo (ci ho provato, ma non ha funzionato).