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 :) -
my $dirs = "$folder";
-
opendir (BIN, $dirs) || die "cant open directory: $!";
-
my @array = grep { -d "$dirs" } readdir BIN;
-
foreach my $file (@array) {eval {
-
symlink ("$folder/$file","$file/_trn.img");
-
4 1310 KevinADC 4,059
Recognized Expert Specialist
actually you are reading in a list of folders in the directory: - 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: - my @array = grep { -d $dirs } readdir BIN;
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. - use File::Basename;
-
use File::Find::Wanted;
-
-
my $path = '/path/to/directory';
-
my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;
-
-
foreach my $file (@files) {
-
-
-
my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
-
my $new_filename = $old_filename . '_new' . $ext;
-
-
rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
-
-
}
--Kevin
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. - use File::Basename;
-
use File::Find::Wanted;
-
-
my $path = '/path/to/directory';
-
my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;
-
-
foreach my $file (@files) {
-
-
-
my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
-
my $new_filename = $old_filename . '_new' . $ext;
-
-
rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
-
-
}
--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 :)
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: -
rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
-
to something like: -
symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
-
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: -
use File::Basename;
-
use File::Find::Wanted;
-
my $path = '/path/to/directory';
-
my %ext_map = ( '.jpeg' => '.jpg' );
-
my @files = find_wanted( sub { -f && /\.jp.*g$/ }, $path ) ;
-
foreach my $file (@files) {
-
my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
-
my $new_ext = $ext_map{$ext} || $ext;
-
my $new_filename = $old_filename . '_new' . $new_ext;
-
symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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 ?
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |