2012-02-21 46 views
0

我基本上需要測試一個變量在批處理文件中的時間。例如:如何批量測試var的長度?

@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. 
+0

我不是批處理腳本,但Google給出了這個:http:// fo rums.afterdawn.com/thread_view.cfm/514228 – Ryan 2012-02-21 04:11:20

+2

是不是字符串「批處理文件」11個字符長? – 2012-02-21 04:55:15

回答

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