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›File Operations

File Operations

File copying, moving, deleting, and redirecting output to a file.

Simple enough for a 5-year-old to understand

Basic file commands

Example code
1
copy notes.txt backup.txt
Copies.
CodeCopies.
2
move notes.txt archive\
Moves to another folder.
CodeMoves to another folder.
3
del backup.txt
Deletes.
CodeDeletes.
4
dir
Lists the files in the folder.
Output / InputLists the files in the folder.
CodeOutput / Input
💡 Hint: del is irreversible; think twice before deleting.

🎯 Exercise & Practice

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

1.Which command deletes a file?

About this topic

File operations are the primary use case for batch files: copying, moving, deleting, creating folders, and redirecting output to a file. These commands are the operating system's own tools.

The most important rule is to use quotes. Spaces are common in Windows paths, and an unquoted path is parsed as multiple arguments. This can lead to results as severe as deleting the wrong file.

The second important rule is that relative paths are resolved not based on the script's folder, but on the folder of the window running the script. To access a file in the same folder as the script, the special variable providing the script's own path must be used.

What you will learn

  • Copy and move commands
  • Delete and folder operations
  • Output redirection
  • Using quotes
  • Relative access based on the script folder

Who is this for?

  • Those writing backup scripts
  • Those automating installation steps
  • Those dealing with path errors

When to use?

When accessing files distributed alongside the script, the special variable that always provides the script's own folder should be used.

If deletion and recursive folder removal commands receive their path from a variable, it must first be checked that the variable is not empty; an empty path can lead to deleting an unexpected target.

Common mistakes

Wrong

@echo off
copy C:\Program Files\data.txt D:\backup\

Because there is a space in the path, the command thinks the path is two separate arguments and looks for the wrong file. Since spaces are normal in Windows paths, every path must be enclosed in quotes; this is a rule that must be applied without exception in file operations.

Correct

@echo off
copy "C:\Program Files\data.txt" "D:\backup\"

Wrong

@echo off
copy settings.ini "C:\app\"

The relative path is resolved not according to the folder where the script is located, but according to the current folder of the command window; when the script is called from another folder, the file cannot be found. To access files that come with the script, the special variable giving the script's own folder must be used.

Correct

@echo off
copy "%~dp0settings.ini" "C:\app\"

Wrong

@echo off
set hedef=%1
rd /s /q "%hedef%"
echo Deleted

If no argument is provided, the variable remains empty and the recursive delete command may target an unexpected path; there is a risk of irreversible data loss. Before destructive commands are executed, it must be verified that the target is not empty and actually exists.

Correct

@echo off
set hedef=%~1
if "%hedef%"=="" (
  echo Target folder not specified
  exit /b 1
)
if not exist "%hedef%" (
  echo Folder not found: %hedef%
  exit /b 1
)
rd /s /q "%hedef%"
echo Deleted

Frequently asked questions

How do I write the output to a file?

A single greater-than sign overwrites the file and writes, while a double sign appends to the end. Error output is a separate stream and is redirected separately; to capture both together, a special syntax combining the two streams is used.

How do I get the script's own folder?

A special syntax is used that gives the drive and path components of the zeroth argument. The result ends with a backslash, so you can append the file name directly. It is the only syntax that completely solves relative path issues.

Which command is more suitable for multi-file copying?

There are commands that can copy a folder tree recursively and skip unchanged files; they are significantly more capable than the simple copy command and are the standard choice for backup tasks.

Summary

Every path is enclosed in quotes.

Relative paths are resolved relative to the window's folder.

The script folder is obtained with a special syntax.

The target is checked before destructive commands.

Output and error streams are redirected separately.

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