Batch script to zip all the files without the parent folder
Asked Answered
S

4

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

Smitt answered 14/7, 2011 at 16:21 Comment(3)
I know you use WinRar, but I'd look into 7-zip. The CLI interface is quite nice, and I've made a similar script with it when I used Windows.Daciadacie
Not sure of this but how about C:\MyFolder\* ?Sandblast
Compressing the folder and compression the inner files gonna give same resultHairpin
D
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).

Delvecchio answered 15/7, 2011 at 7:32 Comment(1)
Actually, * at the end is not needed. Just end the path with a \ or /.Honeysweet
W
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"

Wodge answered 6/8, 2012 at 6:16 Comment(0)
H
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

Hairpin answered 14/7, 2011 at 16:50 Comment(2)
What C:\Documents and Settings\ishwar\Desktop\e-commerce> means?? Besides that, the rar command is won't work on my CommandPrompt..Smitt
@Rebel: It is just an example path. Replace rar with rar.exe or, as in your example, winrar.exe.Euphrosyne
L
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 ------------------
Loeffler answered 6/7, 2012 at 10:44 Comment(1)
you should explain what this is doingGobbledegook

© 2022 - 2024 — McMap. All rights reserved.