Skip to main content

How to Create a TAR Archive in Batch Script

The TAR (Tape Archive) format is the standard for packaging files together in Unix/Linux environments. While ZIP compresses everything immediately, TAR is fundamentally just an uncompressed bundle of files stitched into a single file. Usually, it is compressed afterward into a .tar.gz.

Historically, Windows required third-party tools like 7-Zip to handle TAR files. However, modern Windows 10/11 natively includes the tar.exe utility, identical to the Linux version.

In this guide, we will demonstrate how to bundle folders into a .tar archive directly from a Batch script.

The Strategy: The Native tar Command

  1. Identify the source directory or files to bundle.
  2. Define the target .tar filename.
  3. Use the native tar.exe utility with the c (create) and f (file) flags.

Method 1: Creating an Uncompressed TAR File

Unlike a zip, this simply aggregates files without compressing them. It is useful for transferring massive datasets across a network where you care about grouping, but compression wastes too much CPU time.

@echo off
setlocal

:: Define paths
set "sourceDir=C:\Network\WebData"
set "archiveFile=C:\Backups\WebsiteBundle.tar"

:: Check if the source directory exists
if not exist "%sourceDir%\" (
echo [ERROR] Source directory "%sourceDir%" does not exist.
pause
exit /b 1
)

:: Ensure the output directory exists
for %%A in ("%archiveFile%") do (
if not exist "%%~dpA" mkdir "%%~dpA"
)

echo Bundling "%sourceDir%" into "%archiveFile%"...
echo.

:: Save the current directory so we can return to it
pushd "%sourceDir%\.."

:: Execute TAR creation
:: 'c' means create a new archive
:: 'v' means verbose (list files as they are processed)
:: 'f' specifies the archive filename
tar.exe -cvf "%archiveFile%" "WebData"

if %errorlevel% equ 0 (
echo.
echo ==========================================
echo BUNDLE SUCCESSFUL
echo Output: %archiveFile%
echo ==========================================
) else (
echo.
echo [ERROR] TAR creation failed with exit code %errorlevel%.
popd
pause
exit /b %errorlevel%
)

:: Return to the original directory
popd

pause
endlocal
tip

If you run tar -cvf Archive.tar C:\Logs\Daily, the resulting TAR will contain the full folder hierarchy C/Logs/Daily/ inside the archive. The script above uses pushd to navigate to the parent directory first, then references the folder by its short name so only WebData/ appears at the archive root.

Method 2: Creating a Compressed .tar.gz

The native tar.exe includes compression flags. Combining the z flag instructs the utility to compress the output using GZIP simultaneously.

@echo off
setlocal

set "sourceDir=C:\App\Logs"
set "archiveFile=C:\Archived\Logs_Jan.tar.gz"

:: Check if the source directory exists
if not exist "%sourceDir%\" (
echo [ERROR] Source directory "%sourceDir%" does not exist.
pause
exit /b 1
)

:: Ensure the output directory exists
for %%A in ("%archiveFile%") do (
if not exist "%%~dpA" mkdir "%%~dpA"
)

echo Compressing "%sourceDir%" into "%archiveFile%"...
echo.

:: Navigate to the parent directory to control internal paths
pushd "%sourceDir%\.."

:: Extract just the folder name from the full source path
for %%F in ("%sourceDir%") do set "folderName=%%~nxF"

:: 'c' = create, 'z' = gzip compress, 'f' = output file
tar.exe -czf "%archiveFile%" "%folderName%"

if %errorlevel% equ 0 (
echo.
echo ==========================================
echo COMPRESSION SUCCESSFUL
echo Output: %archiveFile%
echo ==========================================
) else (
echo.
echo [ERROR] Compression failed with exit code %errorlevel%.
popd
pause
exit /b %errorlevel%
)

popd

echo.
echo Compression complete.
pause
endlocal
warning

The native Windows tar.exe is only available on Windows 10 build 17063 and later. If your script must support older systems, check for the executable first with where tar.exe >nul 2>&1 and fall back to 7-Zip if it is not found.

Why Create TAR Archives in Batch?

  1. Cross-Platform Deployments: Bundling an entire Node.js backend app directory on Windows, SCP-transferring the .tar.gz bundle to an Ubuntu server, and extracting it identically.
  2. Docker Containers: Building a root file system snapshot natively on Windows to import as a Docker container base image via docker load.
  3. Log Harvesting: Instead of sending 500 tiny text logs across the network (slow due to SMB file headers), tar.exe streams them into a single chunk for efficient transfer.

Important Considerations

  1. Path Structuring: If you run tar -cvf Archive.tar C:\Logs\Daily, the resulting TAR will literally contain the folder hierarchy C/Logs/Daily/ inside the archive. If you only want the files to sit at the root of the archive, cd /d C:\Logs\Daily\ first, then run tar -cvf Archive.tar *.
  2. Excluded Files: TAR includes everything by default. If you need to omit standard subdirectories like .git or node_modules, you must use the --exclude flag: tar -cvf bundle.tar --exclude "node_modules" .
  3. Single-Threaded Performance: The native Windows tar.exe does not support multi-threading (unlike 7-Zip). A multi-gigabyte compress operation (-czf) will run on a single CPU core for significantly longer than an equivalent .7z command might.

Conclusion

Creating TAR archives brings fundamental cross-platform data management directly to the Windows command line. Because modern systems embed tar.exe natively, your Batch scripts can assemble complex deployment packages and backup bundles without demanding the installation of any third-party software, guaranteeing smooth deployment pipelines from Windows to the Linux ecosystem.