# Boolean question. File here but not there

**URL:** <https://www.ruby-forum.com/t/boolean-question-file-here-but-not-there/110234>\
**Category:** Ruby\
**Created:** [July 24, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/boolean-question-file-here-but-not-there/110234 "2007-07-24T15:09:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 24, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/boolean-question-file-here-but-not-there/110234/1 "2007-07-24T15:09:26Z")

</div>

Hi,  
I’m tryiing to determine that, for every TIFF file I have, is there a  
corresponding PDF file, and, if there isn’t, then, notate that in the  
log. If there is a PDF file, then, don’t log it. So, the resulting log  
should show me a bunch of PDF files that I’ll need to create, so that I  
have a PDF for every TIFF I have. This script below seems to be listing  
every single PDF file as not being there, even though the file’s there.  
Whew. Obviously, I must have something wrong with my Boolean logic. . .  
.

Thanks,  
Peter

…  
list.each do |tifdir|  
pdfdir = tifdir  
Dir.chdir(“L:/tiff/cdtiff/#{tifdir}”)  
Dir.glob("\*.tif").each do |tiffile|  
pdffile = File.basename(tiffile, “.tif”) + “.pdf”  
tifthere = File.exists?(“L:/tiff/cdtiff/#{tifdir}/#{tiffile}”)  
pdfnotthere = !File.exists?(“L:/pdf/single/#{pdfdir}/#{pdffile}”)  
if tifthere && pdfnotthere  
File.open(“E:/logs/pdfnotthere.txt”, “a”) { |f| f.print  
“#{pdfdir}/#{pdffile} not there\n” }  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/boolean-question-file-here-but-not-there/110234/2 "2007-07-26T00:45:50Z")

</div>

On Jul 24, 2007, at 8:09 AM, Peter B. wrote:

> ## Whew. Obviously, I must have something wrong with my Boolean Dir.chdir(“L:/tiff/cdtiff/#{tifdir}”)
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Try using two Dir.globs  
one for an array of tiffs and one for an array of pdfs  
then use Array#include? to compare one array’s elements to the other  
array by basename only.  
create an array of matches or an array of non matches or both  
use Array.collect to create the new array of pairs.  
While you do that delete them from the previous arrays.  
Output the 3 arrays and you have your pairs and non pairs.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 26, 2007, 12:45am UTC](https://www.ruby-forum.com/t/boolean-question-file-here-but-not-there/110234/3 "2007-07-26T00:45:57Z")

</div>

Peter B. wrote:

> Thanks,

What is this line for? The only reason that tiffile has a value is that  
the Dir.glob("\*.tif") returned a filename proving the existence of the  
tif file, Dir.glob() is not going to return the names of non existent  
files or are you expecting the file to disappear? If so you have other  
problems.

Also if pdfdir is a copy of tifdir you should probably just find a  
better name for the variable and simplify some more. There is quite a  
bit of superfluous code here and it will only serve to confuse you at a  
later date when you come back to it and ask yourself “Why did I do  
that?”

Try this simple rewrite:

list.each do |dir|  
Dir.chdir(“L:/tiff/cdtiff/#{dir}”)  
Dir.glob("\*.tif").each do |tiffile|  
pdffile = File.basename(tiffile, “.tif”) + “.pdf”  
pdfthere = File.exists?(“L:/pdf/single/#{dir}/#{pdffile}”)  
if ! pdfthere  
File.open(“E:/logs/pdfnotthere.txt”, “a”) { |f| f.print  
“#{dir}/#{pdffile} not there\n” }  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 26, 2007, 2:21pm UTC](https://www.ruby-forum.com/t/boolean-question-file-here-but-not-there/110234/4 "2007-07-26T14:21:29Z")

</div>

Peter H. wrote:

> Peter B. wrote:
> 
> > Thanks,
> 
> What is this line for? The only reason that tiffile has a value is that  
> the Dir.glob("\*.tif") returned a filename proving the existence of the  
> tif file, Dir.glob() is not going to return the names of non existent  
> files or are you expecting the file to disappear? If so you have other  
> problems.
> 
> Also if pdfdir is a copy of tifdir you should probably just find a  
> better name for the variable and simplify some more. There is quite a  
> bit of superfluous code here and it will only serve to confuse you at a  
> later date when you come back to it and ask yourself “Why did I do  
> that?”
> 
> Try this simple rewrite:
> 
> list.each do |dir|  
> Dir.chdir(“L:/tiff/cdtiff/#{dir}”)  
> Dir.glob("\*.tif").each do |tiffile|  
> pdffile = File.basename(tiffile, “.tif”) + “.pdf”  
> pdfthere = File.exists?(“L:/pdf/single/#{dir}/#{pdffile}”)  
> if ! pdfthere  
> File.open(“E:/logs/pdfnotthere.txt”, “a”) { |f| f.print  
> “#{dir}/#{pdffile} not there\n” }  
> end  
> end  
> end

I don’t know exactly what line you’re referring to, Peter. But, your  
suggestions are good ones. I like the “pdfthere” with the ! coming  
later, instead of my “pdfnotthere.” Thanks a lot for your help. I’m  
playing with a combo of both yours and John’s suggestions above.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [July 26, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/boolean-question-file-here-but-not-there/110234/5 "2007-07-26T14:23:08Z")

</div>

John J. wrote:

> On Jul 24, 2007, at 8:09 AM, Peter B. wrote:
> 
> > ## Whew. Obviously, I must have something wrong with my Boolean Dir.chdir(“L:/tiff/cdtiff/#{tifdir}”)
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Try using two Dir.globs  
> one for an array of tiffs and one for an array of pdfs  
> then use Array#include? to compare one array’s elements to the other  
> array by basename only.  
> create an array of matches or an array of non matches or both  
> use Array.collect to create the new array of pairs.  
> While you do that delete them from the previous arrays.  
> Output the 3 arrays and you have your pairs and non pairs.

Thanks a lot. Yes, your suggestion in an intelligent one. My headspace  
expands every day with Ruby! Thanks again.

-Peter
