Batch script to zip all the files without the parent folder

wahyueka31 picture wahyueka31 · Jul 14, 2011 · Viewed 35.7k times · Source

I wanted to create a batch file that can make a zip file from a folder that I put in the script. Here's my script:

@REM ------- BEGIN xpi.bat ----------------
@setlocal
@echo off
set path="C:\Program Files\WinRAR\";%path%

winrar.exe a -afzip -m5 -ed -pTest -r c:\test.zip c:\MyFolder

REM ------- END xpi.bat ------------------

The script above creates a zip file with a structure like this,

MyFolder
--subFolder1
--subFolder2
--file1.txt
--file2.doc
--file3.js

But what I want the zip file that is formed has a structure like the this, without the folder parent (MyFolder),

subFolder1
subFolder2
file1.txt
file2.doc
file3.js

Can anyone help me fix this?

note:application that I use is WinRar

Answer

Andriy M picture Andriy M · Jul 15, 2011

Change the winrar.exe invocation line as follows:

winrar.exe a -afzip -m5 -ed -pTest -r -ep1 c:\test.zip c:\MyFolder\*

The -ep1 switch tells the archiver to exclude the base folder from the paths. But for C:\MyFolder the base folder is C:\, so MyFolder will still be added to the archive. Therefore you need to change the path to c:\MyFolder\*, for which the base folder is c:\MyFolder (and it will be excluded).