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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to split the content of a directory

Status
Not open for further replies.

wysiwyg

Programmer
May 8, 2000
6
CA
I'm new to perl and I have a simple question . <br>How do I split he content of a directory<br><br>here is the content of the directory:<br><br>mai 11 10:25 .<br>oct 10 1997&nbsp;&nbsp;..<br>mai 05 05:57 index<br>mai 11 11:36 test.pl<br><br>I want each line to be an element of an array<br>and after that I want to remove the first and the second element.<br><br>tks<br>Annie<br>;o)
 
Use <FONT FACE=monospace>opendir</font> and <FONT FACE=monospace>readdir</font>.&nbsp;&nbsp;Here's a little snippet of code to illustrate:<br><FONT FACE=monospace><br>opendir(MYDIR, &quot;.&quot;);&nbsp;&nbsp;&nbsp;&nbsp;#Open a new directory handle.<br><br># Read each file name from the directory handle.&nbsp;&nbsp;When<br># we run out of file names, the while will fail and<br># we'll move on.<br>while ( $Current_File = readdir(MYDIR) ) {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;# Ignore &quot;.&quot; and &quot;..&quot; (Optional)<br>&nbsp;&nbsp;&nbsp;&nbsp;if ( $Current_File =~ /\.¦\.\./ ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<i>Do your file processing here</i><br>}<br></font><br><br>Note:&nbsp;&nbsp;I've just tapped this in from memory, and at my age that can be a bit dodgy ;^)&nbsp;&nbsp;You might need to sort out any missing braces, or the regexp might be a little suspect.<br><br>Hope it puts you on the right track though.&nbsp;&nbsp;Additionally, you can do a <FONT FACE=monospace>perldoc -f opendir</font> or <FONT FACE=monospace>perldoc -f readdir</font> to see the online docs.<br><br>Hope this helps.
 
Try:<br><br>opendir DIR,&quot;.&quot;;<br>@filelist=grep { /^./ } readdir DIR;<br>closedir DIR;<br><br>then @filelist contains every element in the dir that DOESN'T start with &quot;.&quot; (assuming that's what you want to get rid of)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top