Skip to main content

We are working on a script that will fix file naming issues for users new to OneDrive. We have some users who have all kinds of MS illegal characters in their file names and folder names. One user has 800+ such names to fix.



Rather than have them do this manually, or buy third party software we decided to write some scripts that will find all these illegal characters/names and change them. This will be a policy in Self Service that will:
1. Run a script that looks over all the file names and generates a report.
2. Make the changes.
3. Create two reports. One that lists changes made, and a second that lists changes that could not be made and why.



Some questions for those more experienced than we are (we are new to Jamf as of July):




  • Has this been done/attempted before? I don't see anything, but want to make sure we are not re-inventing the wheel or going down a rabbit hole that will ultimately not work for some reason we are missing.

  • We could share this with others, would there be interest?

  • Any suggestions on how to handle the find/replace logic? We can use legal characters, but need to be mindful that we could change an illegal name to a legal, but existing name. For example if "/" was replaced with "-" we could have example/1.doc changed to example-1.doc which may already exist?



With OneDrive being so new for Mac users I can see how many will have this issue. HOpefully this works out and could be helpful to the larger Jamf community.

@strayer your script works perfectly. Thanks for that ! It would be just great if it could also do leading or ending spaces. Did you or @Heading ever figure that out ?



her you go champ

zmv '(**/)(*)' '$1${${2##[[:space:]]#}%%[[:space:]]#}'


#!/bin/zsh
###################################################################
#
# Script to check user's OneDrive folders and files
# for any illegal characters, leading or trailing spaces and
# overlong path names and to correct them to help allow smooth
# synching in OneDrive.
#
# Date: Wed 28 Jul 2021 12:08:33 AEST
# Version: 1
#
##################################################################

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )

if [[ -z "$loggedInUser" ]] || [[ "$loggedInUser" == 'root' ]] || [[ "$loggedInUser" == "loginwindow" ]] ; then
loggedInUser="$3"
fi
autoload zmv

oneDriveFolder=/Users/$loggedInUser/OneDrive\\ -\\ Oganisation\\ Name

if [[ -d "${oneDriveFolder}" ]] ; then

cd "${oneDriveFolder}"

zmv '(**/)(*)' '$1${2//[^A-Za-z0-9. ]/_}'
zmv '(**/)(*)' '$1${${2##[[:space:]]#}%%[[:space:]]#}'

else

echo "OneDrive Folder not Found"

fi
exit 0

@HenryOzsoy 

That command used to work for me but now I keep getting: 

zmv: error(s) in substitution: 

Only it doesn't tell me what the error(s) actually is (are). 

 


I've got users who are trying migrate years worth of Mac file names still--just today I watched a customer move a hierarchy into the OneDrive folder and immediately OneDrive reported 1,677 Sync errors. And the self-service policy that called a script similar to above that was tested and worked fine only a year ago now fails. What did you do, Tim Apple? 

 


@HenryOzsoy 

That command used to work for me but now I keep getting: 

zmv: error(s) in substitution: 

Only it doesn't tell me what the error(s) actually is (are). 

 


Hi @teodle 

I have this script still working. Please mind it does run in zsh

#!/bin/zsh
###################################################################
#
# Script to check user's OneDrive folders and files
# for any illegal characters, leading or trailing spaces and
# overlong path names and to correct them to help allow smooth
# synching in OneDrive.
# Must run in terminal as: sudo zsh scriptname.sh
# Date: Wed 28 Jul 2021 12:08:33 AEST
# Version: 1
#
###################################################################

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
if [[ -z "$loggedInUser" ]] || [[ "$loggedInUser" == 'root' ]] || [[ "$loggedInUser" == "loginwindow" ]] ; then
loggedInUser="$3"
fi

autoload zmv

oneDriveFolder=/Users/$loggedInUser/OneDrive\\ -\\ Your\\ Organisation\\ Name

if [[ -d "${oneDriveFolder}" ]] ; then
cd "${oneDriveFolder}"
zmv '(**/)(*)' '$1${2//[^A-Za-z0-9. ]/_}'
zmv '(**/)(*)' '$1${${2##[[:space:]]#}%%[[:space:]]#}'
else
echo "OneDrive Folder not Found"
fi
exit 0

@HenryOzsoy 

I keep getting those substitution errors returned by zmv. My script has run under zsh, just like yours. In fact I copied yours just now and substituted the path for our org's OneDrive folder and it still errors. M1 Mac running Big Sur 11.5.2. Tearing out my hair. Just had to disable the Self Service Policy because it won't work. Can't figure it out. 


Okay everyone, I figured it out. It's not that the commands don't work. It's that the commands won't work on the OneDrive Folder. I cannot figure it out. 

 If I create a folder on my desktop called test,  nest a bunch of illegal file and foldernames there, then modify the variable in the script above so that it points to that folder, the script works perfectly. But when I point it at the OneDrive folder, I always get the following error, and no illegal  file/folder names are changed:

zmv: error(s) in substitution:

file exists: Icon_

I can run the zmv command from within  subdirectories in the OneDrive folder and it does what I expect it to do. 

If I run the command straight from terminal within the top level directory of the OneDrive folder, I get the same error as above. 

This worked last spring. Obviously both Mac OS X and the OneDrive app have been updated since then. 

There's something about the top level of the OneDrive Folder that's stopping zmv from doing what I want it to do. 

 

 

 


Okay everyone, I figured it out. It's not that the commands don't work. It's that the commands won't work on the OneDrive Folder. I cannot figure it out. 

 If I create a folder on my desktop called test,  nest a bunch of illegal file and foldernames there, then modify the variable in the script above so that it points to that folder, the script works perfectly. But when I point it at the OneDrive folder, I always get the following error, and no illegal  file/folder names are changed:

zmv: error(s) in substitution:

file exists: Icon_

I can run the zmv command from within  subdirectories in the OneDrive folder and it does what I expect it to do. 

If I run the command straight from terminal within the top level directory of the OneDrive folder, I get the same error as above. 

This worked last spring. Obviously both Mac OS X and the OneDrive app have been updated since then. 

There's something about the top level of the OneDrive Folder that's stopping zmv from doing what I want it to do. 

 

 

 


SOLVED: zmv won't do anything if there's a file called "Icon_" in the top level directory that you're wanting to do pattern matching/substitution.  Why on earth would it care? It's just a file. I removed that file (have no idea how it got there or what it's for (some sort of legacy resource fork artifact?) and then all my commands and scripts proceeded to execute without error. 

 


got it into a policy in our TEST environment and it works great. (as long as the OneDrive folder doesn't have a file named "Icon_")...... KUDOS to @HenryOzsoy for the zmv syntax that strips leading and trailing whitespace. 

KUDOS to @franton earlier for helping tighten up my scripts with regards to notifications. 


Hi @teodle 

I have this script still working. Please mind it does run in zsh

#!/bin/zsh
###################################################################
#
# Script to check user's OneDrive folders and files
# for any illegal characters, leading or trailing spaces and
# overlong path names and to correct them to help allow smooth
# synching in OneDrive.
# Must run in terminal as: sudo zsh scriptname.sh
# Date: Wed 28 Jul 2021 12:08:33 AEST
# Version: 1
#
###################################################################

loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
if [[ -z "$loggedInUser" ]] || [[ "$loggedInUser" == 'root' ]] || [[ "$loggedInUser" == "loginwindow" ]] ; then
loggedInUser="$3"
fi

autoload zmv

oneDriveFolder=/Users/$loggedInUser/OneDrive\\ -\\ Your\\ Organisation\\ Name

if [[ -d "${oneDriveFolder}" ]] ; then
cd "${oneDriveFolder}"
zmv '(**/)(*)' '$1${2//[^A-Za-z0-9. ]/_}'
zmv '(**/)(*)' '$1${${2##[[:space:]]#}%%[[:space:]]#}'
else
echo "OneDrive Folder not Found"
fi
exit 0

I'm sorry for the dumb question, I don't know where to add my onedrive folder name and I am having errors like OneDrive Folder not Found. Could you please show me an example assuming my one drive folder name is OneDrive - WinnTech.

Thank you!


I'm sorry for the dumb question, I don't know where to add my onedrive folder name and I am having errors like OneDrive Folder not Found. Could you please show me an example assuming my one drive folder name is OneDrive - WinnTech.

Thank you!


you have to escape the spaces that Microsoft OneDrive uses when setting up default folders for each org. In your case, I believe your variable declaration would look like this: 

 

oneDriveFolder=/Users/$loggedInUser/OneDrive\\ -\\ WinnTech

 


SOLVED: zmv won't do anything if there's a file called "Icon_" in the top level directory that you're wanting to do pattern matching/substitution.  Why on earth would it care? It's just a file. I removed that file (have no idea how it got there or what it's for (some sort of legacy resource fork artifact?) and then all my commands and scripts proceeded to execute without error. 

 


you my want to incorporate rm -rf Icon_ after 

cd "${oneDriveFolder}"

as OneDrive creates this file in senarios where you would have signed into a pc, then signed into your OneDrive to sync your profile on the pc. Your correct with this file located in the root folder zmv seems to do nothing.


you my want to incorporate rm -rf Icon_ after 

cd "${oneDriveFolder}"

as OneDrive creates this file in senarios where you would have signed into a pc, then signed into your OneDrive to sync your profile on the pc. Your correct with this file located in the root folder zmv seems to do nothing.


going to add that to the script. I suppose it would be best practice to test for the existence of  the file, then remove it if it's there. 

Thanks for the explanation of how/why that "Icon_" file is created!!

 


Reply