Zipping Folders with WinRar Command in Batch Script on windows

You know that tedious boring job of Raring or Zipping files from remote location where your project is deployed, just so you can copy it on another server so you can extract it on production or any other server? The reason being told from your CTO to do it manually, because using scripts is prone to errors. Well they are wrong! I would trust my stupid .bat cripple script on one click, rather do this thing myself manually and forget some step, thing, file etc. What this batch script does:

Upon setting your WinRar location, Source Folder destination, and Zip File Location it just zips the files from the source including sub directories to your designated Zip Folder etc.

Solution:

Just a note that as a bonus this script removes the web.config files when zipping the project as I found it more convenient (when deploying on production to not fuck up your project when some idiot just overwrites the whole files)

Scroll down to see example of folder structure before and after zipping if you like

@ECHO OFF

SET RarDirPath="C:\Program Files (x86)\WinRAR\winrar.exe"

SET SourceDirPath=\\192.168.1.1\Build
SET SFolderName=MyProjectFolder
SET SourceDirPath=%SourceDirPath%%SFolderName%

SET DestinationDirPath=C:\Tmp\

SET ExcludeFiles=-x*Web.Config
SET ExcludeFolders=-x%SourceDirPath%\DatabaseFolder

%RarDirPath% a -afzip -ep1 %ExcludeFiles% %ExcludeFolders% "%DestinationDirPath%%SFolderName%.zip" "%SourceDirPath%"

Output:

So if your Source Folder Looks like this:

├───AwesomeWebPage
│   │   favicon.ico
│   │   Global.asax
│   │   Web.config
│   │
│   ├───bin
│   ├───Content
│   │       Site.css
│   │
│   ├───DatabaseFolder
│   │       SomeOtherSQL.sql
│   │
│   └───Views
│       │   Web.config
│       │   _ViewStart.cshtml
│       │
│       ├───Home
│       │       Index.cshtml
│       │
│       └───Shared
│               _Layout.cshtml
│
└───DatabaseFolder
    │   Some SQL.SQL
    │
    └───bin

Destination Folder will contain Zip file with this structure:

└───AwesomeWebPage
    │   favicon.ico
    │   Global.asax
    │
    ├───bin
    ├───Content
    │       Site.css
    │
    ├───DatabaseFolder
    │       SomeOtherSQL.sql
    │
    └───Views
        │   _ViewStart.cshtml
        │
        ├───Home
        │       Index.cshtml
        │
        └───Shared
                _Layout.cshtml

Notice how the exclusion of files, excluded the web.Config from all of the folders. And as for the DatabaseFolder, it only excluded the one in the root of the Source dir, the other one stays intact

Leave a Reply

Your email address will not be published. Required fields are marked *