FileTest.directory?( x ) weird behavior

J

Josh Charles

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 =3D ""
Dir.foreach( @userFilePath ) do |x|
=09if FileTest.directory?( x )=20
=09 rtn +=3D "<Folder name=3D\"" + x + "\" />" unless x <=3D ".."
=09end
end
return rtn
end
=09
def GetFiles
rtn =3D ""
Dir.foreach( @userFilePath ) do |x|
rtn +=3D "<File name=3D\"" + x + "\" size=3D\"000\" />" unless
FileTest.directory?( x )
end
return rtn
end

It gives the output of:

<Folders>
<Folder name=3D"test"/>
</Folders>
<Files>
<File name=3D"404.html" size=3D"000"/>
<File name=3D"500.html" size=3D"000"/>
<File name=3D"asdf" size=3D"000"/>
<File name=3D"asdnt.txt" size=3D"000"/>
<File name=3D"silk" size=3D"000"/>
<File name=3D"thingy.html" size=3D"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
 
R

Robert Klemme

Josh Charles said:
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
 
D

Dave Burt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,964
Members
47,511
Latest member
svareza

Latest Threads

Top