Hello Monks,

I am working with Windows file system from Linux and trying to replace 1000 of "find" operation with quick search in hash %filesystem, which I build only once during start of my script.

#!/usr/bin/perl -w use strict; use Data::Dumper; my $mountpoint = '/media/sdb2'; my %filesystem; $filesystem{$mountpoint} = traverse($mountpoint); sub traverse{ my %hash; my ($folder) = $_[0]; opendir DIR,$folder; my @content = grep {!/^\.{1,2}$/} readdir DIR; closedir DIR; foreach my $entry(sort @content){ if(-f $folder."/".$entry){ $hash{lc $entry} = $entry; }elsif( -d $folder."/". $entry){ $hash{$entry}= traverse($folder."/".$entry); }else{ print $folder."/".$entry." ignoring\n"; }; }; return %hash; }; print Dumper %filesystem;


1. The code still dont work, I am working on it right now, but you understand what I mean. I am trying to represent any folder as subkey of hash.
2. Because in Windows file naming is case insensitive and in Linux case sensitive, I set key to low case and leave value unchanged.
3. I found Tie::FileSystem module on CPAN, but it do much more then I need - read the content of the file in memory, but I need the file structure only.
4. Ok, say I build the %filesystem hash and now I am trying to find file /media/sdb2/folder1/DIRECTORY/file.TXT. The query can return two values: the path (case sensitive) to the file or undef if the file not exists. How to get it from my hash if case of data in hash are differ from asked ?
Example:
I launch my script and read the file system, populating %filesystem hash, so I have $filesystem{'/media/sdb2'}{'Folder1'}{'Directory'}{'File.txt'}.
Then I read some value from Windows registry, this value is folder1/directory/file.txt (the casing is incorrect, its ok in Windows, but not ok on Linux).
Is it possible to get right file location with "exists" from %filesystem hash? Example:
$hash{aaa}{bbb}{ccc}="ccC"; if(exists $hash{lc aAa}{lc bBB}{lc CCC}){ print "exists: $hash{lc aAa}{lc bBB}{lc CCC}\n"; }else{ print "not exists\n"; };
this example return "exists ccC", but how to get right case of "bbb" folder? I want to retrieve it without to read the filesystem again.
Thanks

In reply to treating the filesystem structure as hash of hashes by resistance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.