Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Last revisionBoth sides next revision
fuss:ruby [2015/01/09 05:34] – external edit 127.0.0.1fuss:ruby [2015/04/15 17:08] office
Line 2: Line 2:
  
 <code ruby> <code ruby>
-########################################################## +########################################################################### 
-## Wizardry and Steamworks (c) 2013,  GPLv3             ## +##  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      ## 
-##########################################################+###########################################################################
 def wasListReverse(list = []) def wasListReverse(list = [])
   if list.length <= 1; return list; end   if list.length <= 1; return list; end
Line 15: Line 15:
  
 <code ruby> <code ruby>
-########################################################## +########################################################################### 
-## Wizardry and Steamworks (c) 2013,  GPLv3             ## +##  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      ## 
-##########################################################+###########################################################################
 def wasDualQuicksort(a = [], b = [])  def wasDualQuicksort(a = [], b = []) 
   raise("The sorted arrays must be of equal length.") unless(a.length == b.length)   raise("The sorted arrays must be of equal length.") unless(a.length == b.length)
Line 80: Line 80:
  
 <code ruby> <code ruby>
-########################################################## +########################################################################### 
-## Wizardry and Steamworks (c) 2013,  GPLv3             ## +##  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      ## 
-##########################################################+###########################################################################
 def wasListStride(list=[], start=0, stride=0) def wasListStride(list=[], start=0, stride=0)
   if start > 0   if start > 0
Line 99: Line 99:
 </code> </code>
  
 +====== Sort Files Into Directories ======
  
 +The following program is meant to run in a directory containing unsorted files. The script creates directories ''0'' to ''9'' and ''A'' to ''Z'' and puts the files inside those directories by the first character of the file name.
 +
 +<code ruby>
 +#!/usr/bin/ruby
 +###########################################################################
 +##  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      ##
 +###########################################################################
 +# Get all the files in the current directory.
 +entries = Dir.entries(
 +    Dir.pwd
 +).select(
 +    &lambda{
 +        |x| 
 +        !File.directory?("#{x}") && 
 +            File.basename("#{$0}").casecmp(x) != 0 # omit us!
 +    }
 +)
 +# Sort files in [0-9A-Z] directories.
 +("0".."9").to_a.concat(('A'..'Z').to_a).each do |dir|
 +    begin
 +        if !Dir.exists?(dir)
 +            Dir.mkdir(dir)
 +        end
 +        entries.select(&lambda{|x| "#{x}".chars.first.casecmp(dir) == 0}).each do |file|
 +            File.rename(file, File.join(dir, file))
 +        end
 +    rescue
 +        puts "Could not move files to: " + dir
 +    end
 +end
 +</code>
 +
 +An example application is a directory containing a bunch of archives which you want to sort into directories from ''0'' to ''9'' and ''A'' to ''Z''.

fuss/ruby.txt · Last modified: 2022/04/19 08:28 by 127.0.0.1

Wizardry and Steamworks

© 2025 Wizardry and Steamworks

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.