AfterDawn Forums

Here is a Long Filename / Filepath Scan Utility

I wrote and am using a batch file to scan for long filenames / filepaths. It was inspired by Indochine in his post #514228 "HELP with finding Length of String in BATCH file"

His insight was helpful to me so I thought I would give back. Please note that the one thing that made this batch file 5 times longer to write is the Delayed Expansion concept in variables - ie. !variable! vs. %variable%. I still haven't got it clear when I'm supposed to use which.

I did not go to the effort of being precise about the length of the filename. As it is written, all lengths are +2. For my purposes, a length of 222 is inconsequential compared to 220, or 257 vs. 255, or 300 vs. 298. Also, I have tested this on mapped network drives and it seems to work fine.

Thanks, Indochine, for the idea of writing the filename/path to a file and using the filesize as length.

For the inexperienced, this is a batch file for Windows. You can create the utility by selecting all the text between ===Start=== and ===End=== and pasting it into Notepad. Modify the "set scanroot=c:\" line near the top of the program for the point in the directory tree you want scanned - it will do the entire C: drive as it is written. Then save the Notepad file as something like "lfn.cmd". Then open a command prompt window at the location you saved lfn.cmd and simply type as a command "lfn". When the scan is complete, any filenames/paths longer than the specified length (220 as written) will be listed in the file "lfn.dat" which can be viewed with Notepad.

If these instructions are not clear and simple to you, then you should get someone to help you.

===== Start of Program =============================
@echo off
REM ***********************************************************************
REM ***********************************************************************
REM ***********************************************************************
REM ***
REM ***
REM ***This batch file will scan the full path of all files from the
REM ***specified folder root looking for long file names, ie. > 255 characters.
REM ***Offending filenames/paths are written to lfn.dat pre-pended with
REM ***their length. The length breakpoint should be set in the range of
REM ***220 chars. Filename/path length is determined by writing the filename
REM ***to a .tmp file and then checking the size of that .tmp file. The filesize
REM ***will always be 2 chars longer due to the CR/LF written with the filepath.
REM ***
REM ***********************************************************************
REM ***********************************************************************
REM ***********************************************************************

setlocal EnableDelayedExpansion
set /a count=0
set /a mod=0
set /a length=0
set /a toolong=0
set /a howlong=220
set scanroot=c:\
set msg=

REM ***
REM ***Delete output file from last session
REM ***
if EXIST lfn.dat del lfn.dat

REM ***
REM ***For each file in the tree from this root...
REM ***Accumulate number of filenames scanned
REM ***mod=0 at every 100th record
REM ***Set fn to the current filename
REM ***Write only the current filename to lfn.tmp
REM ***%a=file parameters of lfn.tmp; ~z of a = file size, which is length of filename +2
REM ***If filename is too long...
REM ***Count it...
REM ***Print the length and current full filename to the end of lfn.dat
REM ***If this is a 100th record...
REM ***Update the screen, showing only the first 50 characters of the current filename
REM ***
for /r %scanroot% %%i in (*.*) do (
set /a count+=1
set /a mod=count%%100
set fn=%%i
echo !fn! > lfn.tmp
for %%a in (lfn.tmp) do (set /a length=%%~za)
if !length! GTR !howlong! (
set /a toolong+=1
echo !length!-!fn!>>lfn.dat
if !mod! EQU 0 (
set msg=Scanned:!count! Found:!toolong! !fn:~0,50!
echo !msg!

REM ***
REM ***Final screen update
REM ***
set msg=Scanned:!count! Found:!toolong! !fn:~0,50!
echo !msg!
===== End of program ==============================

This message has been edited since its posting. Latest edit was made on 14 Jan 2013 @ 13:29
▼▼ This topic has 0 answers - they are below this advertisement ▼▼
AfterDawn Advertisement
This discussion thread has been automatically closed, as it hasn't received any new posts during the last 180 days. This means that you can't post replies or new questions to this discussion thread.

If you have something to add to this topic, use this page to post your question or comments to a new discussion thread.