Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Batch Script - Right String
Next article icon

Batch Script - String length

Last Updated : 04 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article , we are going to learn how to find length of any String using Batch Script.

Batch Script :

@echo off  set str=Geeks For Geeks  call :strLen str strlen  echo String is %strlen% characters long  pause  exit /b    :strLen  setlocal enabledelayedexpansion    :strLen_Loop     if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop  (endlocal & set %2=%len%)  goto :eof

In Batch Scripting there no function for checking length of string ,So we will create a function to find length of string.

Explanation :

  • By using ' set ' we are getting our input string whose length is to calculated.
  • In above code we are creating a Function "strLen" , which we can use to find our string length.
  • From ':strLen' our function is started , then we will initialize our Loop with 'strLen_Loop'.
  • ("!%1:~%len%!") this statement is checking that our string is ended or not by using (=="").
if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
  • During execution of our 'if' statement its checking if ("!%1:~%len%!"=="") this argument is True then it will Break the Loop and if its False , it will be continued.
  • Now if our argument is False then we will set our 'len' variable as 'len=len+1' (also written as len+=1).
  • By using 'goto :strLen_Loop' we are continuing our Loop.
  • Now , again it will check whether our string is ended or not by using command ("!%1:~%len%!"=="") and increase 'len' by 1 if argument is False.
  • Now let's assume when our argument is True , loop will break and our string length is set in 'strlen'.
  • When 'strlen' is called it will print our string length.

Output:


Next Article
Batch Script - Right String
author
thenavneet
Improve
Article Tags :
  • Linux-Unix
  • Batch-script

Similar Reads

  • Batch Script - Left String
    In this article, we are going to study Left string in Batch Script. In Batch Script, the Left string is used to extract the characters from the beginning of the string by giving position 0 and a length using:~ while expanding a variable content. Example 1: Batch script set str=GeeksForGeeks echo.%st
    1 min read
  • Batch Script - Right String
    In this article, we are going to learn how to use the concept of Right String using Batch Script. Right String uses the concept of negative indexing. We have to extract the characters using :~ followed by the negative index of the character from which we want to print a substring from the main strin
    2 min read
  • Batch Script - Empty String
    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" pauseExplana
    1 min read
  • Batch Script - Create String
    In this article, we are going to learn how to create a String using Batch Script. Batch Script :@echo off set str=Geeks for Geeks echo %str% pauseBy using ' set ' we are getting input of any string.Ex: set str=input stringIn the next line, we are using ' echo %str% ' for printing our input string.Th
    1 min read
  • Batch Script - Strings
    A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can u
    4 min read
  • Batch Script - Mid String
    In this article , we are going to learn how to use the concept of Mid String using Batch Script. Using the concept of 'Mid String' we are extracting out a sub string between two indices of any given string. Batch Script :@echo off set str=GeeksforGeeks echo %str% set str=%str:~5,-5% echo %str% pause
    2 min read
  • Batch Script - Replace a String
    In this article, we are going to Replace a substring with any given string. Batch Script :@echo off set str=GFG is the platform for geeks. echo %str% set str=%str:the=best% echo %str% pause In the above example, we are going to replace 'the' by substring 'best' using %str:the=best% statement. Explan
    1 min read
  • Batch Script - String Concatenation
    String Concatenation is combining two or more strings to create a new string. For instance, let a string "Good" and take another string "Morning" now by string concatenation i.e. "Good" + "Morning" and we got new string i.e. "Good Morning". This is string concatenation. Let see some examples of stri
    2 min read
  • Batch Script - String Interpolation
    String interpolation is a technical way of saying parsing a value that might be a variable into a string. It is an important part of dealing with the application that contains texts in some way to make them dynamic and programmatic. In batch scripting, we can perform string interpolation as well as
    2 min read
  • Batch Script - toInt
    In the Batch script, every variable is considered as a string and optionally integers. We need numbers all the time especially when it comes to performing system operations. We might extract numbers from a string variable but performing operations on them are not the same and require some additional
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences