HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to MS-DOS BatchVariablesConditions (IF)Loops (FOR)File OperationsParameters and Subroutines
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔀 Condition
🔁 Loop
⚙️ Commands & Flow
MS-DOS›Parameters and Subroutines

Parameters and Subroutines

Passing external values to a file, branching with GOTO, and subroutines with CALL.

Simple enough for a 5-year-old to understand

Receive parameters from outside

You access the values you write next to the batch file while running it sequentially as percent one, percent two.

Example code
1
@echo off
2
echo First parameter: %1
The first given value.
Output / InputThe first given value.
3
echo Second parameter: %2
The second given value.
Output / InputThe second given value.
Output / Input
💡 Hint: The file name itself is retrieved with percent zero.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How to access the first value given externally to the batch file?

About this topic

Passing external values to a batch file is done using percentage-signed numbers: the first argument, the second argument, and so on. The zeroth number gives the name of the script itself.

Directly up to nine arguments can be accessed; for more, a command that shifts the arguments is used. There is a separate syntax to get all arguments collectively, which is useful when passing the call to another program.

Argument syntax has modifier modifiers: such as removing quotes, getting only the filename, making the path full. These modifiers are the most useful and least known feature of batch files.

What you will learn

  • Accessing arguments
  • The quote removal modifier
  • Missing argument checking
  • Argument shifting
  • Path component modifiers

Who is this for?

  • Reusable script writers
  • Those who get errors in calls with arguments
  • Those who call their scripts from other scripts

When to use?

If the script will be called by other scripts or scheduled tasks, an existence check must be written for each argument and an explanatory message must be given in case of misuse.

The quote-removing modifier should be used for paths that the user might pass in quotes; otherwise, nested quotes will form with your own quotes.

Common mistakes

Wrong

@echo off
echo File: %1
copy "%1" "D:\backup\"

If the user passes the path in quotes, the argument comes with its quotes and nested quotes form with your own quotes; the command cannot find the path. Using the modifier that removes quotes makes both quoted and unquoted calls work correctly together.

Correct

@echo off
echo File: %~1
copy "%~1" "D:\backup\"

Wrong

@echo off
set kaynak=%~1
copy "%kaynak%" "D:\backup\"
echo Copied

When no argument is given, the variable remains empty, copying fails, but the script continues to write a success message. Every script working with arguments must have a missing argument check and an appropriate exit code.

Correct

@echo off
set kaynak=%~1
if "%kaynak%"=="" (
  echo Usage: %~nx0 ^<source-file^>
  exit /b 1
)
copy "%kaynak%" "D:\backup\"
if errorlevel 1 (
  echo Copy failed
  exit /b 1
)
echo Copied

Wrong

@echo off
echo 1: %1
echo 10: %10

There is no such thing as a two-digit argument number; this syntax is read as the first argument appended with a zero character. To access arguments after nine, it is necessary to use the shift command.

Correct

@echo off
echo 1: %1
shift
shift
shift
shift
shift
shift
shift
shift
shift
echo 10: %1

Frequently asked questions

What are the modifier extensions?

Alongside quote removal, there are modifiers that give the drive letter, folder path, filename, extension, and full path components. Several can be combined. These modifiers are the shortest way to break down a file path into its components.

What does the shift command do?

Shifts the arguments one position to the left: the second argument becomes the first. By using it in a loop, you can process an unlimited number of arguments. Since it also shifts the zeroth number, it is necessary to save the script name beforehand.

How do I pass all arguments together?

The asterisk-marked syntax gives all arguments as they are. It is ideal for wrapper scripts passing the call to another program; however, since it does not process quotes, if you need to break them down into components, reading them one by one with shift is safer.

Summary

Arguments are read with percentage-signed numbers.

The quote-removing modifier must always be used.

There is no two-digit argument number.

Anything after nine is read by shifting.

Missing argument check is mandatory.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code