-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-rmSMBchars.sh
More file actions
executable file
·130 lines (123 loc) · 4 KB
/
test-rmSMBchars.sh
File metadata and controls
executable file
·130 lines (123 loc) · 4 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
#!/bin/bash
#####################################################################################################
#
# ABOUT THIS PROGRAM
#
# NAME
# test-rmSMBchars.sh - Remove illegal characters preventing OneDrive/SMB Copying or Sync.
#
# SYNOPSIS
# sudo test-rmSMBchars.sh [PATH]
#
####################################################################################################
#
# HISTORY
#
# Version: 1.1
#
# - 1.0 Sherman MacAdmins Slack, 24.01.2019 Initial Build
# - 1.1 Michael Tanner, 18.10.2021 Added path variable to script
#
####################################################################################################
#
verbose=1
#Set/clear variables
folder=$@
loop=0
loopnum=0
#Rotate Logs
logfile=~/Library/Logs/sanitizelog
archlog=~/Library/Logs/sanitizelogarch
progress=/private/var/tmp/sanitizeprogress
cat $logfile >> $archlog
rm $logfile
touch $logfile
touch $progress
osascript -e 'tell application "Terminal" to do script "tail -f ~/Library/Logs/sanitizelog"'\
-e 'tell application "Terminal" to do script "tail -f /private/var/tmp/sanitizeprogress"'\
-e 'tell application "Terminal" to activate'\
-e 'tell application "Terminal" to set position of window 1 to {1, 1}'\
-e 'tell application "Terminal" to set position of window 2 to {600, 1}'
sleep 2
date=$(date)
echo Sanitize Script >> $logfile
echo Script run date: $date >> $logfile
echo >> $logfile
#Only line break on new line
OIFS="$IFS"
IFS=$'\n'
while [ $loop = 0 ]; do
for fulldir in $(find $folder | grep -v ".DS_Store"); do
old=$fulldir #$(echo $fulldir | sed -e 's/[ ]/\\ /g')
file=$(basename "$old")
rootdir=$(dirname "$fulldir")
extension=$(echo $file | awk -F . '{print $NF}')
noext=0
if [ $file = $extension ]; then
noext=1
fi
# Swap Syntax file=$(echo $file | sed -e 's/[FIND]/REPLACE/g')
# Remove #%*\/?
file=$(echo $file | sed -e 's/[#%*\\\/?\"]//g')
# Repeat of above line with " and ' inverted
file=$(echo $file | sed -e "s/[#%*\\\/?\']//g")
# Swap : and | for _
file=$(echo $file | sed -e 's/[:|]/_/g')
# Swap & with and
file=$(echo $file | sed -e 's/[&]/and/g')
# Swap < { with (
file=$(echo $file | sed -e 's/[<{]/(/g')
# Swap > } with )
file=$(echo $file | sed -e 's/[}>]/)/g')
# Swap ~ with -
file=$(echo $file | sed -e 's/[~]/-/g')
#escape trailing *
file=$(echo $file | sed -e 's/[*]/\\*/g')
#remove extension
file="${file%.*}"
#remove trailing and leading spaces
file=$(echo $file | xargs )
#remove trailing .
file=$(echo $file | sed -e 's/\.$//g')
#remove trailing and leading spaces again
file=$(echo $file | xargs )
#remove leading .
file=$(echo $file | sed 's/\.*//')
file2=$(basename $old)
if [ $noext = 1 ]; then
echo file has no extension
else
#re add extension if not a directory
file=$file.$extension
fi
# Construct new file path
new=$rootdir/$file
#Rename
if [ $old != $new ]; then
mv $old $new
echo renamed $old to $new >> $logfile
fi
if [ $verbose = 1 ]; then
echo "fulldir:$fulldir"
echo "file:$file"
echo "rootdir: $rootdir"
echo "old: $old" >> $progress
echo "new: $new" >> $progress
echo "extension: $extension"
fi
lastline=$(tail -n 1 ~/Library/Logs/sanitizelog)
echo Processed file $file >> $progress
done
if [ $lastline = "loopcomplete" ]; then
loop=1
echo Last loop modified 0 files - Exiting >> $progress
fi
loopnum=$((loopnum+1))
echo Loop# $loopnum complete >> $logfile
echo loopcomplete >> $logfile
done
echo "
----------Script Complete----------
You can now close this window" >> $logfile
killall tail
rm $progress