Remote Exploit Forums

Go Back   Remote Exploit Forums > International Communities > BackTrack German Community > Tutorials und Howtos


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2009, 12:37 PM
Fancy's Avatar
Moderator
 
Join Date: Dec 2007
Posts: 39
Default Kleines Ruby Skript - Files mit Sonderzeichen umbenennen

Hallo zusammen,

ich habe mir gerade ein kleines Ruby-Skript gebastelt, welches Files (und Unterverzeichnisse) im lokalen Arbeitsverzeichnis umbenennt, die Sonderzeichen (vor allem Leerzeichen) enthalten. Vielleicht kann's ja jemand gebrauchen:

Code:
#!/usr/bin/ruby

basedir = '.'
files = Dir.glob("*")  # erzeuge file liste


puts "\nChecke lokales Verzeichnis....\n\n"

files.each do |file|

  name = File.basename(file, ".*")
  ext = File.extname(file)

# Hash mit den zu ersetzenden Sonderzeichen
  ersetzungen = { /;/ => "-",
                   /\s/ => "_",
                   /\'\`/ => "=",
                   /\&/ => "_und_",
                   /\$/ => "_dollar_",
                   /%/ => "_prozent_",
                   /§/ => "_parag_",
                   /[\(\)\[\]<>]/ => ""
  }  

  tempfile = file.dup		# erstelle Duplikat von Variable "file"

  ersetzungen.each do |orig, fix|
    name.gsub!(orig,fix)		# dies aendert die Variable "file" !!!
  end

  # File(s) umbenennen
  File.rename(tempfile, name + ext)

 # Kleiner Report: was wurde getan?
  if tempfile != name + ext 
	puts "Umbenannt: #{tempfile} ---> #{name + ext} \n"
  end
end

puts "\n.......fertig.\n\n"

PS: Das Skript benennt die Files rigoros um bzw. überschreibt eventuell schon vorher umbenannte Files!!

Beispiel:
Code:
Honolulu temp # ll
total 20
 0 drwxr-xr-x   2 root root   112 2009-06-25 13:29 .
20 drwxr-xr-x 108 root root 20736 2009-06-25 13:29 ..
 0 -rw-r--r--   1 root root     0 2009-06-25 13:29 abc%efg.doc
 0 -rw-r--r--   1 root root     0 2009-06-25 13:29 hallo du.txt
Honolulu temp # fixmyFiles.rb 

Checke lokales Verzeichnis....

Umbenannt: hallo du.txt ---> hallo_du.txt 
Umbenannt: abc%efg.doc ---> abc_percent_efg.doc 

.......fertig.

Honolulu temp # ll
total 20
 0 drwxr-xr-x   2 root root   120 2009-06-25 13:30 .
20 drwxr-xr-x 108 root root 20736 2009-06-25 13:29 ..
 0 -rw-r--r--   1 root root     0 2009-06-25 13:29 abc_percent_efg.doc
 0 -rw-r--r--   1 root root     0 2009-06-25 13:29 hallo_du.txt
__________________
"Si tacuisses, philosophus mansisses"

Last edited by Fancy; 06-25-2009 at 01:13 PM.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 06:52 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2