DevOps · K8s · Volleyball · Travel  •  DevOps · K8s · Volleyball · Travel  •  DevOps · K8s · Volleyball · Travel
Explore NY Stream

Using WinRAR zip the file & send it to FTP server

— ny_wk

Using WinRAR zip the file & send it to FTP server

The WinRAR command line lets you compress a file or folder into a date-stamped, password-protected archive and then push it to a remote FTP server without ever touching the WinRAR window. Pair rar.exe (or WinRAR.exe) with a scripted upload and Windows Task Scheduler, and you have a hands-off backup that runs every night while you sleep.

This guide walks through every piece of that pipeline: building the archive from the WinRAR command line, scripting the FTP transfer with both the classic ftp.exe client and a modern PowerShell/WinSCP approach, gluing it all into a single .bat file, scheduling it, and avoiding the traps that silently break automated transfers.

The Goal: An Unattended Compress-and-Upload Backup

The end state is one batch file that you can run by hand or trigger on a schedule. Each time it runs it will:

  • Compress a source file or directory into a .rar archive using the WinRAR command line.
  • Stamp the filename with the current date so every run produces a unique, sortable archive instead of overwriting yesterday's backup.
  • Optionally encrypt the archive with a password so the data is protected in transit and at rest.
  • Upload the finished archive to an FTP (or, ideally, SFTP/FTPS) server.
  • Log the result so you can confirm it actually worked.

Everything below builds toward that single file, one step at a time.

Step 1: Compress a File With the WinRAR Command Line

WinRAR ships two command-line executables. WinRAR.exe is the GUI build but accepts command-line switches and shows a progress window; rar.exe is the pure console tool and is the better choice for silent automation because it returns clean exit codes and never pops a window. Both live in the install directory, usually C:\Program Files\WinRAR\.

The basic a (add) command

The core verb is a, which means "add to archive." The first path after the verb is the archive to create; everything after it is the content to include.

"C:\Program Files\WinRAR\rar.exe" a C:\Backups\softwares.rar E:\W2\Softwares

Because the source path contains a folder, add -r to recurse into all subdirectories. Without it, WinRAR only grabs files in the top level of the folder.

"C:\Program Files\WinRAR\rar.exe" a -r C:\Backups\softwares.rar E:\W2\Softwares

Essential switches

SwitchWhat it does
aAdd files to an archive (creates it if missing).
-rRecurse subdirectories.
-agAppend the current date/time to the archive name using a format mask.
-p<pwd>Encrypt file data with a password (no space after -p).
-hp<pwd>Encrypt both data and the file-name headers (stronger).
-m0 to -m5Compression level: -m0 store (fastest), -m5 maximum (smallest).
-ep1Exclude the base folder path so the archive stores relative paths.
-yAssume "Yes" to all prompts (vital for unattended runs).
-dfDelete source files after they are archived (use with care).
-ibckRun minimized in the background.

Date-stamped names with -ag

The -ag switch is what turns a fixed filename into a rolling backup set. By default it inserts a YYYYMMDDHHMMSS-style string, but you control the format with a mask. For example, -agYYYY-MM-DD produces names like softwares2026-06-28.rar.

"C:\Program Files\WinRAR\rar.exe" a -r -ag-YYYY-MM-DD C:\Backups\softwares.rar E:\W2\Softwares

The leading dash inside the mask (-ag-YYYY-MM-DD) inserts a literal separator so the result reads softwares-2026-06-28.rar rather than running the date straight into the base name. Use uppercase MM for month and lowercase mm for minutes, and uppercase DD for day; mixing them up is the most common -ag mistake.

Adding a password

To encrypt the archive, append -p immediately followed by the password, with no space. Use -hp instead if you also want to hide the list of file names inside the archive:

"C:\Program Files\WinRAR\rar.exe" a -r -hpMyStr0ngPass -ag-YYYY-MM-DD C:\Backups\softwares.rar E:\W2\Softwares

WinRAR uses AES-256 encryption for the RAR5 format. Be aware that a password typed directly into a batch file is stored in plain text on disk; later in this guide there are safer ways to handle credentials.

Putting the compress step together

A robust, fully silent compress command for automation looks like this:

"C:\Program Files\WinRAR\rar.exe" a -r -m5 -ep1 -y -ibck -ag-YYYY-MM-DD C:\Backups\softwares.rar E:\W2\Softwares

That recurses the folder, uses maximum compression, stores relative paths, answers prompts automatically, runs in the background, and writes a date-stamped archive into C:\Backups\.

Step 2: Upload the Archive to an FTP Server

Once the archive exists, the second half of the pipeline transfers it. There are two practical ways to script this on Windows.

Option A: The classic ftp.exe script

Windows ships a built-in FTP client, ftp.exe. It cannot read commands from the command line directly, so you feed it a plain-text script with the -s: switch. Each line of that script is exactly what you would type at the ftp> prompt.

Create a script file, for example C:\Scripts\upload.txt:

open 10.170.4.99
username_here
password_here
binary
cd /incoming
lcd C:\Backups
put softwares.rar
bye

Key points about that script:

  • The line after open is the username and the next line is the password; they are read in that order.
  • binary (or its abbreviation bi) forces binary mode. Always set binary for a .rar file — ASCII mode corrupts compressed archives.
  • cd changes the remote directory; lcd changes the local directory so you do not have to type a full local path on the put line.
  • put uploads the file; bye (or quit) closes the session cleanly.

Run the script from a command prompt or batch file:

ftp -s:C:\Scripts\upload.txt

Because the date-stamped archive has a changing name, hard-coding put softwares.rar will not match a file called softwares-2026-06-28.rar. Two simple fixes: either keep a fixed-name copy to upload, or generate the script on the fly in your batch file (shown in Step 3). To upload by pattern instead of an exact name, use mput with a wildcard and disable the per-file prompt:

prompt
mput softwares-*.rar

Option B: PowerShell or WinSCP (the modern path)

The legacy ftp.exe client only speaks plain, unencrypted FTP and has clumsy passive-mode handling. For anything crossing a network you do not fully control, a modern client is the better choice.

PowerShell with WebClient handles a quick FTP put in a single line and lets you keep credentials in script variables:

$c = New-Object System.Net.WebClient
$c.Credentials = New-Object System.Net.NetworkCredential('user','pass')
$c.UploadFile('ftp://10.170.4.99/incoming/softwares.rar','C:\Backups\softwares.rar')

For encrypted transfers, install WinSCP, which supports SFTP and FTPS and ships a scriptable command-line tool. A WinSCP script that securely uploads over SFTP looks like this:

open sftp://user:pass@10.170.4.99/ -hostkey="ssh-ed25519 256 xx:xx:..."
put C:\Backups\softwares-*.rar /incoming/
exit

Invoke it with winscp.com /script=C:\Scripts\upload.wsf /log=C:\Scripts\upload.log. WinSCP's /log flag is invaluable for diagnosing failed automated runs, and its -hostkey verification prevents man-in-the-middle attacks that plain FTP cannot detect.

Step 3: Combine Everything Into a Single Batch File

Now the two halves merge. The batch file below compresses the folder with a date-stamped name, regenerates the FTP script so it always references the correct filename, runs the upload, and appends a timestamped line to a log.

@echo off
setlocal
set RAR="C:\Program Files\WinRAR\rar.exe"
set SRC=E:\W2\Softwares
set DEST=C:\Backups
set FTPHOST=10.170.4.99
set FTPUSER=username_here
set FTPPASS=password_here
 
rem --- build a date stamp like 2026-06-28 ---
for /f "tokens=1-3 delims=/-. " %%a in ('echo %DATE%') do set STAMP=%%c-%%b-%%a
set ARCHIVE=softwares-%STAMP%.rar
 
rem --- Step 1: compress ---
%RAR% a -r -m5 -ep1 -y "%DEST%\%ARCHIVE%" "%SRC%"
if errorlevel 1 (echo [%DATE% %TIME%] COMPRESS FAILED >> "%DEST%\backup.log" & exit /b 1)
 
rem --- Step 2: write the FTP script ---
set SCRIPT=%TEMP%\ftpup.txt
(
echo open %FTPHOST%
echo %FTPUSER%
echo %FTPPASS%
echo binary
echo cd /incoming
echo lcd %DEST%
echo put %ARCHIVE%
echo bye
) > "%SCRIPT%"
 
rem --- Step 3: upload ---
ftp -s:"%SCRIPT%"
del "%SCRIPT%"
echo [%DATE% %TIME%] Done uploading %ARCHIVE%. >> "%DEST%\backup.log"
endlocal

A few things worth understanding in that script:

  • The date parsing depends on your regional settings. The for /f token order (%%c-%%b-%%a) assumes %DATE% prints as DDD DD/MM/YYYY. If your machine formats dates differently, adjust the token order, or sidestep the problem entirely by letting WinRAR's -ag switch generate the stamp.
  • errorlevel checking stops the batch from trying to upload an archive that never got created. rar.exe returns 0 on success and non-zero on failure, which makes if errorlevel 1 reliable.
  • The temp script is deleted after the upload because it briefly contains the password in clear text.

Step 4: Schedule It With Task Scheduler

An automated backup is only useful if it runs on its own. Windows Task Scheduler handles that. You can do it through the GUI or the command line.

From the command line, register a daily 2:00 AM job with schtasks:

schtasks /Create /TN "WinRAR FTP Backup" /TR "C:\Scripts\winrarbackup.bat" /SC DAILY /ST 02:00 /RU SYSTEM

Through the GUI, open Task Scheduler, choose Create Task, and configure these settings that matter for unattended batch jobs:

  • On the General tab, select Run whether user is logged on or not so the job fires even with no one signed in.
  • Check Run with highest privileges if the script writes to protected folders.
  • On the Actions tab, set the program to your .bat path and set Start in to the script's folder so relative paths resolve.
  • On the Settings tab, enable Run task as soon as possible after a scheduled start is missed for machines that may be off at 2 AM.

Test the task immediately rather than waiting for the schedule: schtasks /Run /TN "WinRAR FTP Backup". If it works on demand but fails on schedule, the cause is almost always permissions or a working-directory difference — the two things the settings above address.

Common Pitfalls and How to Avoid Them

Automated FTP backups fail quietly far more often than they fail loudly. These are the issues that cause most silent breakage.

  • Passive vs. active mode. Behind a firewall or NAT, active-mode FTP usually stalls because the server cannot open a data connection back to your machine. The legacy ftp.exe defaults to active mode and offers no clean passive toggle, which is a leading reason scripted uploads hang. Modern clients like WinSCP and the .NET FTP classes default to passive mode — another reason to prefer them.
  • ASCII vs. binary corruption. Forgetting binary in an ftp.exe script corrupts the archive on transfer, and the failure only surfaces when you try to extract it. Set binary mode every time.
  • Plain-text credentials. Passwords sitting in a .bat or .txt file are readable by anyone with file access. Restrict NTFS permissions on the script, generate the FTP script into a temp file and delete it after use, or move to a credential-aware tool. WinSCP can store credentials in an encrypted session profile rather than in the script.
  • Unencrypted FTP. Plain FTP sends your username, password, and data in clear text across the wire. Prefer SFTP or FTPS wherever the server supports it; both encrypt the session and authenticate the host.
  • Hard-coded filenames. A date-stamped archive will not match a static put softwares.rar line. Generate the script dynamically, upload by wildcard, or keep a fixed-name copy.
  • Path quoting. Any path containing spaces — including C:\Program Files\WinRAR\ — must be wrapped in double quotes, or the command breaks at the space.

Step 5: Verify the Backup Actually Worked

Never assume a scheduled job succeeded. Build verification into the process:

  • Read the log. The batch file appends a timestamped "Done uploading" line. If that line is missing for a given day, the run failed before reaching it.
  • Test the archive integrity after creation with rar.exe t C:\Backups\softwares-2026-06-28.rar, which checks the archive for corruption without extracting it.
  • Confirm the remote file size matches the local one. A truncated file on the server usually means a dropped connection or an ASCII-mode transfer.
  • Periodically restore a test extract from the server copy. A backup you have never restored is only a hopeful guess, not a proven backup.

Key Takeaways

  • Use rar.exe a -r to create archives from the WinRAR command line; rar.exe is cleaner than WinRAR.exe for silent automation.
  • The -ag switch generates date-stamped filenames so each run produces a unique archive instead of overwriting the last backup.
  • Encrypt with -p (data) or -hp (data plus file names), and always upload in binary mode to avoid corrupting the archive.
  • The classic route is ftp -s:script.txt; the safer modern route is PowerShell or WinSCP with SFTP/FTPS and host-key verification.
  • Schedule with schtasks or Task Scheduler, then verify with logs, rar.exe t integrity checks, and occasional test restores.

Frequently Asked Questions

What is the difference between rar.exe and WinRAR.exe on the command line?

Both accept the same switches, but WinRAR.exe is the GUI build and shows a progress window, while rar.exe is a pure console tool that runs silently and returns reliable exit codes. For scheduled, unattended backups, rar.exe is the better choice because it never pops a window and integrates cleanly with errorlevel checks.

How do I add a date and time to the archive name automatically?

Use the -ag switch with a format mask, for example -ag-YYYY-MM-DD, which appends something like -2026-06-28 to the base name. Remember the case rules: uppercase MM is month, lowercase mm is minutes, and uppercase DD is day. This avoids fragile date parsing in batch scripts that depends on regional settings.

Why does my scripted FTP upload hang or time out?

The most common cause is active versus passive mode. Behind a firewall or NAT, the built-in ftp.exe client tends to stall because it defaults to active mode and the server cannot open the return data connection. Switching to a client that uses passive mode by default — WinSCP or the .NET FTP classes in PowerShell — usually resolves it.

Is it safe to store the FTP password in the batch file?

Not really. Anyone who can read the file can read the password, and plain FTP also transmits it in clear text over the network. Restrict NTFS permissions on the script, generate the credential file into a temporary location and delete it after each run, and prefer SFTP or FTPS with an encrypted session profile so the password is neither stored nor transmitted in the clear.

For more Windows automation, scripting, and system-admin walkthroughs, subscribe to @explorenystream on YouTube.