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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:50:08+00:00 2026-06-17T06:50:08+00:00

In Windows, how do you access arguments passed when a batch file is run?

  • 0

In Windows, how do you access arguments passed when a batch file is run?

For example, let’s say I have a program named hello.bat. When I enter hello -a at a Windows command line, how do I let my program know that -a was passed in as an argument?

  • 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-17T06:50:09+00:00Added an answer on June 17, 2026 at 6:50 am

    As others have already said, parameters passed through the command line can be accessed in batch files with the notation %1 to %9. There are also two other tokens that you can use:

    • %0 is the executable (batch file) name as specified in the command line.
    • %* is all parameters specified in the command line — this is very useful if you want to forward the parameters to another program.

    There are also lots of important techniques to be aware of in addition to simply how to access the parameters.

    Checking if a parameter was passed

    This is done with constructs like IF "%~1"=="", which is true if and only if no arguments were passed at all. Note the tilde character which causes any surrounding quotes to be removed from the value of %1; without a tilde you will get unexpected results if that value includes double quotes, including the possibility of syntax errors.

    Handling more than 9 arguments (or just making life easier)

    If you need to access more than 9 arguments you have to use the command SHIFT. This command shifts the values of all arguments one place, so that %0 takes the value of %1, %1 takes the value of %2, etc. %9 takes the value of the tenth argument (if one is present), which was not available through any variable before calling SHIFT (enter command SHIFT /? for more options).

    SHIFT is also useful when you want to easily process parameters without requiring that they are presented in a specific order. For example, a script may recognize the flags -a and -b in any order. A good way to parse the command line in such cases is

    :parse
    IF "%~1"=="" GOTO endparse
    IF "%~1"=="-a" REM do something
    IF "%~1"=="-b" REM do something else
    SHIFT
    GOTO parse
    :endparse
    REM ready for action!
    

    This scheme allows you to parse pretty complex command lines without going insane.

    Substitution of batch parameters

    For parameters that represent file names the shell provides lots of functionality related to working with files that is not accessible in any other way. This functionality is accessed with constructs that begin with %~.

    For example, to get the size of the file passed in as an argument use

    ECHO %~z1
    

    To get the path of the directory where the batch file was launched from (very useful!) you can use

    ECHO %~dp0
    

    You can view the full range of these capabilities by typing CALL /? in the command prompt.

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

Sidebar

Related Questions

I have a silverlight control, for arguments sake let's say it inherits from the
CIFS is common internet file system, which is a protocol used to access windows
REWORK FOR REOPEN: How can I have access to Windows Explorer FTP programmatically via
I have NFS installed for Windows 2008 but when I try to access C:/Windows/System32/mount.exe
To access the properties of windows controls in multi threaded application, I have modified
I want to access .sdf file from windows ce mobile to my desktop application
I have a c# program that access a java program by tcp. In my
I have the following code to import a delimited file into an Access 2003
I would like to disable the parent windows access of scrollbars when the fancybox
I am using Windows XP to access another Windows XP via Remote Desktop. I

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.