#1 20 May 2019 11:16

Registered: 17 Apr 2016
Posts: 20

Count filename length and output to file if over 102 characters?

The problem: I have a bunch of files in subdirectories. I want to recursively check the length of each filename and output it to a text file (ideally with the path) if the name is over 102 characters. I want to do this to check for names that are too long for burning to a disc and that will need to be renamed.

I tried Googling for a solution, but I can't find any that exactly match what I want to do and they use so many variable manipulations that when I try adapting them to what I want to do, they fail.

I grabbed the following code from somewhere a while back and at the time it seemed to work. Of course now it doesn't work and I have no idea why.

echo off
SETLOCAL EnableDelayedExpansion
for %%F in (*.*) do ( 
echo %%F >tempfile.txt
for %%X in (tempfile.txt) do (set /A length=%%~zX-3)
if !length! gtr 102 echo %%F !length! >>!Results.txt
del tempfile.txt

It's not recursive, but even in the current directory it doesn't work. If there is a file with a name over 102 characters, it creates a text file named "Results.txt", not "!Results.txt" and that contains the single word "length", rather than the expected contents of the variable. I want the results filename to start with "!" so that it will be at the top of the directory list.

It correctly writes the names to the temp file, so the problem is in the line that tries to count the length of that file. I have absolutely no clue why that line doesn't work, or why I can't specify a filename that starts with !, although I suspect it's the fault of the stupid delayedexpansion and some archaic rule that I'm not familiar with no matter how many times I try to understand why this is even a thing.

Last edited by Rekrul (20 May 2019 13:00)


#2 20 May 2019 15:58

From: UK
Registered: 29 Dec 2006
Posts: 1,077

Re: Count filename length and output to file if over 102 characters?

If you're using delayed expansion, you will have problems trying to use "!" for stuff, as you've discovered.

I can see what that script is trying to do, but it's a bit over engineered for your problem -- though it would be a reasonable solution for finding the exact length of a string, I think you just need to know if it's too long or not, correct?

You haven't made it clear if it's the file name or full path you want to check.  I've assumed file name.

To that end, try this


@echo off
REM recurse in current directory looking for files items
for /f "usebackq tokens=*" %%a in (`dir /a:-d /b /s *.*`) do (
  REM invoke subroutine passing the file name and maximum permissible length
  call :check "%%~a" 102

goto :EOF

REM assuming you want the file name only
set name="%~nx1"

REM use this next line instead if you want to test the whole path
REM set name="%~1"
setlocal enabledelayedexpansion
REM substring expansion using passed param %2 as starting offset, get 1 character
REM if no character was found, the file name is within the maximum length
if "!name:~%2,1!" EQU "" (
  echo:OK - "%name%"
) ELSE (
  echo:TOO LONG - "%name%" 
  REM we don't know how long, because we didn't measure that
  REM but we can show the extra characters
  echo:Extra characters: "!name:~%2!"
goto :EOF

Last edited by bluesxman (21 May 2019 09:42)

cmd | *sh | Ruby | Chef


Board footer

Powered by FluxBB