Xfce Forum

Sub domains
 

You are not logged in.

#1 2017-03-11 10:23:30

jack_the_pirate
Member
Registered: 2016-09-10
Posts: 59

Need help - Thunar Custom Action: append timestamp prefixe to filename

I've got a Thunar script with this only line:
cp "$@" "$@-$TS".txt
it appends a timestamp suffix and the .txt extension to the file I right-click in

Now, I want to append a timestamp prefix
and also add the .txt extension
but it doesn't work:
cp "$@" "$TS-$@".txt


This also appends a timestamp suffix to the filename:
cp $1 $1_$(date +"%d-%b-%Y_%H:%M:%S").txt

But, when I try to put the timestamp as a prefix, it doesn't work either:
cp $1 "$(date +"%d-%b-%Y_%H:%M:%S")"_"$1".txt


What am I doing wrong?
Why do adding timestamp suffixes work while prefixes don't?


Queen - Megadeth - Metallica - 80's

Offline

#2 2017-03-11 14:32:43

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: Need help - Thunar Custom Action: append timestamp prefixe to filename

Depends on which command parameter you are using in the custom action. If you use %f or %F, then the whole path and filename is being sent. So in the first instance:

cp "$@" "$@-$TS".txt

...the expanded command is:

cp /home/toz/test.text /home/toz/test.txt-11-Mar-2017_09:05:39.txt

...which works because the path piece (/home/toz) is at the front. However:

cp "$@" "$TS-$@".txt

...expands to:

cp /home/toz/test.text 11-Mar-2017_09:05:39-/home/toz/test.txt.txt

...and since you have unescaped /s, they are assumed to be path delimiters and that statement doesn't work.

Here is a simple debug custom action you can add to your list of custom actions to show you what the parameters will look like when sent to your script (requires zenity):

zenity --info --text="f=%f\nF=%F\nd=%d\nD=%D\nn=%n\nN=%N"

...in appearance settings, select all options. Running this on the test.txt file displays:
tca1.png

Another useful thing to do is to drop debug statements in your script to be able to capture errors to see what they are. Here is the script that I am calling with the custom action:

#!/bin/bash

TS="$(date +"%d-%b-%Y_%H:%M:%S")"

echo "running first command:" > ~/t.log 2>&1
cp "$@" "$@-$TS".txt >> ~/t.log 2>&1
echo "running second command:" >> ~/t.log 2>&1
cp "$@" "$TS-$@".txt >> ~/t.log 2>&1

...after you run the custom action, look at the ~/t.log file for info on any messages that may have resulted from the custom action.

running first command:
running second command:
cp: cannot create regular file '11-Mar-2017_09:05:39-/home/toz/test.txt.txt': No such file or directory

Also, to address why the .txt extension is being re-used (it's being re-used in both of your examples, once at the end and once at the beginning), is because $@ includes the whole filename including the extension that you are copying. If you want to have only one extension, you'll need to manipulate the incoming string and parse out the filename and extension.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2017-03-11 15:22:35

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: Need help - Thunar Custom Action: append timestamp prefixe to filename

Try this script file:

#!/bin/bash

TS="$(date +"%d-%b-%Y_%H:%M:%S")"

for f in "$@"
do

   dir=$(dirname "$f")
   fullfilename=$(basename "$f")
   extension="${fullfilename##*.}"
   filename="${fullfilename%.*}"

   # append timestamp
   cp "$f" "$dir/$filename-$TS.$extension"

   # prepend timestamp
   cp "$f" "$dir/$TS-$filename.$extension"

done

exit 0

It will work with single files (%f) and multiple files (%F).  Comment out one of the append or prepend actions if you don't want both to happen.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#4 2017-03-11 21:10:47

jack_the_pirate
Member
Registered: 2016-09-10
Posts: 59

Re: Need help - Thunar Custom Action: append timestamp prefixe to filename

Hi ToZ,

Thank you very much! big_smile

This script works great!

Your explanation is excellent!!! And permits me new possibilities by making some changes!

Thank you very very much!!!!


Queen - Megadeth - Metallica - 80's

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.015 seconds, 9 queries executed - Memory usage: 536.35 KiB (Peak: 537.2 KiB) ]