You can't have two files by the same name at the same time, so you'll need to first create the directory under a temporary name, then move the file into it, then rename the directory. Or alternatively rename the file to a temporary name, create the directory, and finally move the file.
I see that Nautilus scripts can be written in any language. You can do this with the most pervasive scripting language, /bin/sh.
#!/bin/sh set -e for file do case "$file" in */*) TMPDIR="${file%/*}"; file="${file##*/}";; *) TMPDIR=".";; esac temp="$(mktemp -d)" mv -- "$file" "$temp" mv -- "$temp" "$TMPDIR/$file" done
Explanations:
set -e aborts the script on error. - The
for loop iterates over the arguments of the script. - The
case block sets TMPDIR to the directory containing the file. It works whether the argument contains a base name or a file path with a directory part. mktemp -d creates a directory with a random name in $TMPDIR. - First I move the file to the temporary directory, then I rename the directory. This way, if the operation is interrupted in the middle, the file still has its desired name (whereas in the rename-file-to-temp approach there's a point in time when the file has the wrong name).
If you want to remove the file's extension from the directory, change the last mv call to
mv -- "$temp" "$TMPDIR/${file%.*}"
${file%.*} takes the value of file and removes the suffix that matches .*. If the file has no extension, the name is left unchanged.