Friday, November 28, 2008

Display the Length of the Filename in Windows

Sometimes if the file name is too long, some application will not work properly. So, it is better to make the name short. What is the length of the file name now ?

Instead of counting by fingers, I have written this JScript named filename_length.js :

// -------------------------------------------------------------
// Licence Information:
// --------------------
// This coding is offered at no charge for 
// NON-COMMERCIAL PERSONAL USE only.
// The coding is copyright.
// Reproduction of this coding in whole or in part 
// in any form without the express written permission 
// of the author is strictly prohibited. 
// -------------------------------------------------------------
// Disclaimer : 
// ------------
// This coding is distributed "as is" and is UNSUPPORTED.
// NO WARRANTY of any kind is expressed or implied.
// You use AT YOUR OWN RISK.
// The author will not be liable for any data loss,
// damages, and loss of profits or any other kind of
// tangible or intangible loss while using or 
// misusing this coding.
// -------------------------------------------------------------
// Purpose: Display the length of the filename(s)
//          as well as the length of the fullpath filename(s).
//          If there are multiple files,
//             the whole list of files will be displayed line by line
//             with the length of the fullpath filename inside brackets
//             And, at the end, the file(s) with the longest length
//             will be identified (except when all files are with same
//             length).
// Noted  : This script counts the number of characters in the name,
//          not the number of byte.  Double-byte character (e.g.
//          Chinese) is counted as 1 in the filename length.
//          For example, a filename with 4 Chinese characters will
//          give a length of 4 (even though the name occupies 8 bytes).
// Limitation: 1. The total number of files passed to this script
//                cannot be too large because this will trigger the 
//                error - arguments list is too long
//             2. When filename is with Unicode characters, some 
//                application cannot open it because the application
//                does not use Unicode-API.
//                This script also cannot display Unicode filename
//                correctly.  Instead, a Question Mark will be seen.
// Platform    : Windows XP
// Installation: 1. Put this script in the any folder you like
//               2. Go to the Send To folder
//               3. Create a shortcut to point to this script
// Usage     : Inside any folder,
//             1. Select one (or multiple) file
//             2. Right-Click
//             3. Select Send To
//             4. Select the shortcut of this script
//             The information will be dislayed in a pop up window.
//             After reading, click OK to quit.
// Author    : Alvin SIU
// Date      : 2007-11-28
//-------------------------------------------------------------

  var fso=  WScript.createObject("Scripting.FileSystemObject");
  var args= WScript.arguments;

//-------------------------------------------------------------
// Ensure at least 1 argument
//-------------------------------------------------------------
  if (args.length == 0) { 
    WScript.Echo('The script requires argument(s)'); 
    WScript.Quit()
  }

//-------------------------------------------------------------
// Main program
//-------------------------------------------------------------
  var dasFullpathFilename ;
  var dasFilename         ;
  var dasCount            ;
      dasCount= 0         ;
  var dasLongest          ;   
      dasLongest= 0       ;
  var dasLongestString    ;  // Store all filenames with longest length
      dasLongestString= '';
  var k                   ; 
  var dasString           ;
      dasString= ''       ;
  var dasString1          ;
  var shorter             ; // When all files are with same length,
      shorter= false      ; // both the longer and shorter flags
  var longer              ; // will be false.
      longer=  false      ;
  var dasCount            ;
      dasCount= 0         ;

  for ( k= 0 ; k < args.length ; k++) {
    dasFullpathFilename= args(k) ; 
    dasFilename= fso.GetFileName(dasFullpathFilename);

    dasCount++ ;
    dasString1= '[' + dasCount + '.]     '
              + dasFilename.length + ' ( ' 
              + dasFullpathFilename.length + ' ) ' 
              + dasFilename + '\n';

    dasString+= dasString1 ; 

    if     (dasFilename.length > dasLongest) {
              if (k > 0) longer= true ; 
              dasLongest=       dasFilename.length ;
              dasLongestString= dasString1 ;
    }
    else if (dasFilename.length == dasLongest) {
              dasLongestString+= dasString1 ;
         }
    else shorter= true;
  }

  if (k > 1) {
     if (shorter | longer) {
        WScript.Echo( dasString
                    + '\nThe Longest Filename is/are :\n'
                    + dasLongestString);
     }
     else {
            WScript.Echo( dasString );
          }
  }
  else {
         WScript.Echo( dasFilename.length + ' : ' 
                     + dasFilename+'\n'
                     + dasFullpathFilename.length + ' : ' 
                     + dasFullpathFilename);
       }

//-------------------------------------------------------------
// End Of Script
//-------------------------------------------------------------
The installation and usage instructions are at the beginning of the script flower comment.


No comments:

Duplicate Open Current Folder in a New Window

Sometimes after I opened a folder in Win7, I would like to duplicate open the same folder again in another explorer window. Then, I can ope...