例如我有一个foldet测试和一些文件:test1.txt,test12.txt,test123.txt ...我想要一个批处理程序来计算这些文件名中的字符并在屏幕上输出如 - name“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
请帮忙!提前谢谢;)
答案 0 :(得分: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
答案 1 :(得分: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
(虽然我更改了目标目录以进行测试......)