如何批量测试var的长度?

时间:2012-02-21 04:09:37

标签: batch-file string-length

我基本上需要测试变量在批处理文件中的时间。例如:

@echo off
set /p someVar=""
//some code to figure out how long it is
cls
echo someVar is %length% characters long.
pause
exit

所以,如果我输入

Batch files

它会说

someVar is 10 characters long.

1 个答案:

答案 0 :(得分:1)

根据@minitech提供的链接,这是一个应该有效的脚本:

@echo off
set /p someVar=""

::some code to figure out how long it is

::Start by writing the file to a temp file
echo %someVar%>"%temp%\tmp.txt" 

:: get the files size in bytes and subtract 3 for the trailing space and newline and delete the temp file
for %%a in (%temp%\tmp.txt) do set /a length=%%~za & set /a length -=3 & del "%temp%\tmp.txt"

cls

echo someVar is %length% characters long.

pause

exit