1

例如,我有一个文件夹测试和一些文件:test1.txt、test12.txt、test123.txt ...我想要一个批处理程序来计算这些文件名中的字符并在屏幕上输出,例如 - 名称“test1.txt”由 9 个字符组成。名称“test12.txt”由 10 个字符组成。等等。

我写了这个脚本,但它不起作用:(

   @echo off
    setlocal enableextensions enabledelayedexpansion
    set /p directory="specify Desirable Dir: "
    pushd "%directory%"
     for %%j in (*) do (
        set /a countch=0
          set OFileName=%%j
            :loop
            set NFileName=!OFileName:~0,%countch%!
            IF !OFileName!==!NFileName! (
                echo name "!OFileName!" consists of %countch% chars
            ) ELSE (
                set /a countch = countch + 1
                goto loop
            )    
    )
    popd
    endlocal
    pause

请帮忙!提前致谢 ;)

4

2 回答 2

1

在这里你可以找到一堆strlen实现: http ://ss64.org/viewtopic.php?id=424

  @echo off
    setlocal enableextensions enabledelayedexpansion
    set /p directory="specify Desirable Dir: "
    pushd "%directory%"
     for %%j in (*) do (

          set "OFileName=%%j"
          call :strlen0.3 OFileName fnamelen
          echo !fnamelen!
    )
    popd
    endlocal
    goto :eof

:strlen0.3  StrVar  [RtnVar]
  setlocal EnableDelayedExpansion
  set "s=#!%~1!"
  set "len=0"
  for %%A in (2187 729 243 81 27 9 3 1) do (
    set /A mod=2*%%A
    for %%Z in (!mod!) do (
        if "!s:~%%Z,1!" neq "" (
            set /a "len+=%%Z"
            set "s=!s:~%%Z!"

        ) else (
            if "!s:~%%A,1!" neq "" (
                set /a "len+=%%A"
                set "s=!s:~%%A!"
            )
        )
    )
  )
  endlocal & if "%~2" neq "" (set %~2=%len%) else echo **%len%**
exit /b
    pause
于 2013-09-09T09:06:33.880 回答
1
@ECHO OFF
SETLOCAL 
SET "sourcedir=c:\sourcedir"
pushd "%sourcedir%"
for %%j in (*) do (
 set OFileName=%%j
 SETLOCAL ENABLEDELAYEDEXPANSION
 SET "ch="
 FOR /l %%L IN (2048,-1,0) DO IF NOT DEFINED ch (
  set ch=!OFileName:~%%L,1!
  IF DEFINED ch SET /a ch=1+%%L&echo name "!OFileName!" consists of !ch! chars
 )
 ENDLOCAL 
)
popd
GOTO :EOF

(虽然我改变了测试的目标目录......)

于 2013-09-09T15:30:11.863 回答