Skip navigation

Real-World Scripting: Locating Long Paths in a Directory

Downloads
7828.zip

As the Windows NT administrator at your company, you recently started receiving complaints from users who've been having problems copying, editing, saving, and deleting files on the main file server. You've noticed some very long folder names and filenames in your directory. A quick check reveals that one problematic file has a path 270 characters long—well over Windows' 255 path-length limit. You're concerned that the paths to many more files are near or have exceeded the limit and that these long paths are causing the problems users are experiencing with the files.

Rather than manually check all the path lengths in the directory, you want to write a script that automatically locates and reports the names and locations of at-risk files (i.e., files whose paths exceed the Windows' path-length limit or another threshold you specify). You plan to use this report as the basis for corrective actions, such as truncating long folder names and shortening filenames. The script has the following requirements:

  • Make the path-length threshold limit configurable.
  • Create separate HTML reports for each top-level folder.
  • Include the date, path-length threshold, and folder name in each report's heading.
  • Identify any paths that exceed the path-length threshold (i.e., the at-risk files).
  • Enumerate the absolute path (i.e., the fully qualified path containing drive and directory information) of each at-risk file.
  • Specify the path length of each at-risk file.
  • Number the files in each report.
  • Print a message stating when the script doesn't find any at-risk files.

The script LongPathLocator.pl meets these requirements. To use LongPathLocator.pl, you need to know how the script works and how to prepare your system and the script.

How the Script Works
LongPathLocator.pl is a Perl script that uses NT shell commands to obtain path information from the NT network. Although NT shell commands are quite useful, you can't use the NT shell scripting language to write this script. The NT shell command that locates strings (i.e., Findstr) works only with strings about 100 characters long. Because you need to work with strings about twice that length, you need to use another scripting language, such as Perl.

LongPathLocator.pl uses the NT shell Dir command. In one instance, the script uses Dir with the /s and /b switches to obtain the file paths in the directory you specify. The /s switch ensures that Dir lists all the paths in the directory and its subdirectories. The /b switch ensures that Dir lists only the paths and no summary information.

After Dir obtains the file paths, it populates the @List array with them. The script then tests each path against the path-length threshold you specify. If a path is over the threshold, the script uses Perl's while statement to continue incrementing the threshold number until the script reaches the last character in the path, which causes the loop to end. The last incremented number is the path length of that at-risk file. The script uses the $Counter variable to number the at-risk files in each report.

The script prints the information it gathers in the Long Path Report. Figure 1 contains an example of a report with paths over the at-risk threshold of 200. Figure 2 contains an example of a report without paths over the threshold.

How to Prepare Your System and the Script
You can download LongPathLocator.pl from the Code Library on the Win32 Scripting Journal Web site (http://www.winntmag.com/newsletter/scripting). However, before you can use this script, you need to follow these steps:

  1. Download and install Perl for Win32 on the NT workstation or server where you plan to run the script. Perl for Win32 is available as part of ActiveState's free ActivePerl package (http://www.activestate.com/activeperl). I used ActivePerl, build 519, to write LongPathLocator.pl.
  2. Configure the local or remote path to the directory in which you want to test for long paths, as callout A in Listing 1 shows. In a local path, use double backslashes instead of single backslashes (e.g., C:\\testdir\\fred instead of C:\testdir\fred). In a remote path, use four initial backslashes followed by double backslashes (e.g., \\\\fileserver12\\publicshare instead of \\fileserver12\publicshare).
  3. Configure the path-length threshold you want to use, as callout A shows. Any file with a path over this threshold will appear in the Long Path Report.
  4. Configure the HTML file's location and name, as callout B shows. You must include $DIR in the filename (e.g., C:\\TEMP\\output$DIR.html).
  5. Test the script to make sure you've configured it correctly. The script's runtimes will vary, depending on the number of files in the directory and the speed of the network connection to the file server.
  6. Configure the report's path to a final location on the intranet Web server, if appropriate.
  7. Schedule LongPathLocater.pl to run regularly.
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish