Batch Script - Strings

Introduction to Strings in Batch Scripting

Hello, aspiring programmers! Today, we're going to dive into the wonderful world of strings in Batch scripting. As your friendly neighborhood computer teacher, I'm excited to guide you through this journey. Don't worry if you've never written a line of code before – we'll start from the very beginning!

Batch Script - Strings

A string, in simple terms, is a sequence of characters. It could be a word, a sentence, or even a whole paragraph. In Batch scripting, we use strings all the time to display messages, store information, and manipulate text.

Basic String Operations

Displaying Strings

Let's start with the most basic operation – displaying a string. In Batch, we use the echo command for this.

@echo off
echo Hello, World!
pause

Save this as hello.bat and run it. You'll see "Hello, World!" displayed on your screen. The @echo off at the beginning is a good practice to keep your script output clean, and pause at the end keeps the window open so you can see the result.

Storing Strings in Variables

Now, let's store our string in a variable:

@echo off
set message=Hello, Batch scripting!
echo %message%
pause

Here, we're using the set command to store our string in a variable called message. To use the variable, we surround it with percent signs.

String Concatenation

Joining strings together (concatenation) is a common operation. In Batch, it's as simple as putting them next to each other:

@echo off
set firstName=John
set lastName=Doe
set fullName=%firstName% %lastName%
echo Full name: %fullName%
pause

This script will output "Full name: John Doe". Notice how we added a space between %firstName% and %lastName% to separate them.

Working with User Input

Let's make our scripts interactive by getting input from the user:

@echo off
set /p name=What's your name? 
echo Nice to meet you, %name%!
pause

The /p switch with set allows us to prompt the user for input. Try running this script and entering your name!

String Manipulation

Substring Extraction

Batch allows us to extract parts of a string using the %variable:~start,length% syntax:

@echo off
set message=Hello, World!
echo %message:~0,5%
echo %message:~7,5%
pause

This will output:

Hello
World

The first echo extracts characters from position 0 to 5, and the second from position 7 to 5 characters after that.

String Replacement

We can also replace parts of a string:

@echo off
set phrase=The quick brown fox jumps over the lazy dog
echo %phrase:fox=cat%
pause

This replaces "fox" with "cat" in our phrase.

Advanced String Operations

String Length

Batch doesn't have a built-in function to get string length, but we can use a clever workaround:

@echo off
set string=Hello, World!
call :strLen string strlen
echo The length of "%string%" is %strlen%
pause
goto :eof

:strLen
setlocal enabledelayedexpansion
set "s=!%~1!"
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
  if "!s:~%%N,1!" neq "" (
    set /a "len+=%%N"
    set "s=!s:~%%N!"
  )
)
endlocal & set %~2=%len%
goto :eof

This script defines a function :strLen that calculates the length of a string. It's a bit complex, but it shows how we can create powerful tools in Batch.

Case Conversion

Batch doesn't have built-in case conversion, but we can use the for command with /L option to achieve this:

@echo off
set string=Hello, World!
call :toUpper string result
echo Upper case: %result%
call :toLower string result
echo Lower case: %result%
pause
goto :eof

:toUpper
for %%a in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do call set %2=%%%2:%%a=%%a%%%
goto :eof

:toLower
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do call set %2=%%%2:%%A=%%a%%%
goto :eof

This script defines two functions: :toUpper and :toLower for case conversion.

Conclusion

Congratulations! You've just taken your first steps into the world of string manipulation in Batch scripting. Remember, practice makes perfect, so don't be afraid to experiment with these concepts. Try combining different techniques we've learned today to create your own unique scripts.

As we wrap up, here's a table summarizing the string operations we've covered:

Operation Syntax Example
Display String echo string echo Hello, World!
Store String set variable=string set message=Hello
Concatenate set result=%string1% %string2% set fullName=%firstName% %lastName%
User Input set /p variable=prompt set /p name=What's your name?
Substring %variable:~start,length% %message:~0,5%
Replace %variable:old=new% %phrase:fox=cat%

Keep coding, keep learning, and most importantly, have fun with Batch scripting!

Credits: Image by storyset