Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

December 17, 1999 03:19 PM

Real-World Scripting: Locating Long Paths in a Directory

Windows IT Pro
InstantDoc ID #7828
Rating: (0)
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.

Related Content:

ARTICLE TOOLS

Comments
  • Dick Lewis
    10 years ago
    Feb 19, 2002

    Without further information, identifying the problem is difficult. The problem could be one of many. For example, you might be using the incorrect Perl version for your operating system or you might need to install Windows Installer to install the MSI file.

  • Karen
    10 years ago
    Feb 07, 2002

    I am very new to scripting and I was wondering if someone might be able to help me. I am trying to run the LongPathLocator.pl script but when I attempt to install ActivePerl, I receive the following error: Internal Error 2755. 1632 C:\\Perl\\ActivePerl-5.6.1.631-MSWin32-x86.msi. Does anyone know what the problem might be? Please excuse my ignorance on this topic.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.