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

Home Posts Topics Members FAQ

Moving files based on file name

This is the scenario:

I have the following ZIP files in a directory. The numbers represent
versions (the highest number is the latest version of the file).
mercury001.zip
mercury002.zip
mercury003.zip
venus122.zip
venus123.zip
venus124.zip
mars005.zip
mars006.zip
mars007.zip

I wanted to keep the latest version and move (copy and delete) the rest to
an archive directory. The directory should only contain the following
files:
mercury003.zip
venus124.zip
mars007.zip

I am a novice PERL developer. How do I accomplish this? Would appreciate
some examples. Thanks!

Tor
Jul 19 '05 #1
1 4670
Here is one way to do it.
Tested only on Active State perl v5.8.0 on Windows XP.
Will not work if the file order from a glob is not alphabetic.
Geraldo
---

#!/perl/bin/perl

use strict;
use Carp;

# path to zip files
my $zipDir = "C:/tmp/zips";
die ("$!") unless -d $zipDir;
# path to archive
my $archDir = "C:/tmp/arch";
die ("$!") unless -d $archDir;
# get the directory listing
#default order is by file name on NTFS
#may not be the case on other platforms
my @zips = glob $zipDir.'/*';
die ("$!") unless @zips;
# reverse it
#puts latest file name for each type first
@zips = reverse @zips;
my %types;
for ( @zips )
{
my $fn = $_;
# strip everything up to the filename
$fn =~ s/.*\///;
# put the file type into $1
$fn =~ s/(.*?)\d//;
if( $types{$1} )
{
# for loop has already hit the latest of this type
#archive the rest
$fn = $_;
$fn =~ s/.*\///;
rename $zipDir.'/'.$fn, $archDir.'/'.$fn || die;
}
# for loop has not hit this type yet
# set the type key to 1
$types{$1} = 1;
}
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1665
by: Michal Migurski | last post by:
Hi everyone, I've been planning to move to Python from PHP for some time now. I use PHP extensively for web scripting, with mod_php, Apache, and a DB (I would characterize my knowledge of PHP as...
4
5478
by: Stephen Ghelerter | last post by:
I am moving a web site with a MySql database to another server. Can I create a database on the new server with the same name and then move the tables there, or is life not that simple? Or can I...
6
8870
by: Hemant Shah | last post by:
Folks, I need to move HOME directory of an instance to another directory. What is the best way of doing it? Is changing password file enough? or dies DB2 store this info in it's own config? ...
3
1407
by: jqpdev | last post by:
Hello all, I've been developing web apps using Borland's websnap technology which is built upon asp technology. I'm tranisitioning to ASP.NET VS.NET and need some techniques/best practices to...
5
3818
by: Franck | last post by:
Hello, I've just moved to visual developper 2005 to do so, I also had to use the convert assistant. what it did; moving my file resx files that i had in a diresctory called resx to a new...
2
3201
by: 63q2o4i02 | last post by:
Hi, I'm using python 2.4 and windows XP. I have two packages in the windows version of python in site-packages. They are PyVisa and ctypes, and both live in c:\python24\lib\site-packages ...
3
1795
by: Daves | last post by:
why the heck can't I use Directory.Move() to rename a directory (containing files) to a entry 2 levels below e.g. uploads/tempfiles -> uploads/tempfiles/48/003files if I do this in one line...
3
1737
by: tshad | last post by:
I normally build my site with DW and used VS 2003 to build a Web page to call my web service. I now want to move the file over to my production system, but don't know which files I need. I...
11
2230
by: Jim in Arizona | last post by:
I've looked around the web but can't find anything to help me out. I was able to get some code to move some files from one directory to another, which works fine: ...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7333
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,...
1
7061
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
7502
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
5637
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
5057
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
4716
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
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.