HOWTO · Batch

How to Extract or Unzip File in Batch Script

Learn how to extract or unzip files in batch scripts with this comprehensive tutorial. Discover methods using Windows built-in tools and 7-Zip for efficient file management. Master batch scripting to automate your workflow and save time on repetitive tasks. Perfect for beginners and experienced users alike!

On this page

To extract a ZIP archive from a Windows batch file, set the archive and destination paths, then call a command-line extractor. Windows tar is a built-in option on supported Windows 10 and later systems; 7-Zip is an installed alternative. This guide shows the commands, explains a Windows Shell and VBScript route, and outlines how to process several archives safely. The command examples are Windows-specific and are shown as syntax; they have not been executed in this packet.

Extract a ZIP file with Windows tar

On supported Windows 10 and later systems, tar can extract ZIP archives without installing another tool. Microsoft documents the Windows tar command and its availability here. In this batch example, zipFile is C:\Work\sample.zip and outputDir is C:\Work\sample-extracted; replace both placeholder paths with your own. The script creates the destination if needed, then runs tar -xf "%zipFile%" -C "%outputDir%".

The -x option extracts, -f names the archive, and -C selects the directory where its contents appear. The destination directory is created first if needed. Quoting the variables keeps spaces in paths together as one argument. This example covers ZIP archives; do not assume tar is available on older Windows installations or that this command is appropriate for every archive format.

Save this as a .bat file and run it on a supported Windows installation:

@echo off
setlocal
set "zipFile=C:\Work\sample.zip"
set "outputDir=C:\Work\sample-extracted"

if not exist "%zipFile%" (
    echo ZIP archive not found: "%zipFile%"
    exit /b 1
)
if not exist "%outputDir%" (
    mkdir "%outputDir%"
    if errorlevel 1 (
        echo Could not create destination: "%outputDir%"
        exit /b 1
    )
)

tar -xf "%zipFile%" -C "%outputDir%"
exit /b %errorlevel%

For a portable demonstration of the extraction result using Python’s standard library, run this self-contained example. It creates a small ZIP archive in a temporary directory, extracts it, and checks the extracted file without relying on an external archive or fixed path:

from pathlib import Path
from tempfile import TemporaryDirectory
from zipfile import ZipFile

with TemporaryDirectory() as temp_dir:
    root = Path(temp_dir)
    archive = root / "sample.zip"
    destination = root / "extracted"
    destination.mkdir()

    with ZipFile(archive, "w") as zip_file:
        zip_file.writestr("notes/readme.txt", "Batch extraction demo")

    with ZipFile(archive) as zip_file:
        zip_file.extractall(destination)

    assert (destination / "notes" / "readme.txt").read_text() == "Batch extraction demo"

The Python example demonstrates the source archive and destination relationship; it is not a Windows batch command and does not establish which Windows versions provide tar. The Windows tar command above has not been executed here. When it runs successfully, the archive contents appear under the directory assigned to outputDir.

Extract a ZIP file with 7-Zip

Use 7-Zip when it is installed or when you need its support for formats beyond ZIP. This batch example uses C:\Program Files\7-Zip\7z.exe for sevenZipPath, C:\Work\sample.zip for zipFile, and C:\Work\sample-extracted for outputDir; change them to match your installation and files. The official 7-Zip command-line documentation describes its commands and switches. The script creates the destination if needed, then runs "%sevenZipPath%" x "%zipFile%" -o"%outputDir%".

The x command extracts while preserving the archive’s directory structure, and -o specifies the destination; keep it attached to the path as shown. Install 7-Zip first, or change sevenZipPath to match its installation. The extracted files appear under outputDir. Save this as a .bat file. This Windows command example has not been executed here; if later batch steps depend on completed extraction, check the command’s exit status and account for any files the archive may have written before reporting success.

@echo off
setlocal
set "sevenZipPath=C:\Program Files\7-Zip\7z.exe"
set "zipFile=C:\Work\sample.zip"
set "outputDir=C:\Work\sample-extracted"

if not exist "%sevenZipPath%" (
    echo 7-Zip executable not found: "%sevenZipPath%"
    exit /b 1
)
if not exist "%zipFile%" (
    echo ZIP archive not found: "%zipFile%"
    exit /b 1
)
if not exist "%outputDir%" (
    mkdir "%outputDir%"
    if errorlevel 1 (
        echo Could not create destination: "%outputDir%"
        exit /b 1
    )
)

"%sevenZipPath%" x "%zipFile%" -o"%outputDir%"
exit /b %errorlevel%

Use Windows Shell and VBScript from a batch file

A Windows batch file can call Windows Script Host to use the Shell automation object. Microsoft documents the Shell Folder.CopyHere method here. The following .bat example writes a complete temporary VBScript file, passes the destination directory first and the source ZIP archive second, and removes the temporary file after cscript returns. The sample paths are C:\Work\sample.zip and C:\Work\sample-extracted; replace them before running. The script checks whether Windows Shell can open each namespace and reports which path is unavailable.

@echo off
setlocal
set "zipFile=C:\Work\sample.zip"
set "outputDir=C:\Work\sample-extracted"
set "scriptFile=%TEMP%\extract-zip-%RANDOM%.vbs"

if not exist "%zipFile%" (
    echo ZIP archive not found: "%zipFile%"
    exit /b 1
)
if not exist "%outputDir%" (
    mkdir "%outputDir%"
    if errorlevel 1 (
        echo Could not create destination: "%outputDir%"
        exit /b 1
    )
)

> "%scriptFile%" (
    echo Dim shell, destination, source
    echo Set shell = CreateObject^("Shell.Application"^)
    echo Set destination = shell.NameSpace^(WScript.Arguments^(0^)^)
    echo If destination Is Nothing Then
    echo     WScript.Echo "Destination folder is unavailable: " ^& WScript.Arguments^(0^)
    echo     WScript.Quit 2
    echo End If
    echo Set source = shell.NameSpace^(WScript.Arguments^(1^)^)
    echo If source Is Nothing Then
    echo     WScript.Echo "ZIP archive namespace is unavailable: " ^& WScript.Arguments^(1^)
    echo     WScript.Quit 3
    echo End If
    echo destination.CopyHere source.Items
)
if errorlevel 1 (
    echo Could not write temporary VBScript: "%scriptFile%"
    exit /b 1
)

cscript //nologo "%scriptFile%" "%outputDir%" "%zipFile%"
set "scriptStatus=%errorlevel%"
del "%scriptFile%" >nul 2>&1
if not "%scriptStatus%"=="0" (
    echo Windows Script Host returned status %scriptStatus%.
    exit /b %scriptStatus%
)
echo CopyHere was requested; check the destination separately before using extracted files.
exit /b 0

This route relies on Windows Script Host and Shell ZIP handling. In the temporary VBScript, argument 0 is the destination folder and argument 1 is the source ZIP archive. CopyHere may return before the file copy finishes, so a successful cscript return is not proof that extraction has completed. The batch message only confirms that the copy request returned; if later commands move, delete, or read extracted files, add a separate completion check appropriate to the archive and destination. This Windows-specific technique is shown as an unexecuted example.

Automate extraction of multiple ZIP files

Loop over the source directory’s ZIP files and give each archive its own destination directory. Separate destinations prevent files with the same name from different archives from overwriting one another. This batch file sets illustrative paths, checks the source directory, creates the output root, and reports directory-creation or extraction failures. It exits with status 1 if any archive fails or if no ZIP files are found.

@echo off
setlocal
set "sourceDir=C:\Work\incoming"
set "outputRoot=C:\Work\extracted"
set "found=0"
set "failed=0"

if not exist "%sourceDir%\" (
    echo Source directory not found: "%sourceDir%"
    exit /b 1
)
if not exist "%outputRoot%\" (
    mkdir "%outputRoot%"
    if errorlevel 1 (
        echo Could not create output root: "%outputRoot%"
        exit /b 1
    )
)

for %%F in ("%sourceDir%\*.zip") do if exist "%%~fF" call :extract "%%~fF" "%%~nF"

if "%found%"=="0" (
    echo No ZIP files found in: "%sourceDir%"
    exit /b 1
)
if "%failed%"=="1" exit /b 1
echo All ZIP files were extracted successfully.
exit /b 0

:extract
set "found=1"
set "destination=%outputRoot%\%~2"
if not exist "%destination%\" (
    mkdir "%destination%"
    if errorlevel 1 (
        echo Could not create destination for "%~1": "%destination%"
        set "failed=1"
        exit /b 0
    )
)

tar -xf "%~1" -C "%destination%"
if errorlevel 1 (
    echo Extraction failed for: "%~1"
    set "failed=1"
) else (
    echo Extracted "%~1" to "%destination%"
)
exit /b 0

Here %%~fF expands to the full path of the current archive and %%~nF to its name without the .zip extension. Each archive is extracted into a correspondingly named subdirectory under outputRoot. A missing source directory or an output-root creation failure stops the script immediately with a nonzero status. A per-archive destination or tar failure prints the affected path, marks the batch run as failed, and allows later archives to be attempted; the final status is nonzero if any such failure occurred. If no ZIP files match, the script prints a message and exits nonzero. Within an interactive Command Prompt, use %F rather than %%F; doubled percent signs are for a batch file. The tar command example is Windows-specific and has not been executed here.

Troubleshoot batch ZIP extraction

  • The command is not found: Check that tar is available on that Windows installation. For 7-Zip, confirm that sevenZipPath points to 7z.exe and that 7-Zip is installed. On older Windows versions without the required tar, use an installed extractor such as 7-Zip.
  • A path containing spaces is split: Quote the archive and destination paths as in the examples. For 7-Zip, retain the -o prefix directly before the quoted destination path when quoting it, such as -o"C:\Work Files\Extracted".
  • The destination cannot be opened or created: Ensure its parent exists and that your account can write there. Create the destination before extraction, and choose a folder rather than a file path.
  • Files are missing or extraction appears incomplete: Confirm that the source path names the intended ZIP archive, that it is a ZIP file, and that the archive can be opened. Check the extractor’s exit status. With the Shell/VBScript CopyHere approach, allow for asynchronous completion before using the extracted files.
  • Several archives contain identically named files: Extract each archive to a separate directory as in the loop pattern, rather than sending all contents to one shared destination.
  • The command behaves differently in another shell: These batch syntax examples target Windows Command Prompt (CMD). They are not Bash or PowerShell syntax and should not be pasted unchanged into those shells.