Skip to main content
Question

Script to fix OneDrive file, folder, path errors


Show first post

Forum|alt.badge.img+7
rcorbin wrote:

@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:]]#}'


Forum|alt.badge.img+7
#!/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

Forum|alt.badge.img+7
  • Valued Contributor
  • November 18, 2021

@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). 

 


Forum|alt.badge.img+7
  • Valued Contributor
  • November 18, 2021

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? 

 


Forum|alt.badge.img+7
  • Contributor
  • November 18, 2021
teodle wrote:

@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

Forum|alt.badge.img+7
  • Valued Contributor
  • November 18, 2021

@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. 


Forum|alt.badge.img+7
  • Valued Contributor
  • November 19, 2021

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. 

 

 

 


Forum|alt.badge.img+7
  • Valued Contributor
  • November 19, 2021
teodle wrote:

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. 

 


Forum|alt.badge.img+7
  • Valued Contributor
  • November 19, 2021

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. 


Forum|alt.badge.img
  • New Contributor
  • November 30, 2021
HenryOzsoy wrote:

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!


Forum|alt.badge.img+7
  • Valued Contributor
  • January 6, 2022
winntech wrote:

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

 


Forum|alt.badge.img+7
teodle wrote:

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.


Forum|alt.badge.img+7
  • Valued Contributor
  • April 4, 2022
HenryOzsoy wrote:

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings