Updated version:
This one takes as specification:
- Each statement on one line
- Records are separated by a
\t
character.
That makes it much easier:
require 'fileutils'
File.open("input.txt", 'r').each_line do |l|
next if l.strip.empty?
f = l.split("\t")
dir = File.dirname f[0]
if File.exists? f[0]
p "Moving #{f[0]} to #{dir}/#{f[1].strip}"
# FileUtils.mv f[0], "#{dir}/#{f[1].strip}"
else
p "#{f[0]} does not exist."
end
end
Save this as a file and call it with ruby filename.rb
. It'll read from input.txt
in the same directory and just move the files.
Uncomment the # FileUtils…
line for it to actually move something. This should tell you if the original file can't be found.
Your original input specification was:
- Each statement on one line
- Records are not terminated or separated except for their extension
- Records can contain spaces, and delimiter can be multiple spaces ("at least 1 space")
One therefore has to fall back to matching the first file extension, then interpreting this as the first argument. Then, we need to remove this part from the original line, remove beginning and trailing whitespace, and construct the mv
command.
Additional assumptions:
- All extensions have only three characters, not more, not less
- The file names themselves don't contain a dot (otherwise this breaks)
Since you're on OS X, let's just use Ruby:
require 'fileutils'
File.open("input.txt", 'r').each_line do |l|
next if l.strip.empty?
old_file = l[0, l.index(/\.[a-z0-9]{3}/i) + 4]
dir = File.dirname old_file
new_filename = l.sub(old_file, '').lstrip.chomp
p "Moving #{old_file} to #{dir}/#{new_filename}"
# FileUtils.mv old_file, "#{dir}/#{new_filename}"
end
\t
between the entries, or something else to split the entries at?