Giter Club home page Giter Club logo

appendfilename's Introduction

appendfilename.py

bin/screencast.gif

This Python script adds a string to a file name. The string gets added between the original file name and its extension.

In case the file name contains tags as handled as with filetag, the string gets added right before the separator between file name and tags.

Examples for adding the string “new text”:

old file namenew file name
a simple file.txta simple file new text.txt
a simple file – foo bar.txta simple file new text – foo bar.txt
2013-05-09.jpeg2013-05-09 new text.jpeg
2013-05-09T16.17.jpeg2013-05-09T16.17 new text.jpeg
2013-05-09T16.17_img_00042.jpeg2013-05-09T16.17_img_00042 new text.jpeg
2013-05-09T16.17_img_00042 – fun.jpeg2013-05-09T16.17_img_00042 new text – fun.jpeg

Why

Besides the fact that I am using ISO dates and times in file names (as shown in examples above), I am using tags with file names. To separate tags from the file name, I am using the four separator characters: space dash dash space.

For people familiar with Regular Expressions:

(<ISO date/time stamp>)? <descriptive file name> -- <list of tags separated by spaces>.extension

For tagging, please refer to filetag and its documentation.

If I want to add a descriptive file name to files like , e.g. , photographs, I have to rename the original file and insert the description at the right spot of the existing file name.

This is an error-prone task. If I am not careful, I can overwrite parts of the old file name I wanted to keep. Or I could mess up spacing between the old file name, tags, and the new description.

Therefore, I wrote this script that adds a description to the file name without removing old file name parts or tags.

You may like to add this tool to your image or file manager of choice. I added mine to geeqie which is my favorite image viewer on GNU/Linux.

Usage

appendfilename --text foo a_file_name.txt

… adds “foo” such that it results in a_file_name foo.txt

appendfilename a_file_name.txt

… (implicit) interactive mode: asking for the string to add from the user

appendfilename --text "foo bar" "file name 1.jpg" "file name 2 -- foo.txt" "file name 3 -- bar.csv"

… adds tag “foo” such that it results in …

"file name 1 foo bar.jpg"
"file name 2 foo bar -- foo.txt"
"file name 3 foo bar -- bar.csv"

For a complete list of parameters, please try:

appendfilename --help

The file names within the current working directory is read in and all found words can be completed via TAB.


./appendfilename/__init__.py --help
Usage:
    appendfilename [<options>] <list of files>

This tool inserts text between the old file name and optional tags or file extension.


Text within file names is placed between the actual file name and
the file extension or (if found) between the actual file namd and
a set of tags separated with " -- ".
  Update for the Boss  <NEW TEXT HERE>.pptx
  2013-05-16T15.31.42 Error message <NEW TEXT HERE> -- screenshot projectB.png

When renaming a symbolic link whose source file has a matching file
name, the source file gets renamed as well.

Example usages:
  appendfilename --text="of projectA" "the presentation.pptx"
      ... results in "the presentation of projectA.pptx"
  appendfilename "2013-05-09T16.17_img_00042 -- fun.jpeg"
      ... with interactive input of "Peter" results in:
          "2013-05-09T16.17_img_00042 Peter -- fun.jpeg"


:copyright: (c) 2013 or later by Karl Voit <[email protected]>
:license: GPL v3 or any later version
:URL: https://github.com/novoid/appendfilename
:bugreports: via github or <[email protected]>
:version: 2019-10-19


Options:
  -h, --help            show this help message and exit
  -t TEXT, --text=TEXT  the text to add to the file name
  -p, --prepend         Do the opposite: instead of appending the text,
                        prepend the text
  --smart-prepend       Like "--prepend" but do respect date/time-stamps:
                        insert new text between "YYYY-MM-DD(Thh.mm(.ss))" and
                        rest
  -s, --dryrun          enable dryrun mode: just simulate what would happen,
                        do not modify file(s)
  -v, --verbose         enable verbose mode
  -q, --quiet           enable quiet mode
  --version             display version and exit

Installation

Get it from GitHub or install it via pip install appendfilename

Smart Prepend

Although appendfilename was created mainly to add text at the end of a file name, it may also insert text at the beginning of a file name using the --prepend parameter.

A variance of that is --smart-prepend. Following examples demonstrate the effects on smart prepending “new text” with various file names:

new text foo bar.txt
2019-10-20 new text foo bar.txt
2019-10-20T12.34 new text foo bar.txt
2019-10-20T12.34.56 new text foo bar.txt

As you can see, --smart-prepend does take into account that a given date/time-stamp according to date2name and this article will always stay the first part of a file name, prepending the “new text” between the date/time-stamp and the rest.

Integration Into Common Tools

Integration into Windows File Explorer

The easiest way to integrate appendfilename into File Explorer (“Send to” context menu) is by using integratethis.

Execute this in your command line environment:

pip install appendfilename integratethis
integratethis appendfilename --confirm

Windows File Explorer for single files (manual method)

Use this only if the integratethis method can not be applied:

Create a registry file add_appendfilename_to_context_menu.reg and edit it to meet the following template. Please make sure to replace the paths (python, USERNAME and appendfilename) accordingly:

Windows Registry Editor Version 5.00

;; for files:

[HKEY_CLASSES_ROOT\*\shell\appendfilename]
@="appendfilename (single file)"

[HKEY_CLASSES_ROOT\*\shell\appendfilename\command]
@="C:\\Python36\\python.exe C:\\Users\\USERNAME\\src\\appendfilename\\appendfilename\\__init__.py -i \"%1\""

Execute the reg-file, confirm the warnings (you are modifying your Windows registry after all) and cheer up when you notice “appendfilename (single file)” in the context menu of your Windows Explorer.

As the heading and the link name suggests: this method works on single files. So if you select three files and invoke this context menu item, you will get three different filetag-windows to tag one file each.

Windows File Explorer for single and multiple selected files (manual method)

Use this only if the integratethis method can not be applied:

Create a batch file in your home directory. Adapt the paths to meet your setup. The content looks like:

C:\Python36\python.exe C:\Users\USERNAME\src\appendfilename\appendfilename\__init__.py -i %*

If you want to confirm the process (and see error messages and so forth), you might want to append as well following line:

set /p DUMMY=Hit ENTER to continue ...

My batch file is located in C:\Users\USERNAME\bin\appendfilename.bat. Now create a lnk file for it (e.g., via Ctrl-Shift-drag), rename the lnk file to appendfilename.lnk and move the lnk file to ~/AppData/Roaming/Microsoft/Windows/SendTo/.

This way, you get a nice entry in your context menu sub-menu “Send to” which is also correctly tagging selection of files as if you put the list of selected items to a single call of appendfilename.

Integrating into Geeqie

I am using geeqie for browsing/presenting image files. After I mark a set of images for adding file name descriptions, I just have to press a and I get asked for the input string. After entering the string and RETURN, the filenames are modified accordingly.

Using GNU/Linux, this is quite easy accomplished. The only thing that is not straight forward is the need for a wrapper script. The wrapper script does provide a shell window for entering the tags.

vk-appendfilename-interactive-wrapper-with-gnome-terminal.sh looks like:

#!/bin/sh

/usr/bin/gnome-terminal \
    --geometry=73x5+330+5  \
    --hide-menubar \
    -x /home/vk/src/appendfilename/appendfilename/__init__.py "${@}"

#end

In $HOME/.config/geeqie/applications I wrote two desktop files such that geeqie shows the wrapper scripts as external editors to its image files:

$HOME/.config/geeqie/applications/add-tags.desktop looks like:

[Desktop Entry]
Name=appendfilename
GenericName=appendfilename
Comment=
Exec=/home/vk/src/misc/vk-appendfilename-interactive-wrapper-with-gnome-terminal.sh %F
Icon=
Terminal=true
Type=Application
Categories=Application;Graphics;
hidden=false
MimeType=image/*;video/*;image/mpo;image/thm
Categories=X-Geeqie;

In order to be able to use the keyboard shortcuts a, you can define them in geeqie:

  1. Edit > Preferences > Preferences … > Keyboard.
  2. Scroll to the bottom of the list.
  3. Double click in the KEY-column of appendfilename and choose your desired keyboard shortcut accordingly.

I hope this method is as handy for you as it is for me :-)

Integration into Thunar

Thunar is a popular GNU/Linux file browser for the xfce environment.

Unfortunately, it is rather complicated to add custom commands to Thunar. I found a good description which you might want to follow.

To my disappoinment, even this manual confguration is not stable somehow. From time to time, the IDs of $HOME/.config/Thunar/uca.xml and $HOME/.config/Thunar/accels.scm differ.

For people using Org-mode, I automated the updating process (not the initial adding process) to match IDs again:

Script for checking “tag”: do it tag-ID and path in accels.scm match?

#+BEGIN_SRC sh :var myname="tag"
ID=`egrep -A 2 "<name>$myname" $HOME/.config/Thunar/uca.xml | grep unique-id | sed 's#.*<unique-id>##' | sed 's#<.*$##'`
echo "$myname-ID of uca.xml: $ID"
echo "In accels.scm: "`grep -i "$ID" $HOME/.config/Thunar/accels.scm`
#+END_SRC

If they don’t match, following script re-writes accels.scm with the current ID:

#+BEGIN_SRC sh :var myname="tag" :var myshortcut="<Alt>t"
ID=`egrep -A 2 "<name>$myname" $HOME/.config/Thunar/uca.xml | grep unique-id | sed 's#.*<unique-id>##' | sed 's#<.*$##'`
echo "appending $myname-ID of uca.xml to accels.scm: $ID"
mv $HOME/.config/Thunar/accels.scm $HOME/.config/Thunar/accels.scm.OLD
grep -v "\"$myshortcut\"" $HOME/.config/Thunar/accels.scm.OLD > $HOME/.config/Thunar/accels.scm
rm $HOME/.config/Thunar/accels.scm.OLD
echo "(gtk_accel_path \"<Actions>/ThunarActions/uca-action-$ID\" \"$myshortcut\")" >> $HOME/.config/Thunar/accels.scm
#+END_SRC

Integration into FreeCommander

FreeCommander is a orthodox file manager for Windows. You can add appendfilename as an favorite command:

  • Tools → Favorite tools → Favorite tools edit… (S-C-y)
    • Create new toolbar (if none is present)
    • Icon for “Add new item”
      • Name: appendfilename
      • Program or folder: <Path to appendfilename.bar>
    • appendfilename.bat looks like: (please do modify the paths to meet your requirement)
      C:\Python36\python.exe C:\Users\YOURUSERNAME\src\appendfilename\appendfilename\__init__.py %*
      REM optionally: set /p DUMMY=Hit ENTER to continue...
              
      • Start folder: %ActivDir%
      • Parameter: %ActivSel%
      • [X] Enclose each selected item with =”=
      • Hotkey: select next available one such as Ctrl-1 (it gets overwritten below)
    • remember its name such as “Favorite tool 01”
      • OK

So far, we’ve got appendfilename added as a favorite command which can be accessed via menu or icon toolbar and the selected keyboard shortcut. If you want to assign a different keyboard shortcut than Ctrl-1 like Alt-a you might as well follow following procedure:

  • Tools → Define keyboard shortcuts…
    • Scroll down to the last section “Favorite tools”
    • locate the name such as “Favorite tool 01”
    • Define your shortcut of choice like Alt-a in the right hand side of the window
      • If your shortcut is taken, you’ll get a notification. Don’t overwrite essential shortcuts you’re using.
    • OK

Related tools and workflows

This tool is part of a tool-set which I use to manage my digital files such as photographs. My work-flows are described in this blog posting you might like to read.

In short:

For tagging, please refer to filetags and its documentation.

See date2name for easily adding ISO time-stamps or date-stamps to files.

For easily naming and tagging files within file browsers that allow integration of external tools, see appendfilename (once more) and filetags.

Moving to the archive folders is done using move2archive.

Having tagged photographs gives you many advantages. For example, I automatically choose my desktop background image according to the current season.

Files containing an ISO time/date-stamp gets indexed by the filename-module of Memacs.

Here is a 45 minute talk I gave at Linuxtage Graz 2018 presenting the idea of and workflows related to appendfilename and other handy tools for file management:

bin/2018-05-06 filetags demo slide for video preview with video button -- screenshots.png

How to Thank Me

I’m glad you like my tools. If you want to support me:

  • Send old-fashioned postcard per snailmail - I love personal feedback!
  • Send feature wishes or improvements as an issue on GitHub
  • Create issues on GitHub for bugs
  • Contribute merge requests for bug fixes
  • Check out my other cool projects on GitHub

appendfilename's People

Contributors

bdillahu avatar nbehrnd avatar novoid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

appendfilename's Issues

renaming file which is a link to same file name doesn't work recursive

appendfilename should rename all linked "parent links" as long as the file name is the same. However, this seems to work only for the first link and not for all.

How to reproduce the issue

  1. create two nested TagTrees with https://github.com/novoid/filetags from files/* to tagtrees1/ and then from tagtrees1/tag/* to tagtrees2/ (all within the same parent directory)
  • this results in a file within tagtrees2/tag/foo -- tag.txt that points to tagtrees1/tag/foo -- tag.txt which points to files/foo -- tag.txt
  1. appendfilename -t "foobar" results in:
  • tagtrees2/tag/foo foobar -- tag.txt
  • tagtrees1/tag/foo foobar -- tag.txt
  • files/foo -- tag.txt <-- here, the renaming/recursion stops which is wrong. It should be: files/foo foobar -- tag.txt

Educated guess how to fix this

Adapt def handle_file_and_symlink_source_if_found(filename, text, dryrun) so that it works recursively just like filetags does with tags.

Help command line switch

I had to take a look at the code in this repo to find the available command line switches.
It would be nice if a --help and/org -h switch would show the syntax. Or, the syntax could be shown if no arguments are passed at all (instead of the current error message).

inconsistency --smart-prepend vs. date2name stamp

appendfilename functions differently for files stamped by date2name in either default, or --withtime time stamp.

Manual replication:

touch ping.txt
date2name ping.txt

appendfilename 2022-01-06_ping.txt -t new --smart-prepend

to yield

ERROR    Error while trying to build new filename: <class 'TypeError'>
ERROR    1 error(s) occurred. Please check output above.

An analogue programmatic replication with the test script approach (just submitted, incomplete coverage, under construction) with pytest/pytest-3 for Python3:

pytest-3 test_appendfilename.py -m "smart" -v

File 2021-12-31T18.48.22_test.txt is processed as 2021-12-31T18.48.22 book test.txt and thus successfully removed by the test script. However, test file 2021-12-31.txt is processed to 2021-12-31 book txt loosing the period en face of the file extension. (File test.txt is processed by appendfilename to book test.txt, thus indicating appendfilename works fine on this one.).

--smart-prepend option behaves like --prepend option when path is absolute

OS: Windows 10
Installed via: pip
I am happy to provide more information if needed.


Calling appendfilename with --smart-prepend option works as expected when given relative path:

appendfilename --smart-prepend relative/path/to/"2023-02-14 file.txt"
      ... with interactive input of "beginning" results in:
	  "2023-02-14 beginning file.txt"

However, calling it with an absolute path ignores the timestamp and acts like normal --prepend option:

appendfilename --smart-prepend absolute/path/to/"2023-02-14 file.txt"
      ... with interactive input of "beginning" results in:
	  "beginning 2023-02-14 file.txt"

Thunar integration by GUI

Hello Karl,
I learned about your tools by watching your presentation at the Frazer Linuxtage and tried them out afterwards.
I think your tools are absolutely great and I can use them to improve my workflow. I'm using Thunar as a file manager and I integrated your tools through the GUI Edit / Custom Actions, so that I have them in the right click menu. There was no problem with that at all and I think it is easier than editing the config of Thunar the way you are mentioning it in the readme. The only thing that is a little bit strange on my desktop is the position of the window that is opening in interactive mode. But this is an issue that comes because of my windows manager (i3) and is not related to thunar or your tools.

HAGO
Bjoern

appendfilename on Windows shortcut does not rename original file

When working with tag filter view or Tagtrees view of filetag, shortcuts are created whose file names correspond to the original file (but directory differs).

appendfilename is supposed to add additional text to the shortcut file as well as the original file (as long as their file names are same). This does not work with Windows 10.

feature suggest: harmonization of three pattern by date2name

The option --smart-prepend aims to keep time stamps (added by date2name) in front of the file name. However, for stamps assigned with either --compact, --month, or --short, the pattern generated differs; here, the text is leading ahead of the time stamp.

For example, in a live session of Xubuntu 20.04.2 LTS/Fossa and a pristine checkout of appendfilename/master, these are the observations:

  • compact time stamp:
xubuntu@xubuntu:~/Desktop/appendfilename/appendfilename$ python3 __init__.py 20211231_test.txt --smart-prepend --verbose -t book
DEBUG    2022-01-09 13:01:24,885 text found: [book]
DEBUG    2022-01-09 13:01:24,885 extracting list of files ...
DEBUG    2022-01-09 13:01:24,885 len(args) [1]
DEBUG    2022-01-09 13:01:24,885 1 filenames found: [20211231_test.txt]
DEBUG    2022-01-09 13:01:24,885 iterate over files ...
DEBUG    2022-01-09 13:01:24,885 options.smartprepend is set with ||book| |20211231_test|.txt
DEBUG    2022-01-09 13:01:24,885 options.smartprepend is set with |<class 'str'>|<class 'str'>|<class 'str'>|<class 'str'>|<class 'str'>
DEBUG    2022-01-09 13:01:24,885 can't find a date/time-stamp, doing a simple prepend
DEBUG    2022-01-09 13:01:24,885  renaming "20211231_test.txt"
DEBUG    2022-01-09 13:01:24,886       ⤷   "book 20211231_test.txt"
DEBUG    2022-01-09 13:01:24,886 successfully finished.
Please press <Enter> for finishing...
  • month pattern:
xubuntu@xubuntu:~/Desktop/appendfilename/appendfilename$ python3 __init__.py 2021-12_test.txt --smart-prepend --verbose -t book
DEBUG    2022-01-09 13:04:26,884 text found: [book]
DEBUG    2022-01-09 13:04:26,884 extracting list of files ...
DEBUG    2022-01-09 13:04:26,884 len(args) [1]
DEBUG    2022-01-09 13:04:26,884 1 filenames found: [2021-12_test.txt]
DEBUG    2022-01-09 13:04:26,884 iterate over files ...
DEBUG    2022-01-09 13:04:26,884 options.smartprepend is set with ||book| |2021-12_test|.txt
DEBUG    2022-01-09 13:04:26,884 options.smartprepend is set with |<class 'str'>|<class 'str'>|<class 'str'>|<class 'str'>|<class 'str'>
DEBUG    2022-01-09 13:04:26,884 can't find a date/time-stamp, doing a simple prepend
DEBUG    2022-01-09 13:04:26,884  renaming "2021-12_test.txt"
DEBUG    2022-01-09 13:04:26,885       ⤷   "book 2021-12_test.txt"
DEBUG    2022-01-09 13:04:26,885 successfully finished.
Please press <Enter> for finishing...
  • short pattern:
xubuntu@xubuntu:~/Desktop/appendfilename/appendfilename$ python3 __init__.py 211231_test.txt --smart-prepend --verbose -t book
DEBUG    2022-01-09 13:05:26,694 text found: [book]
DEBUG    2022-01-09 13:05:26,694 extracting list of files ...
DEBUG    2022-01-09 13:05:26,695 len(args) [1]
DEBUG    2022-01-09 13:05:26,695 1 filenames found: [211231_test.txt]
DEBUG    2022-01-09 13:05:26,695 iterate over files ...
DEBUG    2022-01-09 13:05:26,695 options.smartprepend is set with ||book| |211231_test|.txt
DEBUG    2022-01-09 13:05:26,695 options.smartprepend is set with |<class 'str'>|<class 'str'>|<class 'str'>|<class 'str'>|<class 'str'>
DEBUG    2022-01-09 13:05:26,695 can't find a date/time-stamp, doing a simple prepend
DEBUG    2022-01-09 13:05:26,695  renaming "211231_test.txt"
DEBUG    2022-01-09 13:05:26,695       ⤷   "book 211231_test.txt"
DEBUG    2022-01-09 13:05:26,695 successfully finished.
Please press <Enter> for finishing...

These observations are coherent with the automatic testing with the test script for pytest for Python 3 just extended, e.g., by

pytest-3  test_appendfilename.py -m "smart" -v

Prepend filename command line switch

I am trying some of your tools and found a need to prepend some text to the filename. That is, between the date/timestamp and the original filename.

For example the command

appendfilename --prepend --text="Invoice" *.pdf

would change the name of

2018-11-16 Apple 123456.pdf

to

2018-11-16 Invoice Apple 123456.pdf

Optionally removing words from file names

There are certain situations where I need to remove one or more words from selected file names.

One example is after added a set of words that contain a typo.

An implementation of this would also addresses #8

Constraints

This issue is a central spot to collect ideas on how to come up with a consistent concept to remove words. This has to work on one file as well as on a group of selected files. It has to work on file names that end with the same set of words as well as with sets of files that do have completely different file names.

Furthermore, there should be ways to deal with edge cases. A few examples:

  • Removing word(s) would produce file name conflicts (two ore more files with same file name in same directory)
  • User requested to remove words that are not available in file names
  • Resulting file name would be empty

Brainstorming

  • The user might be able to interactively enter -2 which results in removing the last two words (separated by spaces or underscores) from the file name.
  • Similar to removing tags with filetags, the user might be able to interactively enter -"foo bar" or -foo to remove the words "foo bar" or "foo" from the end of the existing file names of selected files. If not found, nothing gets removed. Filetags are ignored and are not considered being part of the file name here.
  • ...

Contribute!

Please add your ideas here as well!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.