The Wayback Machine - https://web.archive.org/web/20240213201113/https://www.geeksforgeeks.org/batch-script-empty-string/
Open In App
Related Articles

Batch Script – Empty String

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we are going to create an Empty String using Batch Script.

Batch Script :

@echo off 
set str1=
set str2=Hello

::using if statement we will check that str1 is empty string or not.

if [%str1%]==[] echo "str1 is an empty string"  
if [%str2%]==[] echo "str2 is an empty string"


pause

Explanation :

  • By using ‘ set ‘ we are getting our input string.
    • str1 with no value (i.e Empty string)
    • and str2 as ‘Hello’
  • Now using the if statement we will check whether str1 is an empty string or not.

if [%str1%]==[] echo “str1 is an empty string”  :: parenthesis is insecure always, so always use square brackets.

  • A double colon (::) is used to add any comment in any batch script.
  • Then we are using ‘pause’, to hold the screen until any key is pressed, so that we can read our output.

as we can see our str1 is empty string

As we can clearly see that ‘str1 is an empty string’ is printed as output which means our if statement is True for the str1 argument. Hence str1 is an empty string.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Last Updated : 19 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials