I often use ruby to run through one set of files and then look at
another and if they match then I delete a file, append them.. whatever.
I'm relatively new to ruby so I always want to improve the efficiency of
my code. Here is working code that I used to append two files in
seperate folders and place them in a finalpost folder.
Temphome.each do |filetemp|
Temprep.each do |filerep|
filerep = File.basename(filerep)
filetemp = File.basename(filetemp)
if filerep == filetemp
cmd = "P:\\project\\bin\\pdftk.exe
P:\\project\\post\\Temphome\\#{filetemp}
P:\\project\\post\\Temprep\\#{filetemp} cat output
P:\\project\\post\\Finaltal\\#{filetemp}"
puts "Appending #{filetemp}"
`#{cmd}`
else next
end
end
end
puts "Finished Appending, the new files are located in
P:\\project\\post\\finalpost"
I don't think this is likely to be faster, but it is a slightly different
approach:
1 #!/usr/bin/env ruby
2 # encoding: utf-8
3
4 require 'shellwords'
5
6 HOME = File.join %w{ P: project post Temphome }
7 PREP = File.join %w{ P: project post Tempprep }
8 DEST = File.join %w{ P: project post Finaltal }
9 TOOL = File.join %w{ P: project bin pdftk.exe }
10
11 Dir[File.join(HOME, '*.pdf')].each do |temp|
12 name = File.basename(temp)
13 prep = File.join(PREP, name)
14 if File.exists?(prep)
15 command = [TOOL, temp, prep, 'cat', 'output', File.join(DEST,
name)].shelljoin
16 puts "Appending #{name}"
17 `#{command}`
18 end
19 end
Instead of looping twice (for n * m iterations) this just iterates once
over the first collection of files (n iterations), a theoretical "big-O"
improvement. However, I suspect that *if* there's any performance gain from
this, that it's easily overshadowed by the overhead of running "pdftk" to
concat PDFs.
However, though there's likely no performance improvements, this does
demonstrate the cool "shellwords" library (part of the standard library: Index of Classes & Methods in shellwords: Ruby Standard Library Documentation (Ruby 1.9.2) ).
It is usually much safer to use shellwords to construct commands you plan
to execute as it'll take care of quoting (and escaping characters) in
filenames/commands in case you have spaces in a filename or command token
(or other characters that need quoting or escaping). (NOTE: I'm *assuming*
shellwords is fully customized to work appropriately for the windows
command shell...)
I've also used the "cross-platform" technique of "File.join(...)" which
uses the correct path separator for your platform (backslashes for
windows). However, it appears from your working code that ruby must
internally transform forward-slashes to backslashes in paths on windows (in
at least some places) but it's probably still a good idea to use
"File.join(...)".
···
On Fri, Apr 27, 2012 at 12:04 PM, Christopher D. <lists@ruby-forum.com>wrote:
I often use ruby to run through one set of files and then look at
another and if they match then I delete a file, append them.. whatever.
I'm relatively new to ruby so I always want to improve the efficiency of
my code. Here is working code that I used to append two files in
seperate folders and place them in a finalpost folder.
Temphome.each do |filetemp|
Temprep.each do |filerep|
filerep = File.basename(filerep)
filetemp = File.basename(filetemp)
if filerep == filetemp
cmd = "P:\\project\\bin\\pdftk.exe
P:\\project\\post\\Temphome\\#{filetemp}
P:\\project\\post\\Temprep\\#{filetemp} cat output
P:\\project\\post\\Finaltal\\#{filetemp}"
puts "Appending #{filetemp}"
`#{cmd}`
else next
end
end
end
puts "Finished Appending, the new files are located in
P:\\project\\post\\finalpost"