Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Untested code coming...
#!/usr/bin/perl my $pattern = "PGP"; &renameFiles('/home/user/perl/testincoming'); sub renameFiles { # Pass directory name as parameter my $Dir = shift; opendir(DIR, $Dir) || die "Can't opendir $Dir: $!"; my @Files = grep { -f "$Dir/$_" } readdir(DIR); rewinddir(DIR); my @Dirs = grep { /^[^.].*/ && -d "$Dir/$_" && ! -l "$Dir/$_"} rea +ddir(DIR); closedir DIR; foreach $file (@Files) { open(FILE,"<$Dir/$file") || die "Can't open $file: $!"; my $doRename=0; READFILE: while (<FILE>) { if (/\Q$pattern\E/) { $doRename=1; last READFILE; } } close(FILE); if ($doRename) { my $newname=$file; $newname=~s/^./o/; rename ("$Dir/$file", "$Dir/$newname"); } } # Call self for each subdir foreach $SubDir (@Dirs) { &renameFiles(join("/",$Dir,$SubDir)); } };
Should rename all files containing a PGP line in the whole subdirectory tree. This is just a quick modify of something I already had, so it may contain bugs, etc...
I'm not responsible for what it does to your system, no warraty, etc, etc. You know the standard phrases.

UPDATE:As merlyn humbly suggests in his post, I now check for symlinks with && ! -l "$Dir/$_". The code is still untested from my part.

/brother t0mas

In reply to RE: RE: Odd file rename by t0mas
in thread Odd file rename by Anonymous Monk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2021-05-14 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?