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

  • Home
  • SEARCH
  • 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 1067187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:07:25+00:00 2026-05-16T20:07:25+00:00

I have a variable assignment problem inside the DOS script for loop. It never

  • 0

I have a variable assignment problem inside the DOS script for loop. It never assigns the value, its always blank. Below the sample code

@echo off
set ans=%1
SET STRING=%ans:"=%

echo Parsing the string "%STRING%":
for /f "tokens=1-2" %%a in ("%STRING%") do (
 set Word1 =%%a
 echo Word 1: %%a
 echo Word 1: %Word1%
 set Word2 =%%b
 if %%b.==. (Set server =\\.\pipe\mssql$microsoft##ssee\sql\query ) else (Set server =%%b)
)
echo Server name "%server%"
sqlcmd -s %server%

value of %%a is not assigned to variable Word1. But when i echo the %%a, it shows the correct value. As well in the last empty value check if condition, the server variable never set. I am very confused here. can someone help me out??

P.S: input to the script is any 2 word string (ex: a.bat “l dev-server”)

  • 1 1 Answer
  • 1 View
  • 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-05-16T20:07:26+00:00Added an answer on May 16, 2026 at 8:07 pm

    You need to use delayed expansion – !Word1! instead of %Word1%.

    By default, when the shell first reads the statement, all variables are substituted with their current values, and the modified statement is used every time that line is hit. This is simply how DOS works, and it’s kept that way in the Windows shell for backwards compatibility.

    Delayed expansion, on the other hand, reinserts the value every time the statement is hit. That will give you the desired result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable: char date[11]; , and I need to put inside it
I have a problem with Django. My error message: UnboundLocalError at /city/city local variable
I have a problem with getting a new value of an object. I have
I have run in to a stupid problem... I declared a new variable called
I have solved the problem myself now, below is the solution: No problem any
I have variable in the parent document eg. $var = 'blah'; Then a div
I have variable WCHAR sDisplayName[1024]; How can I check if sDisplayName contains the string
I have variable which I populate from JSON formated data. Something like: var time=my_data[data.results[i].id].time;
I have variable @count of datatype int.I am setting values to this @count. I
i have variable $lang which convert string to the selected language but i am

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.