This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
1 OneDrive Filename Fix.sh
237 lines (181 loc) · 7 KB
/
1 OneDrive Filename Fix.sh
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
#!/bin/bash
###################################################################
#
# Script to check user's Desktop, Documents and OneDrive folders
# for any illegal characters, leading or trailing spaces and
# overlong path names and to correct them to help allow smooth
# synching in OneDrive.
#
# Date: Thu 27 Feb 2018 10:00:33 BST
# Version: 0.1.3
# Creator: dsavage
#
##################################################################
# Clear any previous temp files
rm /tmp/*.ffn
# Get the user
uun=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "\n");'`
# Remove local fstemps so they won't clog the server
find /Users/$uun -name ".fstemp*" -exec rm -dfR '{}' \;
# Find the OneDrive folder location assuming it has OneDrive in the name...
onedrive=`find /Users/$uun -type d -name "OneDrive*" -not -path "*/Library*" -not -path "*/Music*" -not -path "*/Pictures*" -not -path "*/Downloads*" -not -path "*.pkg" -not -path ".Trash"`
Check_Long_Names ()
{
echo File paths longer than 230 characters may not synchronise to the server. The following are the files that are in that category on your system and have now been temporarily moved to a folder called LongFileNames inside your home folder. If you could please shorten the names of these files and move the shortened versions back into either your Desktop or Documents folder, they will then synchronise to the server as normal: > /tmp/clnOut.ffn
echo " " >> /tmp/clnOut.ffn
linecount=`wc -l /tmp/cln.ffn | awk '{print $1}'`
counter=$linecount
while ! [ "$counter" == 0 ]; do
line="`sed -n ${counter}p /tmp/cln.ffn`"
test=`echo $line | wc -m | awk '{print $1}'`
if [ $test -gt "230" ]
then
mkdir /Users/$uun/LongFileNames
mv "$line" /Users/$uun/LongFileNames
open /Users/$uun/LongFileNames
echo Number of Chars = $test Path = $line
echo Number of Chars = $test Path = $line >> /tmp/clnOut.ffn
fi
let "counter = $counter -1"
done
}
Check_Trailing_Chars ()
{
cat /tmp/cln.ffn | grep -v ".pkg" | grep -v ".app" > /tmp/fixtrail.ffn
linecount=`wc -l /tmp/fixtrail.ffn | awk '{print $1}'`
counter=$linecount
while ! [ "$counter" == 0 ]; do
line="`sed -n ${counter}p /tmp/fixtrail.ffn`"
lastChar="`sed -n ${counter}p /tmp/fixtrail.ffn | grep -Eo '.$'`"
if [ "$lastChar" == " " ] || [ "$lastChar" == "." ]
then
name=`basename "$line"` # get the filename we need to change
path=`dirname "$line"` # dirname to get the path
fixedname=$(echo "$name" | tr '.' '-' | awk '{sub(/[ \t]+$/, "")};1') # remove/replace the trailing whitespace or period
mv -f "$line" "$path/$fixedname" # rename the file or folder
fi
let "counter = $counter -1"
done
}
Check_Leading_Spaces ()
{
cat /tmp/cln.ffn | grep -v ".pkg" | grep -v ".app" | grep "/[[:space:]]" > /tmp/fixlead.ffn
linecount=`wc -l /tmp/fixlead.ffn | awk '{print $1}'`
counter=$linecount
while ! [ "$counter" == 0 ]; do
line="`sed -n ${counter}p /tmp/fixlead.ffn`"
name=`basename "$line"` # get the filename we need to change
path=`dirname "$line"` # dirname to get the path
fixedname=`echo $name | sed -e 's/^[ \t]*//'` # sed out the leading whitespace
echo "This is the original - $line"
echo "This is the fix - $path/$fixedname"
mv -f "$line" "$path/$fixedname" # rename the file or folder
let "counter = $counter -1"
done
}
Fix_Names ()
{
# Count the number of lines in the fixdname.ffn or fixfname.ffn file
linecount=`wc -l /tmp/${1}.ffn | awk '{print $1}'`
counter=$linecount
while ! [ "$counter" == 0 ]; do
line="`sed -n ${counter}p /tmp/${1}.ffn`"
name=`basename "$line"` # get the filename we need to change
path=`dirname "$line"` # dirname to get the path
fixedname=$(echo "$name" | tr ':' '-' | tr '\\\' '-' | tr '?' '-' | tr '*' '-' | tr '"' '-' | tr '<' '-' | tr '>' '-' | tr '%' '-' | tr '|' '-' ) # sed out the leading whitespace
echo "$fixedname" >> /tmp/allfixed.ffn
mv -f "$line" "$path/$fixedname" # rename the file or folder
let "counter = $counter -1"
done
}
find /Users/$uun/Documents -name '*[\\/:*?"<>%|]*' -print >> /tmp/acln.ffn
find /Users/$uun/Desktop -name '*[\\/:*?"<>%|]*' -print >> /tmp/acln.ffn
find "${onedrive}" -name '*[\\/:*?"<>%|]*' -print >> /tmp/acln.ffn
# First pass at fixing the file names
Fix_Names acln
sleep 1
echo "++++++++++++++++++ fixed illegal chars"
#rm -f /tmp/cln.ffn
open /tmp/acln.ffn
find /Users/$uun/Documents -name "*" >> /tmp/cln.ffn
find /Users/$uun/Desktop -name "*" >> /tmp/cln.ffn
find "${onedrive}" -name "*" >> /tmp/cln.ffn
Check_Trailing_Chars
echo "++++++++++++++++++ fixed trailing spaces and periods"
sleep 1
rm -f /tmp/cln.ffn
find /Users/$uun/Documents -name "*" >> /tmp/cln.ffn
find /Users/$uun/Desktop -name "*" >> /tmp/cln.ffn
find "${onedrive}" -name "*" >> /tmp/cln.ffn
Check_Leading_Spaces
echo "++++++++++++++++++ fixed leading spaces"
sleep 1
rm -f /tmp/cln.ffn
find /Users/$uun/Documents -name "*" >> /tmp/cln.ffn
find /Users/$uun/Desktop -name "*" >> /tmp/cln.ffn
find "${onedrive}" -name "*" >> /tmp/cln.ffn
Check_Long_Names
sleep 1
rm -f /tmp/cln.ffn
testlong=`grep "$uun" /tmp/clnOut.ffn`
#chown -R $uun:staff /Users/$uun/Desktop
#chown -R $uun:staff /Users/$uun/Documents
#chown -R $uun:staff /Users/$uun/LongFileNames
#send message stuff
Send_Message ()
{
cat <<EOF > /tmp/message.txt
It is necessary for folder or file names containing any illegal characters to be renamed. These characters are the following: \ / : * ? " < > % | they also include leading spaces, trailing . and trailing spaces.
This message is to advise you of any such files or folders that have been affected so that you are aware that of these characters in their names will now have been replaced by a hyphen or removed.
Here are the files and folders that have been renamed in your case:
EOF
#cat /tmp/fixfname.ffn /tmp/fixdname.ffn > /tmp/allfixed.ffn
cat /tmp/message.txt /tmp/allfixed.ffn > /tmp/fullmessage.txt
if ! [ -z "$testlong" ]
then
cat /tmp/fullmessage.txt /tmp/clnOut.ffn > /tmp/tmpmessage.txt
cat /tmp/tmpmessage.txt > /tmp/fullmessage.txt
fi
echo " " >> /tmp/fullmessage.txt
echo "If you have any queries on this matter, please contact the IS Helpline." >> /tmp/fullmessage.txt
echo " " >> /tmp/fullmessage.txt
echo "Regards, Operational Services" >> /tmp/fullmessage.txt
sudo -u $uun osascript <<EOF
set tmpmes to do shell script "echo /tmp/fullmessage.txt"
set message to POSIX file tmpmes
tell application "TextEdit"
activate
open message
end tell
EOF
}
BrokeFiles=`grep $uun /tmp/fixfname.ffn`
BrokeDirs=`grep $uun /tmp/fixdname.ffn`
Broke1Files=`grep $uun /tmp/fix1fname.ffn`
Broke1Dirs=`grep $uun /tmp/fix1dname.ffn`
if ! [ -z "$BrokeFiles" ] || ! [ -z "$Broke1Files" ]
then
var1="true"
else
var1="false"
fi
if ! [ -z "$BrokeDirs" ] || ! [ -z "$Broke1Dirs" ]
then
var2="true"
else
var2="false"
fi
if ! [ -z "$testlong" ]
then
var3="true"
else
var3="false"
fi
if [ "$var1" == "true" ] || [ "$var2" == "true" ] || [ "$var3" == "true" ]
then
#call the mail function
Send_Message
fi
# cleanup
#rm /tmp/*.ffn
exit 0;