Dear all My name is Max, I am just a beginner in the world of Perl and so I apologize if my question, that seems to me so hard that I can get through by myself, is indeed silly. I got a dir plenty of files like that:

GridSquare_0000000077_FoilHole_0000000001_Data_0000000002_20120315_103721.jpg GridSquare_0000000077_FoilHole_0000000001_Data_0000000002_20120315_103721.xml

The number of this files are not sequential I would like to rename this file in a more handy way as 1.jpg and 1.xml, 2.jpg 2.xml up to the last one. In the position number 498 and 500 of the file *xml there are two numbers. I would like also to create a directory with this two numbers (something as 15 or 20) and move in this directory the corresponding files. So if th20120315_103721.xml has in position 498 and 500 the numbers 1 and 5 I would like to move this file and corresponding jpeg in the dir 15 and so on. I started step by step. First with a script that should have modified the name but indeed remove the files !

< #!/usr/bin/perl -w ## ## use strict ; my $dir = 'ParticleAcquisitionDataImages/'; my @fileList = glob "${dir}*jmg*"; foreach (@fileList) { next if -d; my $oldname = $_; # $_=substr($_, 0, 1, ''); # s/.//; s/^.(.*).$/$1/; rename $oldname, $_ or $_ = $oldname, warn $_, ' not renamed: ', $!; } close >
Thank you in advance Max

In reply to Renaming files in a directory in sequence by Max79

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.