Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the string length in a batch file?

There doesn't appear to be an easy way to get the length of a string in a batch file. E.g.,

SET MY_STRING=abcdefg SET /A MY_STRING_LEN=??? 

How would I find the string length of MY_STRING?

Bonus points if the string length function handles all possible characters in strings including escape characters, like this: !%^^()^!.

like image 859
indiv Avatar asked Apr 29 '11 21:04

indiv


People also ask

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do I get the length of a string in PowerShell?

Use $string. Length to Get the String Length of a Variable in PowerShell. The $string. Length is the most straightforward method to check a string length of a variable in PowerShell.

How do I find the length of a string in bash?

We can use the # operator to get the length of the string in BASH, we need to enclose the variable name enclosed in “{ }” and inside of that, we use the # to get the length of the string variable. Thus, using the “#” operator in BASH, we can get the length of the string variable.

What is string in batch file?

Advertisements. In DOS, a string is an ordered collection of characters, such as "Hello, World!".


2 Answers

As there is no built in function for string length, you can write your own function like this one:

@echo off setlocal REM *** Some tests, to check the functionality *** REM *** An emptyStr has the length 0 set "emptyString=" call :strlen result emptyString echo %result%  REM *** This string has the length 14 set "myString=abcdef!%%^^()^!" call :strlen result myString echo %result%  REM *** This string has the maximum length of 8191 setlocal EnableDelayedExpansion set "long=." FOR /L %%n in (1 1 13) DO set "long=!long:~-4000!!long:~-4000!" (set^ longString=!long!!long:~-191!)  call :strlen result longString echo %result%  goto :eof  REM ********* function ***************************** :strlen <resultVar> <stringVar> (        setlocal EnableDelayedExpansion     (set^ tmp=!%~2!)     if defined tmp (         set "len=1"         for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (             if "!tmp:~%%P,1!" NEQ "" (                  set /a "len+=%%P"                 set "tmp=!tmp:~%%P!"             )         )     ) ELSE (         set len=0     ) ) (      endlocal     set "%~1=%len%"     exit /b ) 

This function needs always 13 loops, instead of a simple strlen function which needs strlen-loops.
It handles all characters.

The strange expression (set^ tmp=!%~2!) is necessary to handle ultra long strings, else it's not possible to copy them.

like image 134
jeb Avatar answered Sep 20 '22 06:09

jeb


You can do it in two lines, fully in a batch file, by writing the string to a file and then getting the length of the file. You just have to subtract two bytes to account for the automatic CR+LF added to the end.

Let's say your string is in a variable called strvar:

ECHO %strvar%> tempfile.txt FOR %%? IN (tempfile.txt) DO ( SET /A strlength=%%~z? - 2 ) 

The length of the string is now in a variable called strlength.

In slightly more detail:

  • FOR %%? IN (filename) DO ( ... : gets info about a file
  • SET /A [variable]=[expression] : evaluate the expression numerically
  • %%~z? : Special expression to get the length of the file

To mash the whole command in one line:

ECHO %strvar%>x&FOR %%? IN (x) DO SET /A strlength=%%~z? - 2&del x 
like image 27
Joshua Honig Avatar answered Sep 19 '22 06:09

Joshua Honig