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 4033714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:52:53+00:00 2026-05-20T11:52:53+00:00

I’m trying to loop through the arguments that I am passing to a batch

  • 0

I’m trying to loop through the arguments that I am passing to a batch file. Based on the argument, I want to set a variable flag true or false for use later in the script

So my command is “myscript.bat /u /p /s”

And my code is:

FOR /f %%a IN ("%*") DO (
  IF /I "%%a"=="/u" SET UPDATE=Y
  IF /I "%%a"=="/p" SET PRIMARY=Y
  IF /I "%%a"=="/s" SET SECONDARY=Y
)

It only works if i have a single argument, which tells me that it is getting the entire list of arguments as a single argument. I’ve tried “delims= ” but to no avail. Any thoughts on getting each spaced argument?


What about adding a value to one of the params?

myscript.bat /u /p /d TEST /s

:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
IF /I "%~1"=="/d" SHIFT & SET DISTRO="%~1"
SHIFT & GOTO loop

:cont

But the SHIFT that comes inline with the last IF doesn’t actually shift anything. DISTRO ends up being “/d” instead of “TEST”

  • 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-05-20T11:52:54+00:00Added an answer on May 20, 2026 at 11:52 am

    You could loop over the arguments using SHIFT, GOTO and an extra IF to check if there are no more parameters to parse:

    :loop
    IF "%~1"=="" GOTO cont
    IF /I "%~1"=="/u" SET UPDATE=Y
    IF /I "%~1"=="/p" SET PRIMARY=Y
    IF /I "%~1"=="/s" SET SECONDARY=Y
    SHIFT & GOTO loop
    
    :cont
    ...
    

    UPDATE (addressing the case when a parameter has an argument of its own)

    The SHIFT in the IF statement that checks for /d does work. The issue is that the entire line is evaluated at once and both instances of %~1 get replaced with the same value, which is /d at that point.

    So, basically the solution in this case would be to cause the interpreter to evaluate the SET DISTRO="%~1" part separately from the IF /I "%~1"=="/d". There can be various approaches to this. For instance, you could simply move SHIFT & SET DISTRO="%~1" to the next line and skip it if %~1 is not /d:

    ...
    IF /I NOT "%~1"=="/d" GOTO skip_d
    SHIFT & SET "DISTRO=%~1"
    :skip_d
    ...
    

    Another method could be to assign a special value (e.g. a ?) to DISTRO and shift when /d is encountered. Then, on the next line, check if DISTRO has that special value and set it to %~1:

    ...
    IF /I "%~1"=="/d" SHIFT & SET DISTRO=?
    IF "%DISTRO%"=="?" SET "DISTRO=%~1"
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.