Active 8 months ago. Viewed 84 times. Improve this question. TLP Trey Trey 53 5 5 bronze badges. Maybe you are looking for the rename utility. Apparently you are getting errors. You should include those when asking for help. Also you should use use strict; use warnings; to get critical information about your code.
Add a comment. Active Oldest Votes. Start the script with! Improve this answer. Updated the script and corrected my mistakes thanks guys. The script ran with no errors but is not rename the file so they all stayed the same. Trey: Try printing the filenames as you progress. Maybe the script doesn't find any file? The standard way to copy a file is to use the copy function of File::Copy. For documentation visit perldoc File::Copy. Toggle navigation Perl Maven.
Standard output, standard error and command line redirection Warning when something goes wrong What does die do? Unknown warnings category Can't use string Symbolic references in Perl Can't locate Can't locate object method " Useless use of hash element in void context Useless use of private variable in void context readline on closed filehandle in Perl Possible precedence issue with control flow operator Scalar value So you can chdir before renaming:. You might want to combine your directory reading and filtering using grep :.
You seem to be assuming glob -like behavior rather than than readdir -like behavior. The underlying readdir system call returns just the filenames within the directory, and will include two entries. This carries through to the readdir function in Perl, just to give a bit more detail on mu's answer. Alternately, there's not much point to using readdir if you're collecting all the results in an array anyways. As already mentioned, your script fails because of the path you expect and the script uses are not the same.
I would suggest a more transparent usage. Hardcoding a directory is not a good idea, IMO. As I learned one day when I made a script to alter some original files, with the hardcoded path, and a colleague of mine thought this would be a nice script to borrow to alter his copies.
Using the absolute paths will leave the user with no doubt as to which files he'll be affecting, and it will also make the script reusable. I don't know if the regex fits the specifig name of the files, but in one line this could be done with:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Using Perl to rename files in a directory Ask Question. Asked 10 years, 1 month ago. Active 2 years, 5 months ago.
0コメント