Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

RE: Odd file rename

by Anonymous Monk
on Jun 14, 2000 at 17:25 UTC ( [id://18090]=note: print w/replies, xml ) Need Help??


in reply to Odd file rename

Names changed to protect the guilty - can I do this while the file is open, or do I need to close it first? Please excuse my hideous indentation, it's a work in progress.
#!/usr/bin/perl @incoming = ( `ls /home/user/perl/testincoming` ); foreach $directory (@incoming) { chomp($directory); @files = ( `ls /home/user/perl/testincoming/$directory` ); foreach $file (@files) { $pattern = "PGP"; open(FILE,"/home/user/perl/testincoming/$directory/$file") or +die "Could n't open $file"; while (<FILE>) { if (/\Q$pattern\E/) { my $newname=$file; $newname=~s/^./o/; rename ($file, $newname); } close FILE; } } }

Replies are listed 'Best First'.
RE: RE: Odd file rename
by Corion (Patriarch) on Jun 14, 2000 at 17:28 UTC

    Doing stuff like moving open files is mostly asking for trouble. Under UNIX systems, this shouldn't be a problem (but who can tell for all UNIX systems ?), under NT this will work and under Windows 9x you will get a sharing violation.

    The best thing is to do the rename afterwards, and it's not really hard to do this either.

RE: RE: Odd file rename
by t0mas (Priest) on Jun 14, 2000 at 18:02 UTC
    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
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://18090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found