Batch script to zip all the files without the parent folder

眉间皱痕 提交于 2019-12-30 18:32:33

问题


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


回答1:


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




回答2:


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"




回答3:


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




回答4:


@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 ------------------


来源:https://stackoverflow.com/questions/6696606/batch-script-to-zip-all-the-files-without-the-parent-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!