Massen Umbenennung mit Ruby

  • Impressum
  • Administration
  • Kontaktformular
  • Jabber
  • Tagcloud
  • Twitter
  • Soup
  • Github

roothausen

Massen Umbenennung mit Ruby

05:02

Monday, May 12. 2008

Nachdem ich mich in letzter Zeit so nebenbei etwas mit Ruby beschaeftigt habe und mir heute nach dem Download einiger Podcasts die Dateien mit "%20" im Dateinamen praesentiert wurden, wollte ich das mal mit Ruby statt zmv loesen.
Nach etwa zwei Minuten ist dabei dieser Einzeiler, der sich direkt auf der Ruby-Shell (irb) ausfuehren laesst, heraus gekommen:


Dir["*.mp3"].each { |file| File.rename(file, file.gsub(/%20/, "_")) }
 

Hier wird einfach bei jeder *.mp3 Datei im Verzeichnis das %20 im Dateinamen durch einen Unterstrich ersetzt.

Posted by admin in computer | Comments (4) | Trackbacks (0)
Defined tags for this entry: computer, programming, regular expression, ruby, software, stuff, tail -f /var/log/life
Related entries by tags:
  • Mobile devices or: How I Learned to Stop Worrying and Love the iOS
  • The VP8 Video Codec
  • My Bachelor Thesis: Scale the realtime web
  • Distributed contact management using HTTP
  • Gesichtserkennung/Eigenfaces
< VIM Bindings in newsbeuter nutzen | Debian upgraden >

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

*Also Ruby ist ja eine sehr schöne Sprache, aber man muss sie nicht auf biegen und brechen für alles einsetzen.

In deisem Fall hätte es bspw. ein Simples rename %20 &quot; &quot; *.mp3 auch getan. ;-)

#1 Anonymous Coward on 2008-05-12 10:55 (Reply)

*Ja wie gesagt: Normalerweise setze ich fuer sowas zvm ein, dass ja sowieso Bestandteil der zsh ist.
Allerdings wollte ich das mal in Ruby ausprobieren um die Sprache besser kennen zu lernen.

Zudem ist mir gerade aufgefallen, dass rename nur das erste vorkommende %20 ersetzt. Die Manpage sagt dazu:


rename will rename the specified files by replacing the first occurrence of from in their name by to.

#2 pfleidi (Homepage) on 2008-05-12 15:47 (Reply)

*#!/bin/sh
for i in %20
do
mv "$i" "$(echo $i | sed -e s/'%20'/' '/g)"
done

#3 ths on 2008-05-13 12:26 (Reply)

*@ths: MIr ist auch bekannt wie man sowas als Shellscript macht ;-)

#4 pfleidi (Homepage) on 2008-05-13 15:43 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Markdown format allowed
 
 

JavaScript String .fromCharCode

Calendar

« February '12 »
Mo Tu We Th Fr Sa Su
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Quicksearch

Kategorien

  • XML computer
  • XML misc
  • XML web
  • XML zeitgeist


All categories

Archiv

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • Recent...
  • Older...

Feeds

  • XML RSS 2.0 feed
  • ATOM/XML ATOM 1.0 feed
  • XML RSS 2.0 Comments

Links

Retinacast
Shackspace
Yaxim
Radio Tux
Kais Blog
Blumen Pfleiderer
Alk
paxos
filzo
Marc Seeger
polzifer
Moritz Haarmann

Tags

android bad world blog blogging browser changes code comic computer contentmanagement encryption feedreader firefox free fun google gui hardware howto html im jabber java life lighttpd linux linux&unix markup media misc mobile murphy networking newsbeuter opensource picture politics presentation privacy programming regular expression rss ruby s9y scala screenshot sdk security server shortys software stuff tail -f /var/log/life test tool tv unix video web webdesign webwide windows xml zeitgeist

Lizenz

Creative Commons License - Some Rights Reserved