How to convert forbidden_chars uploaded by a user to "_"

Nextcloud-client gives an error like “Filename contains trailing spaces when a user has uploaded a file or folder with a “forbidden” character like a space in the front. At our school we work with 3 different OS’es: Mac, Windows and Linux. They all use different filesystems: hfs, ntfs and btrfs. The Nextcloud-server is hosted on openSUSE Linux so it supports most of “forbidden” characters. But off course… Windows does not. A user using MacOS can perfectly upload a file named: " kaka>! crazy°due .pdf” but another user, who this file is shared with using Windows, cannot sync this file and gets an error in the sync app.

I get this error the whole time since MacOS users can put anything into a file of folder name… So renaming file by file is so much work I would get a depression. So my question to you geniuses out there… Can we write some Bash script that I can put in a cronjob running every night so when a user used these “forbidden characters” there will be automatically converted into “_” or in the case of leading or trailing or invisible spaces just be deleted?

I know it is possible by using some regex in some loop function converting all forbidden chars like ['?', '<', '>', ':', '*', '|', '"', chr(0), "\n", "\r"] into “_” or just removing them from the “forbidden string”…

Just I am not so experienced in writing this kind of scripts…

Has anyone done this before? And are you willing to share this bash script with me? I would be really thankful for any advice accomplishing this.

1 Like

A folder with a dot at the and or the filename “con” are also problematic.

please use search the topic has been discussed often in the past e.g. Windows illegal filename characters - can't find the github bug report - #10 by RuudschMaHinda and there was an improvement in nc30 🪟📁📄 Ability to enforce windows compatible file names on the server · Issue #44963 · nextcloud/server · GitHub

I have not yet understood why such a function is not built into Nextcloud.
:thinking:

Many thanks in advance for the clarification! @wwe

as you can see it was finally included.

I’m not a dev so I can’t say for sure why it didn’t happen before, from Github discussions I have the feeling the position before was “why should we limit Linux and Mac users in favor of Windows?” and this statement is still valid but seems the pressure of the most limited OS is too high…

If you could switch the feature on and off, it wouldn’t be a restriction but an enhancement.

There are also other cases where the files should be renamed automatically during upload.

For example, it would make sense to automatically remove the prefix “IMG_” and “VID_” because not all cameras use the prefix and otherwise you can’t sort by file name because the prefix is in the way.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.