How to adhere to 8.3 naming conventions

Posted on 2006-04-21
Last Modified: 2010-10-05
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....

Question by:artofchobo
    LVL 4

    Author Comment


    So far, I have done

         $first = $&;
         $second = $&;
         $filename = "$first"."$second";

    LVL 4

    Author Comment

    $j = 1;
    $i = 0;
    $k = 0;
    @freq = 0;
    #      $first = substr($_, 0, 8);
          $first = $&;
          $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;
    print @x;

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

    This is what I have in mind right now... but its not working......
    LVL 41

    Accepted Solution

    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";


    LVL 41

    Expert Comment


    Any particular reason for the 'B'?  I thought that I had gone above and beyond with sufficient information to warrent an 'A'.
    LVL 4

    Author Comment

    I dont mind giving better records.....

    Sorry... I havent tried it out yet but I guess the code should work!.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now