Skip to content

Instantly share code, notes, and snippets.

@inz
Last active August 29, 2015 14:09

Revisions

  1. inz renamed this gist Nov 14, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. inz created this gist Nov 14, 2014.
    31 changes: 31 additions & 0 deletions rename_files
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/usr/bin/env ruby
    require 'fileutils'

    MAPPING='dslab14-account-mapping.txt'
    FOLDER=ARGV.first || 'DSLab-Harvest-1237'
    TARGET=File.join('processed')
    Dir.mkdir(TARGET) rescue nil

    # Map users to immatriculation numbers.
    users = {}
    File.open(MAPPING, 'r') do |file|
    file.each do |line|
    parts = line.split
    next if parts.length < 5
    user = parts[0]
    immatrnr = parts[4]
    users[user] = immatrnr
    end
    end

    Dir.new(FOLDER).each do |entry|
    user=entry.split('_').first
    next unless users.include?(user)
    base_path = File.join(FOLDER, entry)
    submission_filename = Dir.glob(File.join(base_path, "*.zip")).first
    next unless submission_filename

    # Rename to <immatr.nr>.zip
    puts "Copying from: #{submission_filename} to: #{File.join(TARGET, "#{users[user]}.zip")}"
    FileUtils.cp submission_filename, File.join(TARGET, "#{users[user]}.zip")
    end