• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

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
0
artofchobo
Asked:
artofchobo
  • 3
  • 2
1 Solution
 
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
 
HonorGodCommented:
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
 
HonorGodCommented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now