FileTest.directory?( x ) weird behavior

I have a bit of code I'm working on where I need to seperate the
directories from the files. Currently, it's processing the first
three properly and skipping the rest.

I'm running this on windows.

Here is the code:

def GetFolders
  rtn = ""
  Dir.foreach( @userFilePath ) do |x|
  if FileTest.directory?( x )
      rtn += "<Folder name=\"" + x + "\" />" unless x <= ".."
  end
   end
   return rtn
end
  
def GetFiles
  rtn = ""
  Dir.foreach( @userFilePath ) do |x|
    rtn += "<File name=\"" + x + "\" size=\"000\" />" unless
FileTest.directory?( x )
  end
  return rtn
end

It gives the output of:

<Folders>
<Folder name="test"/>
</Folders>
<Files>
<File name="404.html" size="000"/>
<File name="500.html" size="000"/>
<File name="asdf" size="000"/>
<File name="asdnt.txt" size="000"/>
<File name="silk" size="000"/>
<File name="thingy.html" size="000"/>
</Files>

The actual directory contains:

<DIR> .
<DIR> ..
<FILE> 404.html
<FILE> 500.html
<DIR> asdf
<FILE> asdnt.txt
<DIR> silk
<DIR> test
<FILE> thingy.html
4 File(s) 567 bytes
5 Dir(s) 22,350,041,088 bytes free

I'm still learning ruby, so I've probably written something incorrectly...

Josh

You're testing relative file names only while you want absolute file names:

def GetFolders
  rtn = ""
  Dir.foreach( @userFilePath ) do |x|
     if FileTest.directory?( File.join( @userFilePath, x ) )
       rtn << "<Folder name=\"" + x + "\" />" unless x <= ".." end
  end
  return rtn
end

Kind regards

    robert

···

Josh Charles <josh.charles@gmail.com> wrote:

I have a bit of code I'm working on where I need to seperate the
directories from the files. Currently, it's processing the first
three properly and skipping the rest.

I'm running this on windows.

Here is the code:

def GetFolders
  rtn = ""
  Dir.foreach( @userFilePath ) do |x|
if FileTest.directory?( x )
    rtn += "<Folder name=\"" + x + "\" />" unless x <= ".."
end
   end
   return rtn
end

def GetFiles
  rtn = ""
  Dir.foreach( @userFilePath ) do |x|
    rtn += "<File name=\"" + x + "\" size=\"000\" />" unless
FileTest.directory?( x )
  end
  return rtn
end

It gives the output of:

<Folders>
<Folder name="test"/>
</Folders>
<Files>
<File name="404.html" size="000"/>
<File name="500.html" size="000"/>
<File name="asdf" size="000"/>
<File name="asdnt.txt" size="000"/>
<File name="silk" size="000"/>
<File name="thingy.html" size="000"/>
</Files>

The actual directory contains:

<DIR> .
<DIR> ..
<FILE> 404.html
<FILE> 500.html
<DIR> asdf
<FILE> asdnt.txt
<DIR> silk
<DIR> test
<FILE> thingy.html
4 File(s) 567 bytes
5 Dir(s) 22,350,041,088 bytes free

I'm still learning ruby, so I've probably written something
incorrectly...

The reason for the behaviour you're getting is that those filenames don't
exist in the current working directory, only in the @userFilePath
directory - that's why directory? is returning false.

You're testing relative file names only while you want absolute file
names:

def GetFolders
rtn = ""
Dir.foreach( @userFilePath ) do |x|
    if FileTest.directory?( File.join( @userFilePath, x ) )
      rtn << "<Folder name=\"" + x + "\" />" unless x <= ".." end
end
return rtn
end

Or you can change directory:

#...
Dir.chdir( @userFilePath ) do
  Dir.foreach (@userFilePath ) do |x|
#...
  end
end
#...

Cheers,
Dave

Excellent. Thanks for the help. I should have known better...

Josh

···

On 9/7/05, Robert Klemme <bob.news@gmx.net> wrote:

Josh Charles <josh.charles@gmail.com> wrote:
> I have a bit of code I'm working on where I need to seperate the
> directories from the files. Currently, it's processing the first
> three properly and skipping the rest.
>
> I'm running this on windows.
>
> Here is the code:
>
> def GetFolders
> rtn = ""
> Dir.foreach( @userFilePath ) do |x|
> if FileTest.directory?( x )
> rtn += "<Folder name=\"" + x + "\" />" unless x <= ".."
> end
> end
> return rtn
> end
>
> def GetFiles
> rtn = ""
> Dir.foreach( @userFilePath ) do |x|
> rtn += "<File name=\"" + x + "\" size=\"000\" />" unless
> FileTest.directory?( x )
> end
> return rtn
> end
>
> It gives the output of:
>
> <Folders>
> <Folder name="test"/>
> </Folders>
> <Files>
> <File name="404.html" size="000"/>
> <File name="500.html" size="000"/>
> <File name="asdf" size="000"/>
> <File name="asdnt.txt" size="000"/>
> <File name="silk" size="000"/>
> <File name="thingy.html" size="000"/>
> </Files>
>
> The actual directory contains:
>
> <DIR> .
> <DIR> ..
> <FILE> 404.html
> <FILE> 500.html
> <DIR> asdf
> <FILE> asdnt.txt
> <DIR> silk
> <DIR> test
> <FILE> thingy.html
> 4 File(s) 567 bytes
> 5 Dir(s) 22,350,041,088 bytes free
>
> I'm still learning ruby, so I've probably written something
> incorrectly...

You're testing relative file names only while you want absolute file names:

def GetFolders
  rtn = ""
  Dir.foreach( @userFilePath ) do |x|
     if FileTest.directory?( File.join( @userFilePath, x ) )
       rtn << "<Folder name=\"" + x + "\" />" unless x <= ".."
     end
  end
  return rtn
end

Kind regards

    robert