473,508 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Renaming files

2 New Member
Hi I'm relatively new to Perl.

I'm attempting to read in all the file names which are located in a directory to an array, modify each file name by removing the last 4 characters of each file then replacing them with a new 4 character extension. I then would like to set symbolic links using the new file names.

eg:- <filename>.img to <filename>_trn.img

So far i have the following code which works fine at creating the symbolic links, but i cant seem to get the file name change.
Please help :)

Expand|Select|Wrap|Line Numbers
  1. my $dirs = "$folder";
  2. opendir (BIN, $dirs) || die "cant open directory: $!";
  3. my @array = grep { -d "$dirs" } readdir BIN;
  4. foreach my $file (@array) {eval {
  5. symlink ("$folder/$file","$file/_trn.img");
  6.  
Feb 26 '08 #1
4 1310
KevinADC
4,059 Recognized Expert Specialist
actually you are reading in a list of folders in the directory:

Expand|Select|Wrap|Line Numbers
  1. my @array = grep { -d "$dirs" } readdir BIN;
the -d flag checks if the expression is a directory. There is no need to quote a single scalar, you should stop doing that now, it is a bad habit to get into when writing perl code:

Expand|Select|Wrap|Line Numbers
  1. my @array = grep { -d $dirs } readdir BIN;
Feb 26 '08 #2
eWish
971 Recognized Expert Contributor
I like to use File::Find::Wanted module to seek out files. Then I started using File::Basename to parse the file name. You can use the rename() function to rename a file.

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2. use File::Find::Wanted;
  3.  
  4. my $path = '/path/to/directory';
  5. my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;
  6.  
  7. foreach my $file (@files) {
  8.  
  9.  
  10.     my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
  11.     my  $new_filename = $old_filename . '_new' . $ext;
  12.  
  13.     rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
  14.  
  15. }
--Kevin
Feb 26 '08 #3
al bundy
2 New Member
I like to use File::Find::Wanted module to seek out files. Then I started using File::Basename to parse the file name. You can use the rename() function to rename a file.

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2. use File::Find::Wanted;
  3.  
  4. my $path = '/path/to/directory';
  5. my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;
  6.  
  7. foreach my $file (@files) {
  8.  
  9.  
  10.     my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
  11.     my  $new_filename = $old_filename . '_new' . $ext;
  12.  
  13.     rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
  14.  
  15. }
--Kevin

Hi Kevin

Thanks for this and for your quick response,

However I don't necessarily want to change the original file names located in the folder. I would like to read in the names to an array then remove the last 4 characters of each file within the array. once this is done add a new extension to each of the files within the array then use these new file names to link to the original.
So the end result should look something like this below

<original filename><new exstention> symbolicaly linked to /<folder>/<original file name>
OR
<original filename>_trn.img symbolically linked to <original filename>.img

Hope this makes sense, think I lost it there :)
Feb 27 '08 #4
minowicz
12 New Member
However I don't necessarily want to change the original file names located in the folder. I would like to read in the names to an array then remove the last 4 characters of each file within the array. once this is done add a new extension to each of the files within the array then use these new file names to link to the original.
So the end result should look something like this below

<original filename><new exstention> symbolicaly linked to /<folder>/<original file name>
OR
<original filename>_trn.img symbolically linked to <original filename>.img

Hope this makes sense, think I lost it there :)
Kevin's example code is correct in large part. It is only his misunderstand of your desire to create symlinks rather than renaming the files that is an issue. No doubt this was in large part due to the unfortunate choice of subject in the original post. Still his code stands up to the task. You'd simply need to change:

Expand|Select|Wrap|Line Numbers
  1. rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
  2.  
to something like:

Expand|Select|Wrap|Line Numbers
  1. symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
  2.  
Incidentally, the use of File::Find::Wanted is really only needed if you intend to restrict the changes to particular patterns of filenames, and the use of File::Basename is handy only if your file extension is to remain the same, with only a few characters appended to the basename (as in your examples).

If you are also intending to map certain extensions to new extensions: For instance creating symlinks with .jpg extensions for all files that currently have .jpeg extentions, then File::Basename is still handy. You'd just insert some logic to test for a given extension and rewrite it in the name for the symlink. The following code would still add to the basename, but would create symlinks to .jpeg files as .jpg instead of .jpeg:

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2. use File::Find::Wanted;
  3. my $path = '/path/to/directory';
  4. my %ext_map = ( '.jpeg' => '.jpg' );
  5. my @files = find_wanted( sub { -f && /\.jp.*g$/ }, $path ) ;
  6. foreach my $file (@files) {
  7.     my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
  8.     my $new_ext = $ext_map{$ext} || $ext;
  9.     my  $new_filename = $old_filename . '_new' . $new_ext;
  10.     symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
  11. }
  12.  
I only mention this because it was not entirely clear from your most recent post if you also wanted such a capability in the script.
Feb 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1953
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
16
2955
by: dudufigueiredo | last post by:
I have one folder containing mp3 files, the folder is: C:\My Shared Folder\Rubber Soul And the files are: 01 drive my car.mp3 02 norwegian wood.mp3 03 you won't see me.mp3 04 nowhere man.mp3...
1
3401
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
3
4474
by: Colin | last post by:
I have two files and I want to rename file B to file A without doing a system call - can I do this in C using file pointers ?
6
5930
by: Pegboy | last post by:
I am trying to create a DOS utility that will extract data from a file and use it to form a new filename for that same file. I can successfully open the file, get the data I need and form the new...
1
3786
by: MikeY | last post by:
Hopefully someone can help, I have a listview box where I display my desired files. I single click on the desired file to be renamed and I rename it with a new name. My problem arises when the...
1
2095
by: farseer | last post by:
HI, i have created a DataSet using the DataSet Designer. The default name is DataSet1. I then renamed this to "MyDataSet" and proceed to add my TableAdapters using the Visual Designer. After i...
1
1487
by: Jack Maxwell | last post by:
Hope someone can help here. My brother has asked me if I can assist in renaming about 1500 midi files which are associated with a programm called Cubase SE. With that software installed he can...
1
7186
by: GeoDW | last post by:
Hell All, I have looked around and not found the solution I am looking for within the old threads. Here is my problem: I have a directory full of .img and .rrd files that have long filenames...
2
3581
by: Alan Mailer | last post by:
I am relatively new to VB.net. I want to change the name of a Project and change the name of the files in the Project's various folders to reflect that new project name. Is there something I...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.