CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Hey I want to rename a certain part of a file

 



adaykin
Novice

Jun 27, 2007, 8:33 AM

Post #1 of 13 (1222 views)
Hey I want to rename a certain part of a file Can't Post

I have a script that traverses a directory, and looks for a filename that contains a regular expression. For every filename that contains the regular expression I want to rename the file. However when I rename the file I only want to pull out a certain part of the filename to be renamed, e.g. if the filename is "ExampleFileName.txt" I would only want to rename a certain part like, replace "Name" with "String". How would I do this in perl?
------------------------------------------------------------

New Horizon Designs <-- My site, just updated the GUI to a PHP Nuke interface


KevinR
Veteran


Jun 27, 2007, 8:49 AM

Post #2 of 13 (1221 views)
Re: [adaykin] Hey I want to rename a certain part of a file [In reply to] Can't Post

some functions to look ito:

opendir()
readdir()
close()
rename()
-------------------------------------------------


adaykin
Novice

Jun 27, 2007, 9:08 AM

Post #3 of 13 (1219 views)
Re: [KevinR] Hey I want to rename a certain part of a file [In reply to] Can't Post

I already am using all of those functions in my script, but I don't see how they can help the specific part that I asked for.
------------------------------------------------------------

New Horizon Designs <-- My site, just updated the GUI to a PHP Nuke interface


KevinR
Veteran


Jun 27, 2007, 10:52 AM

Post #4 of 13 (1215 views)
Re: [adaykin] Hey I want to rename a certain part of a file [In reply to] Can't Post

Can we see the script?
-------------------------------------------------


adaykin
Novice

Jun 27, 2007, 11:13 AM

Post #5 of 13 (1212 views)
Re: [KevinR] Hey I want to rename a certain part of a file [In reply to] Can't Post

Yeah I can put in part of it, here is the part that would use the renaming. Earlier the user will put in a pattern he wants to be replaced, and the pattern he wants to replace it with. I am not sure if I can put the variable $wordToBeReplaced in my regular expression or not. Hopefully you can kind of see what I am trying to do. I was thinking if I could change the filename to a string it would be easier to append it.


Code
    while($file = readdir BIN && $file=~/*$wordToBeReplaced*/) { 
foreach $file (@NAMES) {
$file = HERE IS WHERE I WANT TO CHANGE ONLY PART OF THE FILENAME WHEN I RENAME THE FILE
# change $newname
rename($file, $newname) or
warn "Couldn't rename $file to $newname: $!\n";
}
}

------------------------------------------------------------

New Horizon Designs <-- My site, just updated the GUI to a PHP Nuke interface


(This post was edited by adaykin on Jun 27, 2007, 11:21 AM)


KevinR
Veteran


Jun 27, 2007, 12:17 PM

Post #6 of 13 (1207 views)
Re: [adaykin] Hey I want to rename a certain part of a file [In reply to] Can't Post

OK, clearly you have a problem with the logic of your code:


Code
    while($file = readdir BIN && $file=~/*$wordToBeReplaced*/) {  
foreach $file (@NAMES) {


first you assign $file the return value of the expressions on the right in the "while" loop condition, but then you assign it the value of each element of @NAMES in the "foreach" loop. What you should do is a get a list of the files that match your regexp to begin with. Assuming no other checks are necessary:


Code
opendir(BIN,"path/to/directory") or die "$!"; 
my @files = grep {/$wordToBeReplaced/} readdir BIN;
close BIN;


Now you loop through @files and change the names using another regexp:


Code
opendir(BIN,"path/to/directory") or die "$!" 
my @files = grep {/$wordToBeReplaced/} readdir BIN;
close BIN;
foreach my $oldfile (@files) {
my $newname = $oldfile;
$newname =~ s/searchpattern/replacepattern/;
rename($oldname,$newname) or
warn "Couldn't rename $oldname to $newname: $!\n";
}


rename() will work with only the filenames if you are in the directory where the files are, otherwsie you have to prepend the names with the full path to the directory:


Code
rename("path/to/$oldname","path/to/$newname")


and of course changing the filename is subject to the restrictions of your operating system, ie: open files, pe-existing files, etc.
-------------------------------------------------


adaykin
Novice

Jun 27, 2007, 12:33 PM

Post #7 of 13 (1206 views)
Re: [KevinR] Hey I want to rename a certain part of a file [In reply to] Can't Post

ok thanks for the help, I should have mentioned that I am on a windows system, probably xp but maybe vista in the not so distant future. Would I be able to change a filename like I want to on windows xp?
------------------------------------------------------------

New Horizon Designs <-- My site, just updated the GUI to a PHP Nuke interface


KevinR
Veteran


Jun 27, 2007, 12:41 PM

Post #8 of 13 (1204 views)
Re: [adaykin] Hey I want to rename a certain part of a file [In reply to] Can't Post

I think you will be OK on XP and Vista. Might be a problem if the files are open when you try and rename them. I'm not sure what XP or Vista does if the filename already exists but you will find out I suppose. Smile
-------------------------------------------------


KevinR
Veteran


Jun 27, 2007, 12:43 PM

Post #9 of 13 (1203 views)
Re: [adaykin] Hey I want to rename a certain part of a file [In reply to] Can't Post

I made a boo-boo in my code. Instances of $oldfile should be $oldname:


Code
opendir(BIN,"path/to/directory") or die "$!"  
my @files = grep {/$wordToBeReplaced/} readdir BIN;
close BIN;
foreach my $oldname (@files) {
my $newname = $oldname;
$newname =~ s/searchpattern/replacepattern/;
rename($oldname,$newname) or
warn "Couldn't rename $oldname to $newname: $!\n";
}

-------------------------------------------------


adaykin
Novice

Jun 27, 2007, 12:58 PM

Post #10 of 13 (1201 views)
Re: [KevinR] Hey I want to rename a certain part of a file [In reply to] Can't Post

ok 2 more quick ?'s, so you think that changing the filename to a string and inserting at nth character should work on xp, and can I put a variable in the pattern s/$variableHere/replacepattern/ and if so do I have to put it in ""s?
------------------------------------------------------------

New Horizon Designs <-- My site, just updated the GUI to a PHP Nuke interface


KevinR
Veteran


Jun 27, 2007, 1:10 PM

Post #11 of 13 (1200 views)
Re: [adaykin] Hey I want to rename a certain part of a file [In reply to] Can't Post

It would help if you can just say what you are looking for and what you want to change to.

Yes you can use variables in a regexp:


Code
my $var = 'sleeping': 
my $replace = 'eat';
my $search = 'sleep';
$var =~ s/$search/$replace/;
print $var; # prints "eating";


You never (repeat never) put quotes around scalar variables unless you are creating a new string.


Code
$foo = 'foo'; 
$bar = 'bar';

print $foo; # right
print "$foo"; # works but is wrong
print '$foo'; # wrong
$foo = "$foo$bar"; # right because you are making a new string
$foo .= $bar; # also right
$foo = $foo . $bar; # also right
$foo =~ /"$search"/$replace/; # definetly wrong

-------------------------------------------------


adaykin
Novice

Jun 27, 2007, 1:25 PM

Post #12 of 13 (1198 views)
Re: [KevinR] Hey I want to rename a certain part of a file [In reply to] Can't Post

what you are looking for and what you want to change it to are both variables that get entered by the user earlier in the program. I wasn't sure if this would be possible in perl.
------------------------------------------------------------

New Horizon Designs <-- My site, just updated the GUI to a PHP Nuke interface


KevinR
Veteran


Jun 27, 2007, 3:08 PM

Post #13 of 13 (1196 views)
Re: [adaykin] Hey I want to rename a certain part of a file [In reply to] Can't Post


In Reply To
what you are looking for and what you want to change it to are both variables that get entered by the user earlier in the program. I wasn't sure if this would be possible in perl.


It is possible, but user input should always be treated like poison. Handle it with care and use appropriate precautions.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives