9

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

wahyueka31
  • 612
  • 2
  • 8
  • 16

4 Answers4

8

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).

Andriy M
  • 71,352
  • 17
  • 87
  • 142
1

You can use this batch file for creating rar without parent folder.

SET WINRAR="C:\Program Files\WinRAR"

%WINRAR%\WinRAR.exe a -ep1 "D:\Archive\Test.rar" "D:\Projects\Test"

nim007
  • 2,070
  • 3
  • 14
  • 25
0
@REM ------- BEGIN demo.cmd ----------------
@setlocal
@echo off

set path="C:\Program Files\WinRAR\";%path%

for /F %%i in ('dir /s/b *.rar') do call :do_extract "%%i"
goto :eof

:do_extract
echo %1
mkdir %~1.extracted
pushd %~1.extracted
unrar e %1
popd

REM ------- END demo.cmd ------------------
jeb
  • 70,992
  • 15
  • 159
  • 202
Shibu.S
  • 11
  • 2
0

Now I'm listing as per your requirement I've MyFolder Created on my Desktop which contains 5 files for example below as you've given

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

Now you query is to zip all the contents within MyFolder then the first step is to navigate to that folder path which is located in Desktop so first i will locate to my desktop.

Note:(My username will be different from you hope you know the basic windows stuff)

1.C:\Documents and Settings\ishwar\Desktop\MyFolder>set path="c:\ProgramFiles  
  \WinRAR";%path%

  -- Set the path (note if you are doing using commands from cmd prompt you need to
  do this every time when you open cmd newly if you are creating batch file then OK)

2. C:\Documents and Settings\ishwar>cd Desktop

3. C:\Documents and Settings\ishwar\Desktop>cd MyFolder 

-- change directory to the folder in which all the files are stored.

4. C:\Documents and Settings\ishwar\Desktop\MyFolder>winrar a MyFolder *.*

-- this command will zip all the contents and will create MyFolder.rar file within
   MyFolder.

5. You are done.

where,

winrar is command to zip

a is argument

MyFolder to give name to zip.

*.* means zip all the files

enter image description here

Vishwanath Dalvi
  • 31,604
  • 36
  • 115
  • 146
  • What `C:\Documents and Settings\ishwar\Desktop\e-commerce>` means?? Besides that, the `rar` command is won't work on my CommandPrompt.. – wahyueka31 Jul 14 '11 at 22:52
  • @Rebel: It is just an example path. Replace rar with rar.exe or, as in your example, winrar.exe. – Gfy Jul 15 '11 at 04:46