help selecting and displaying specific files after reading directory: 
Author Message
 help selecting and displaying specific files after reading directory:

using xp-pro and perl 5.6.1.
i'm opening and reading a directory with the hopes of selecting specific
files, those with *.tmp extension.  then i plan on using 'unlink' so as to
delete these files:

$currentDir = ".";

closedir CURRENT_PB_DIR;

here's my question, although the above works, i cannot seem to display such
files one per line, even though i'm using \n within the print statement.

is there a more efficient way of doing this with perl?

i then plan to do something like:

i have not tested that part yet though.

thanks



Sat, 05 Nov 2005 20:27:03 GMT  
 help selecting and displaying specific files after reading directory:
Also sprach Scaramouche:

Quote:
> using xp-pro and perl 5.6.1.
> i'm opening and reading a directory with the hopes of selecting specific
> files, those with *.tmp extension.  then i plan on using 'unlink' so as to
> delete these files:

> $currentDir = ".";
> opendir(CURRENT_DIR, $currentDir) || die "can't opendir $currentDir: \n$!";


> closedir CURRENT_PB_DIR;

> here's my question, although the above works, i cannot seem to display such
> files one per line, even though i'm using \n within the print statement.


That's because your print statement is equivalent to

    print "The following *.tmp files were found: ",

$" is by default a space (see 'perldoc perlvar / $LIST_SEPARATOR'). To
print them linewise, join with "\n":


Quote:
> is there a more efficient way of doing this with perl?

> i then plan to do something like:


This is indeed possible:

    unlink <$currentDir/*.tmp>;

In this case, <> is the glob operator. Could also be written as

    unlink glob "$currentDir/*.tmp";

Tassilo
--
{
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval



Sat, 05 Nov 2005 20:35:53 GMT  
 help selecting and displaying specific files after reading directory:
thank you,
for taking the time to read my post, and for answering my questions.



Quote:
> Also sprach Scaramouche:

> > using xp-pro and perl 5.6.1.
> > i'm opening and reading a directory with the hopes of selecting specific
> > files, those with *.tmp extension.  then i plan on using 'unlink' so as
to
> > delete these files:

> > $currentDir = ".";
> > opendir(CURRENT_DIR, $currentDir) || die "can't opendir $currentDir:
\n$!";


> > closedir CURRENT_PB_DIR;

> > here's my question, although the above works, i cannot seem to display
such
> > files one per line, even though i'm using \n within the print statement.

> That's because your print statement is equivalent to

>     print "The following *.tmp files were found: ",

> $" is by default a space (see 'perldoc perlvar / $LIST_SEPARATOR'). To
> print them linewise, join with "\n":


> > is there a more efficient way of doing this with perl?

> > i then plan to do something like:

> This is indeed possible:

>     unlink <$currentDir/*.tmp>;

> In this case, <> is the glob operator. Could also be written as

>     unlink glob "$currentDir/*.tmp";

> Tassilo
> --

{
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval

- Show quoted text -



Sat, 05 Nov 2005 20:58:08 GMT  
 help selecting and displaying specific files after reading directory:

(snipped)

Quote:
> i'm opening and reading a directory with the hopes of selecting specific
> files, those with *.tmp extension.  then i plan on using 'unlink' so as to
> delete these files:
> $currentDir = ".";

$currentDir = "c:/apache/users/test/";

Use a full path, always! There are reasons for this. If you
install your script in an incorrect directory...

Besides, you need a full path for unlink.

Quote:
> opendir(CURRENT_DIR, $currentDir) || die "can't opendir $currentDir: \n$!";

Compare your FILE_HANDLE to your close statement.



Quote:
> closedir CURRENT_PB_DIR;

Incorrect FILEHANDLE. You are leaving your directory
open until your script exits.

An example script is beneath my signature which
may help point you in the right direction.

Purl Gurl
--
#!perl

$currentDir = "c:/apache/users/test/";


closedir CURRENT_DIR;


 {
  $" = "\n ";

  $" = " ";

  print "\nDo you want to delete files? y/n ?\n";
  $yes_no = <STDIN>;
  chomp ($yes_no);
  if ($yes_no eq "y")
   {

     {
      print "Do you want to delete $_ ? y/n ?";
      $yes_no = <STDIN>;
      chomp ($yes_no);
      if ($yes_no eq "y")
       { print "Deleting: $_\n"; unlink "$currentDir$_"; }
     }
   }
  else
   { print "Exiting"; }
 }
else
 { print "No *.tmp files found."; }



Sat, 05 Nov 2005 21:33:01 GMT  
 help selecting and displaying specific files after reading directory:
Also sprach Purl Gurl:

Quote:

> (snipped)

>> i'm opening and reading a directory with the hopes of selecting specific
>> files, those with *.tmp extension.  then i plan on using 'unlink' so as to
>> delete these files:

>> $currentDir = ".";

> $currentDir = "c:/apache/users/test/";

> Use a full path, always! There are reasons for this. If you
> install your script in an incorrect directory...

...then it will remain incorrect regardless of absolute or relative
paths.

Quote:
> Besides, you need a full path for unlink.

Oh, really? Please elaborate on that a little further. unlink() works
fine with both flavours of path-directives.

[ more than 30 lines of butt-ugly code snipped ]

Tassilo
--
{
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval



Sat, 05 Nov 2005 21:57:17 GMT  
 help selecting and displaying specific files after reading directory:


Quote:






That regular expression has two problems: no backslash before
the dot and no dollarsign at the end.  That means it would
match "This-Is-Not-A-tmp-File.txt" and delete a file that it shouldn't.
        -Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


Sun, 06 Nov 2005 09:00:34 GMT  
 help selecting and displaying specific files after reading directory:

Quote:







> That regular expression has two problems: no backslash before
> the dot and no dollarsign at the end.  That means it would
> match "This-Is-Not-A-tmp-File.txt" and delete a file that it shouldn't.

This is correct. However, not in my code example.
I have sense enough to safeguard against those
unnoticed programming errors we all make.

      print "Do you want to delete $_ ? y/n ?";
      $yes_no = <STDIN>;
      chomp ($yes_no);
      if ($yes_no eq "y")
       { print "Deleting: $_\n"; unlink "$currentDir$_"; }

Purl Gurl
--
"Men seem to have this {*filter*}ed up attitude that women are responsible for their
 behaviour as well as their own. If you want PurlGurl to move along then stop
 behaving like STUPID LITTLE BOYS."
  - Finlandia



Sun, 06 Nov 2005 13:51:59 GMT  
 help selecting and displaying specific files after reading directory:
thank you for the input.
i'm not too familiar with perl's regular expressions, are you suggesting
something along these lines:

if the above is correct, exactly what is the function of that dollar sign?

thank you.


Quote:







> That regular expression has two problems: no backslash before
> the dot and no dollarsign at the end.  That means it would
> match "This-Is-Not-A-tmp-File.txt" and delete a file that it shouldn't.
> -Joe
> --
> See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



Sun, 06 Nov 2005 13:53:35 GMT  
 help selecting and displaying specific files after reading directory:

(snipped)

Quote:

> > That regular expression has two problems: no backslash before
> > the dot and no dollarsign at the end.  That means it would
> > match "This-Is-Not-A-tmp-File.txt" and delete a file that it shouldn't.
> i'm not too familiar with perl's regular expressions, are you suggesting
> something along these lines:

> if the above is correct, exactly what is the function of that dollar sign?


For this case example the dollar sign indicates,

"end of line"

This is, nothing following "tmp" in your input data.

Match: period tmp end of line

Your code could be made significantly more efficient
with some modifications. Grep is quite often a poor
choice for working with arrays, not always but often.

This site exemplifies some alternatives:

http://www.*-*-*.com/

I would be tempted to only push those files wanted
into an array.

if (substr ($filename, -4, 4) eq ".tmp")

Purl Gurl
--
"Women get called names all the time; love, baby, honey, {*filter*}, little
 girl, little woman...why does the term 'little boy' incite such
 irrational anger in men. I find this interesting. It's not hypocrisy,
 it's irony."
  - Elaine Ashton



Sun, 06 Nov 2005 14:20:39 GMT  
 help selecting and displaying specific files after reading directory:
[This followup was posted to comp.lang.perl.misc]



Quote:
> using xp-pro and perl 5.6.1.
> i'm opening and reading a directory with the hopes of selecting specific
> files, those with *.tmp extension.  then i plan on using 'unlink' so as to
> delete these files:

> $currentDir = ".";
> opendir(CURRENT_DIR, $currentDir) || die "can't opendir $currentDir: \n$!";


> closedir CURRENT_PB_DIR;

> here's my question, although the above works, i cannot seem to display such
> files one per line, even though i'm using \n within the print statement.

> is there a more efficient way of doing this with perl?

> i then plan to do something like:

> i have not tested that part yet though.

> thanks

Note : You can use the "glob" function to do the file name matching. So
your code could look like the following :


    print "No matches for '*.tmp'\n";
Quote:
}

else {


Quote:
}

--
---------

Barry Kimelman
Winnipeg, Manitoba, Canada



Sun, 06 Nov 2005 14:31:52 GMT  
 help selecting and displaying specific files after reading directory:
On Wed, 21 May 2003 12:53:35 GMT, "Scaramouche"

Don't top-post.  It seriously annoys the regulars
and severely damages your chances of receiving
useful answers to your questions.

If you don't know what top-posting is, read:
http://jargon.watson-net.com/jargon.asp?w=top%2Dpost

You should also read the Posting Guidelines that
are posted here twice a week and may be read at:

http://mail.augustmail.com/~tadmc/clpmisc.shtml

For more information about netiquette in general, see the
"Netiquette Guidelines" at:

http://andrew2.andrew.cmu.edu/rfc/rfc1855.html

Quote:
>i'm not too familiar with perl's regular expressions,  

Then you should definitely read the excellent Perl
documentation available on your computer via:

perldoc perlre

For a quick, beginner's tutorial, see

perldoc perlreq uick

Quote:
>are you suggesting something along these lines:



No, the correct syntax is :

Quote:
>if the above is correct, exactly what is the function of that
>dollar sign?

It "anchors" the regular expression to the end
of the string.

--
Regards, Helgi Briem
helgi DOT briem AT decode DOT is



Sun, 06 Nov 2005 15:36:54 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Help reading directories and displaying contents

2. Regex searches in all files (or selected files) in a directory

3. Create a file in a specific directory

4. Reading directories, entering directories, chaning file permissions...

5. Read/write specific line of file

6. reading specific words from an Mp3 file

7. Extracting specific text from specific files in a list

8. Browsing/Selecting Multiple Files/Directories ?

9. How to select directory instead of file

10. File Directory and read File

11. Reading files in a directory and printing to one txt file

12. Displaying current date and time in specific format

 

 
Powered by phpBB® Forum Software