2

Here's the code :

Dir.foreach('C:\\Documents and Settings\\') { |entry|
    if File.directory?( entry )
        puts entry
    end
}

Dir.foreach('\\\\10.80.14.20\\transfer') { |entry|
    if File.directory?( entry )
        puts entry
    end
}

Both C:\\Documents and Settings and \\10.80.14.20 contain directories. But it only lists the folders under C:\Documents and Settings. While the folders under \\\\10.80.14.20 isn't listed. It seems File.directory?( entry ) cannot work under shared folders. Am I right? If so, is there any other methods to identify folders when under shared folders?

4

2 回答 2

1

map \\10.80.14.20\transfer as a separate drive say F: on your machine where you're trying to run this program and then modify your code to

Dir.foreach('f:\\') { |entry|
    if File.directory?("f:\\#{entry}")
        puts entry
    end
}

However if you intend to run such code as a windows service, then you'll have to map the drive from within your code because windows services don't recognize externally mapped drives.

require 'win32ole'
def map_my_drive
    net = WIN32OLE.new('WScript.Network')
    user_name = "<your_domain>\\<your_user>"
    password = "<your_password>"
    net.MapNetworkDrive( 'f:', "\\\\10.80.14.20\\transfer", nil,  user_name, password )
end 

map_my_drive

Dir.foreach('f:\\') { |entry|
    if File.directory?("f:\\#{entry}")
        puts entry
    end
}
于 2012-10-06T10:19:48.453 回答
0

If you can mount it to a local drive I suppose it will work with the standard Dir.foreach because the operating system will hide it for you.

If you cannot, then you could use the Ruby/SMB library, I'm not sure it will work nowadays, but you could try other as well (net-smb).

Example code (not tested):

require 'smb'

SMB::Dir.foreach('smb://10.80.14.20/transfer') do |entry|
  if entry.dir?
    puts entry
  end
end
于 2012-10-06T10:12:19.433 回答