Hello Monks
I am looking for generic way to implement the following task
Suggest me some ideas
I have a directory say c:\test under which there are some files with names as
ABCTEST_062506.DAT
XYZTEST_062506.DAT
ABCTEST_062406.DAT
XYZTEST_062506.DAT
ABCTEST_062406.DAT
XYZTEST_062506.DAT
ABCTEST_062406.DAT
XYZTEST_062506.DAT
The filenames which start with ABC has to be renamed with
ABCD and filenames start with XYZ should be renamed with XYZD...
I am scanning the directory for files
sub scanDir()
{
$check ="ABCD" # this value will come from an array
$base_dir = $_[0];
opendir(DIR, $base_dir) || die "Error: could not open input direct
+ory $base_dir";
@current_dir = readdir(DIR);
chomp(@current_dir);
foreach $current_dirs (@current_dir)
{
$file_name = $current_dirs;
$var = substr($file_name, 0, 3);
$newvar = substr($file_name,3);
if($var eq $check){
$newFileName = $check . $newvar;
}
}
closedir(DIR);
}
Is this approach correct way
Note: I updated the code there were some mistakes
The idea is to make it generic if suppose some new file names
comes and all we have to specify the new filename and code should work
The original filename will remain same when the file is
copied or moved it will be renamed
Thanks in advance
Sridhar
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.