niCon
niCon

Reputation: 1

count length of filenames in batch

my problem is I want to count the length of multiple filenames and save this numbers into a file.

My approach is this:

@echo off
for %%i in (*.txt) do (
    set Datei=%%~ni
    call :strLen Datei strlen

    :strLen
    setlocal enabledelayedexpansion

    :strLen_Loop
    if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
    (endlocal & set %2=%len%)

    echo.%strlen%>> tmp
)

The problem here is it only works for the first filename and after that it is stuck and does not go on to the next filename.

Upvotes: 0

Views: 2608

Answers (4)

niCon
niCon

Reputation: 1

well the solution was coming from you i just moved the parts out of the loop. the code is this:

@echo off
for %%i in (*.txt) do (
set Datei=%%~ni
call :strLen Datei strlen
)

:strLen
setlocal enabledelayedexpansion

:strLen_Loop
if not "!%1:~%len%!"=="" set /A len+=1
goto :strLen_Loop

(endlocal & set %2=%len%)
echo.%strlen%>> tmp

Upvotes: 0

Paul
Paul

Reputation: 2710

A slightly modified how to do count length of file name.

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
(
    for  /f "tokens=*" %%A in ('dir *.txt /B /S /A:-D ^| findstr /IV "_output.txt"') do (
        set "name=%%~nA" 
        @echo "!name!">"%TMP%\_Temp.txt"

        for %%I in ("%TMP%\_Temp.txt") do ( 
            set /a "length=%%~zI"
            set /a length-=4
            @echo !length!:'!name!'
        )
    )
)> _output.txt 2>&1
del "%TMP%\_Temp.txt"
MORE /C /P _output.txt
ENDLOCAL
EXIT /B 0

Upvotes: 1

JosefZ
JosefZ

Reputation: 30113

Just for another alternative of a one-line command:

for %# in (*.txt) do @for /F "delims=:" %G in ('(echo "%~f#"^&echo(^)^|findstr /O "^"') do @if %~G NEQ 0 ( <^NUL set /p "dummy=%~f#|%~z#|"&set /a %~G-5&echo()

or the same in a bit more readable form:

for %# in (*.txt) ^
do @for /F "delims=:" %G in ('(echo "%~f#"^&echo(^)^|findstr /O "^"') ^
do @if %~G NEQ 0 ( <^NUL set /p "dummy=%~f#|%~z#|"&set /a %~G-5&echo()

Unfortunately, unlike the cmd shell, set command does not display its result in a batch script. Therefore, we need to set a string length to an environment variable and then echo its value with delayed expansion enabled:

@ECHO OFF >NUL
SETLOCAL EnableExtensions EnableDelayedExpansion
rem file lengths:
rem for %%A in (*.txt) do echo Name:%%A, Length: %%~zA
rem full path lengths:
for %%# in (*.txt) do (
  for /F "delims=:" %%G in ('(echo "%%~f#"^&echo(^)^|findstr /O "^"') do (
    if %%~G NEQ 0 (
      set /a "length=%%~G-5"
      rem echo(%%~f#^|%%~z#^|!length!
      echo(%%~f#^|!length!
    )
  )
)

Upvotes: 3

MC ND
MC ND

Reputation: 70923

Just for another alternative, findstr can output the offset of each matching line. Using it over the dir output and substracting the offset of the previous line from the current one (and the CRLF at the end of the line), we get the length of the previous line / file name

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "lastOffset=0"
    for /f "tokens=1,* delims=:" %%a in ('(dir /b *.txt ^& echo(^) ^| findstr /o "^"') do (
        if %%a gtr 0 (
            set /a "size=%%a - lastOffset - 2"
            setlocal enabledelayedexpansion
            echo(!fileName! !size!
            endlocal
        )
        set "lastOffset=%%a"
        set "fileName=%%b"
    )

Upvotes: 2

Related Questions