drive icon indicating copy to clipboard operation
drive copied to clipboard

Auto replace space char in file and folder names

Open wooyek opened this issue 8 years ago • 4 comments

Using spaces is convenient through UI, but not so much through command line. And linux users are used to something else instead od space in names.

I there a feature (or could be) that local file and folder names have spaces auto replaced to a preconfigured char eg. _ or -?

wooyek avatar Nov 21 '17 08:11 wooyek

Currently not, but they might implement it in the future. (probably in the long long future, since there are still bugs that needed to be solved)

However, you can handle spaces by escaping it: drive push Filename\ with\ spaces.txt ~~If you want to have the local filenames containing spaces while the remote filenames having spaces replaced, you can first upload the file and then rename it.~~ This won't work, the remote side need to have the same names as the local ones

drive push Filename\ with\ spaces.txt
drive rename Filename\ with\ spaces.txt  Filename_with_spaces.txt

If you think doing this for each file is too tedious, then write a script to do the job! Let me know if you really need a script. :smiley:

davidhcefx avatar Feb 12 '18 19:02 davidhcefx

I have lot of files, a script would come handy :)

wooyek avatar Feb 12 '18 20:02 wooyek

Sorry I found that the remote side need to have the same names as the local ones! If we have different filenames on both sides, let's say our local folder name is "my long name folder" and our remote folder name is "my_long_name_folder", we couldn't simply run drive ls under that folder. Instead, we have to run drive ls my_long_name_folder to specify the replaced name explicitly.

Therefore, I recommend to rename on both sides, which is also the default behavior of the script. Here's the script:

#! /bin/bash
# space-replacer

# Usage: space-replacer [targetPath]

# Change to other char to replace if you want
char=_
# Also rename the names on the LOCAL SIDE as well
#   Beware that if set to false, the two sides would have different filenames,
#   thus some drive function won't work as expected. i.e. `drive ls` wont work
#   under those folders, but we need to use `drive ls folder_name` to specify
#   the replaced name instead.
changeLocal=true


ReplaceSpace(){
	arr=(`basename "$1"`)
	if [ ${#arr[@]} == 1 ]; then
		return 0
	else
		newname=${arr[0]}
		i=1
		while [ $i != ${#arr[@]} ]; do
			newname="$newname$char${arr[i]}"
			i=$((i+1))
		done
		echo "$newname"
		return 1
	fi
}

if ! [ -f "$1" ] && ! [ -d "$1" ]; then
	echo "Target not found: '$1'"
	exit
fi
op=
if ! $changeLocal; then
	op="-local=0"
fi
yesall=false

echo "Listing filenames..."
find -L $1 | sort -r > /tmp/space-replacer.list
exec 3</tmp/space-replacer.list
echo "Parsing..."
while read -u 3 name; do
	newname=$(ReplaceSpace "$name")
	if [ $? == 1 ]; then
		printf "[$name] -> [$newname] ? (y|n|yesall) "
		if $yesall; then
			echo "y"
			drive rename $op "$name" "$newname"
		else
			read ch
			case $ch in
				y|Y|yes) :;;
				yesall|all) yesall=true;;
				*) continue;; 
			esac
			drive rename $op "$name" "$newname"
		fi
	fi
done

davidhcefx avatar Feb 13 '18 22:02 davidhcefx

This is a bug not only when pushing (where i can fix everything with a script), but also when pulling. I share a folder with some people that has a space in the name. I can't just rename the remote folder for sake of convenience - even though I am the file owner, others synced the folder to their windows PCs already.

Right now this program does not work for me, and I must find something else due to this bug. This is a bummer since it's doing well on other directories. I don't think this should be a problem for the long long future - it breaks interchangeability with windows.

So I propose, when calling any function on paths, it should be encapsulated in "".

SemjonWilke avatar Dec 07 '19 13:12 SemjonWilke