Sentry Answers>Windows>

Pass arguments to a Windows batch script

Pass arguments to a Windows batch script

David Y.

The Problem

How can I write a batch script that takes arguments from the command line?

The Solution

Batch scripts can access up to nine arguments from the command line using the syntax %1 to %9. For example, the script below will print out three provided arguments, each on their own line:

Click to Copy
@echo off echo %1 echo %2 echo %3

If we save this script as echothree.bat, we can execute it as follows:

Click to Copy
echothree.bat One Two Three

This will produce the following output:

Click to Copy
One Two Three

%0 contains the name of the script and %* contains all specified arguments – this is useful if we want to allow a variable number of arguments, or if we want to pass all of the arguments to another script or program. For example, this script would produce the command used to execute it:

Click to Copy
@echo off echo %0 %*

For longer scripts, we can create variables containing our argument values as follows:

Click to Copy
@echo off set name=%1 set type=%2 echo Processing %name% of %type% ...

When dealing with numeric values rather than strings, we need to pass the /A flag to set:

Click to Copy
@echo off set /A a = %1 set /A b = %2 set /A sum = %a% + %b% echo %sum%

If we need to handle more than nine arguments, we can use the shift command. This command moves all argument values back by one – %1 is moved to %0, %2 is moved to %1, and the previously inaccessible tenth argument is moved to %9. We could also use this method to consume all arguments as %1, which may be useful when creating scripts that take arguments in any order (e.g. arguments with flags like -a and -b).

We could use shift to rewrite echothree.bat as follows:

Click to Copy
@echo off echo %1 shift echo %1 shift echo %1 shift

Note that calling shift will not affect the value of %*.

  • Syntax.fmListen to the Syntax Podcast
  • ResourcesWhat is Distributed Tracing
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark of Functional Software, Inc.