How to adhere to 8.3 naming conventions

Hi guys,
I'm a newbie in perl.
I would like to have some ideas on how to convert a text file with filenames, seperated by \n (newline) to become 8.3 format using perl.

Meaning to say that
salarytext.file becomes salary~1.fil (8.3 naming convention)
Basically, its should just be like DOS...
I need some tips....


Thanks
Regards,
chobo
LVL 4
artofchoboAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

artofchoboAuthor Commented:

So far, I have done

while(<>)
{
     m/w{1,8}/;
     $first = $&;
     m/\.\w{3}/;
     $second = $&;
     $filename = "$first"."$second";
}

0
artofchoboAuthor Commented:
#!/usr/bin/perl
$j = 1;
$i = 0;
$k = 0;
@freq = 0;
while(<>)
{
#      $first = substr($_, 0, 8);
      m/\w{1,8}/;
      $first = $&;
      m/\.\w{3}/;
      $second = $&;
      $filename = "$first"."$second\n";
      push @x, $filename;
}

for ($k = 0; $k < $#x+1; $k++)
{
      for ($i = $j; $i < $#x+1; $i++)
      {
#            print "$x[$i]";
            if ($x[$k] == $x[$i])
            {
                  $freq[$k] +=1;
                  transform($x[$i]);
            }
      }
      $j++;
}
print @x;

sub transform
{
      $trunc = shift();
      substr($trunc, 0, 6)."_freq[$k]";
      $x[$i]=$trunc;
}

This is what I have in mind right now... but its not working......
0
HonorGodSoftware EngineerCommented:
It isn't completely clear, from the information provided, what you expect the input to be.

For example, are these the names of existing files?
Are you trying to discern the "actual" 8.3 name of the specified file?

To begin, we need to generate a regular expression that will:
- Identify the 1 to 8 "word" characters in the line
- Identify the 1 to 3 "word" characters after the period

As is frequently the case, the "simple" expression is not completely right:
  $line =~ /(\w{1,8})*\.(\w{1,3})/;
This does (kind of) what is described above, but when we test it with a name like:

This is a very long filename.with a very long extension

the first part of the expression matches with "filename" because it immediately preceeds the period, which is not what we want.  A little thought might lead us to:
  $line =~ /(.+)\.(.+)/;
but this does nothing about restricting the length, so subsequent processing would be required.
Eventually, we might get to:
  $line =~ /(\w{1,8})[^.]*\.(\w{1,3})/;
Which, is what is really needed.  In English, this is:
- Locate (and save) a sequence of 1 to 8 "word" characters (alphnumerics, plus underscore)
- Skip over all non periods, followed by a period
- Locate (and save) a sequence of 1 to 3 "word" characters

  How do we placed the temporary "saved" values into other variables for processing?  Easily enough.
  ($filename, $ext ) = ( $1, $2 )

  So far, so good.  However, if what you are trying to do is to match the way files are named by
Windows, you should note that if there are any blanks in the first 6 positions, Windows will remove
them before building the "short"  or tilda form of the name.  In addition, it will translate all lowercase
letters to uppercase, since it (Windows) does not consider case significant.

  It would seem that the right way to determine the "short" or tilda form of the filename would be to
let Windows tell us what it is.  For example:

  sub trim {
    my $str = shift( @_ );
    $str =~ s/^\s+//;
    $str =~ s/\s+$//;
    return $str;
  }

  @dir = `cmd /c dir /x`;
  for ( my $i = 0; $i < $#dir; $i++ ) {
     chomp( $name = $dir[ $i ] );
     if ( length( $name ) > 52 ) {
        $filename = trim( substr( $name, 39, 12 ) );
        if ( $filename eq "" ) {
           $filename = substr( $name, 52 );
        }
        print "\"$filename\"\n";
     }

  }




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HonorGodSoftware EngineerCommented:
Thanks

Any particular reason for the 'B'?  I thought that I had gone above and beyond with sufficient information to warrent an 'A'.
0
artofchoboAuthor Commented:
Mis-click....
I dont mind giving better records.....

Sorry... I havent tried it out yet but I guess the code should work!.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.