用于压缩没有父文件夹的所有文件的批处理脚本

时间:2011-07-14 16:21:54

标签: batch-file winrar

我想创建一个批处理文件,可以从我放在脚本中的文件夹中创建一个zip文件。这是我的剧本:

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

上面的脚本创建了一个zip文件,其结构如下,

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

但我希望所形成的zip文件有这样的结构,没有文件夹parent (MyFolder)

subFolder1
subFolder2
file1.txt
file2.doc
file3.js

任何人都可以帮我解决这个问题吗?

注意:我使用的 应用程序是WinRar

4 个答案:

答案 0 :(得分:6)

更改winrar.exe调用行,如下所示:

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

-ep1开关告诉归档程序从路径中排除基本文件夹。但对于C:\MyFolder,基本文件夹为C:\,因此MyFolder仍将添加到存档中。因此,您需要将路径更改为c:\MyFolder\*,基础文件夹为c:\MyFolder(并且将被排除)。

答案 1 :(得分:1)

您可以使用此批处理文件创建没有父文件夹的rar。

SET WINRAR =“C:\ Program Files \ WinRAR”

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

答案 2 :(得分:0)

现在我按照你的要求列出了 我在我的桌面上创建了MyFolder,其中包含5个文件 例如,如下所示

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

现在您要查询是在MyFolder中压缩所有内容,然后第一步是 导航到位于桌面的文件夹路径,所以首先我会 找到我的桌面。

注意:(我的用户名将与您希望了解基本的Windows内容不同)

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.

其中,

winrar是压缩命令

a是参数

MyFolder为zip命名。

*.*表示压缩所有文件

enter image description here

答案 3 :(得分: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 ------------------