Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8874861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:49:40+00:00 2026-06-14T18:49:40+00:00

When I was testing a script I came across this issue when trying to

  • 0

When I was testing a script I came across this issue when trying to extract characters from a string using batch. I have simplified it into a simple example. t.txt just contains the word hello.

@echo off
setlocal enabledelayedexpansion
set a=0
set b=1
for /f %%a in (t.txt) do (
set x=%%a
echo !x:~!a!,!b!!
set /a x+=1
)
pause >nul

The problem is, the variable x needs to be accessed using delayed expansion, and because I am updating the values of a and b through the loop these also need to be accessed using delayed expansion.

When trying to use the variables a and b to split the string they all need delayed expansion, but the order of the ! marks means that it is not parsed the way I intended!

CMD will expand my command as !x:~!, !,! and !!, instead of expanding the inner ones first. Obviously I can’t use %‘s either.

The only way I have found to get around this is to call an external function that isn’t in the loop, so I can use %‘s.

@echo off
setlocal enabledelayedexpansion
set a=0
set b=1
set v=
for /f %%a in (t.txt) do (
set x=%%a
call :RETURN x
set /a x+=1
)
pause >nul

:RETURN
set v=%1
echo %v:~!a!,!b!%

Is there any way of getting cmd to parse my command how I need it to, or this just a limitation I will have to use call for?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:49:41+00:00Added an answer on June 14, 2026 at 6:49 pm

    Simply transfer variables a and b to FOR variables.

    @echo off
    setlocal enabledelayedexpansion
    set a=0
    set b=1
    for /f %%a in (t.txt) do (
      set "x=%%a"
      for /f "tokens=1,2" %%A in ("%a% %b%") do echo !x:~%%A,%%B!
      REM this line makes no sense if x=hello: set /a x+=1
    )
    pause >nul
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For testing purposes I have this shell script #!/bin/bash echo $$ find / >/dev/null
For automated testing (using Hudson) I have a script that generates a bunch of
I'm trying to access an FTP server from my PHP script using Codeigniter's FTP
I came across a weird issue when using JVM garbage collection log option with
I am writing a testing script using Ruby and Rails. I have to validate
I am using Windows 2008 r2 64 bit system. While testing our script ,we
How can I run my perl CGI script without apache? This is for testing
I'm currently testing a script on my host. This script is to accept PayPal
I have a testing script that needs to open a process (a Pyro server),
I am trying to create a testing script in Selenium and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.