Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with directory tree's. 1

Status
Not open for further replies.

vcherubini

Programmer
May 29, 2000
527
US
Hello, I have a problem with directory tree calling in Perl.<br><br>The code that I have is this:<br><br><FONT FACE=monospace><br><br>opendir(DIRHANDLE, &quot;C:\Cd\Data\&quot;) or die &quot;Cant open /Cd/Data/: $!&quot;;<br>while ( defined ($filename = readdir(DIRHANDLE)) ) {<br>print &quot;$filname\n&quot;;<br>}<br>closedir(DIRHANDLE);<br><br></font><br>which should print out a file in the directory that I am accessing, except it doesn't. <br><br>Any help on how to open a directory, put all of the files in the directory into an array, and print each member of the array with a foreach loop? (I know how to do the last one)<br><br>Any help would be appreciated.<br><br>Thanks a lot.<br><br>-Vic
 
Try this:<br><FONT FACE=monospace><br>opendir(DIRHANDLE, &quot;C:\Cd\Data\&quot;) or die &quot;Cant open /Cd/Data/: $!&quot;;<br><br># Get a list of files, excluding files where their name<br># begins with &quot;.&quot;, and put them into the array @Files.<br>@Files = grep(!/^\./, readdir(DIRHANDLE));<br><br>closedir(DIRHANDLE);<br><br>foreach $file (@Files) {<br>&nbsp;&nbsp;&nbsp;&nbsp;print $file, &quot;\n&quot;;<br>}<br></font> <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
AndyBo:<br><br>Thanks for the help. I copied the script into my text editor, saved it, and ran it, and it still didn't work. <br><br>I have looked in both the Perl Cookbook and Learning Perl on Win32 Systems. Both of them have the same way of doing as you and I, so it must be because I am using an older version of Perl or something like that.<br><br>Thanks for the help though.<br><br>-Vic
 
Vic, couple of questions:<br><br>What happens when you run it?<br><br>What version of Perl are you running?<br><br>What platform are you running on? (Win98, Unix, etc) <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Sorry I was not clear enough. <br><br>I am, sadly, using Perl 5.003, so I should probably think about updating that.<br><br>When I run it, I get a whole bunch of white space where the file names in the directory should be. It looks like its adding a whole bunch of \n's.<br><br>And finally, I am running on Win2k, but I don't think that should have too much a difference.<br><br>Thanks for all the help.<br>
 
Hey try this instead, it looks the same but this works on my machine for sure.<br><br>opendir (DIR, &quot;C:\Cd\Data\&quot;) or die &quot;Can't open because: $!\n&quot;; <br>@allfiles = sort grep(!/^*\./, readdir(DIR));<br>closedir (DIR);<br><br>foreach $file(@allfiles) {<br> print &quot;$file\n&quot;;<br> }<br>
 
Thank you o so much everyone, especially Damann.<br><br><br>It was not working because I did something very stupid. Perl didn't like the C:\Cd\data\ part, so I just had to change it to \Cd\Data\ and it worked fine. <br><br><br>Thanks for all the help.<br><br><br>-Vic
 
Hmmm, if you use readdir like this:<br><FONT FACE=monospace><b><br>opendir (DIR, &quot;C:\\work&quot;) ¦¦ die &quot;Can't open because: $!\n&quot;; <br><br>foreach $file(readdir(DIR)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$file\n&quot;;<br>}<br>closedir (DIR);<br></font></b><br>then it would cope with very large directories... <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top