Gareth Segree wrote:
>
> I have a directory of files that I want to move to another directory.
> (eg. ALLY20030111W.eps
>      TEST W20030122
>      HELP WANTED20030901WW.eps
>      GIRL WATCH BIRD 20030101
> etc..)
>
> I want to be able to parse the filename and replace the date portion with
> any date
> (eg $1="ALLY" $2="20030111" $3="W" $4=".eps")
> Then I want to make $2="20030925" and if $3 is empty then I assign ".eps"
> to $3 or if $4 is empty then assign ".eps"
>
>
> How do I do this?
>
> #!/usr/bin/perl
> # move_file.plx
> use warnings;
> use strict;
>
> $source = "/path/to/source/";
> $destination = "/path/to/destination/";
> $query = "([A-Za-z]+)(\s*?)([0-9]*)(\s*?)([A-Za-z]*)([eps])"
> opendir DH, $source or die "Couldn't open the current directory: $source";
> while ($_ = readdir(DH)) {
>    next if $_ eq "." or $_ eq "..";
>
>    if (/$query/) {
>       print "Copying $_ ...\n";
>       rename $source$_, $destination$_;
>       print "file copied successfully.\n";
>    }
> }
>
> What's wrong with my code. Am I overlooking something?

Your code doesn't attempt to make the replacement that you describe: it
simply copies a file from one directory to another with no change of
name. More precisely it would, if it were valid Perl. Have you put in

  use warnings;
  use strict;

for the purposes of your post? Because your program will have thrown
up several errors that you should be able to correct.

With regard to editing the file name, you need to say more about
what source filenames are possible. If the only numeric content of
the name is the date field then it's very easy, but if you could
have

  MAN 2 MAN18800101.eps

then it's a little tricker.

Let us know,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to