0

I'm having trouble with this code (again). I'm trying to get Ruby to check if tree is equal to any of the items in $do_not_scan and all I'm getting is a "cannot convert Array into String" error. Any way to fix such a thing?

My code:

#filesniffer by Touka, ©2015
$filecount = 0
$trees = Dir.entries("C:\\")
$do_not_scan = ["bootmgr", "BOOTNXT", "USER", ".", "Documents and Settings", "PerfLogs", "System Recovery", "System Volume Information", "$RECYCLE.BIN"]
def scan
    $trees.each do |tree|
        unless tree.include?($do_not_scan[0...8])
            puts "scanning #{tree}"
            entries = Dir.entries("c:\\#{tree}")
            entries.each do |filename|
                if filename == "**\*.dll"
                    puts "Found #{filename} in #{tree}"
                end
            end
        end
    end
end
def scan_loop
    $trees.each do |tree|
        scan
        unless tree.include?($do_not_scan[0...8])
            subtrees = Dir.entries("#{tree}")
            subtrees.each do |tree|
                scan
                scan_loop
            end
        end
    end
end
scan_loop
sleep
6
  • I would use a Regexp instead. It would help me avoid the issue of case-sensitive comparison of folder names. Also, I would avoid recursion, it uses more resources and there is no need for it in this case. Commented May 4, 2015 at 13:48
  • you are also calling scan without updating $trees. You could get rid of the global variable $trees all together by passing it as an argument to the #scan and #scan_loop methods Commented May 4, 2015 at 14:21
  • 1
    side note - please stop using $global variables. They just aren't used in ruby. Normal variables do not need a $ Commented May 4, 2015 at 14:40
  • "They just aren't used in ruby." Yes they are, just not very often in normal code and they're a hint that someone doesn't know what they're doing if overused/abused, as in this case. In this case they're an indicator that the OP doesn't understand variable scoping. Commented May 4, 2015 at 18:00
  • Why try to reinvent a very well tested and prewritten wheel? Ruby comes with the Find class that will do a recursive descent of a directory hierarchy. Use it instead of trying to do it yourself. Or investigate using Dir.glob('**/*.dll') as it's an alternate path. Commented May 4, 2015 at 18:03

1 Answer 1

1

It looks like the following have to change in the scan and scan_loop methods:

$do_not_scan[0...8].include?(tree) 

In place off:

tree.include?($do_not_scan[0...8])
Sign up to request clarification or add additional context in comments.

1 Comment

it's a step in the right direction but it's trying to scan files now. (need to head to class so I'll check back later)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.