批处理脚本来压缩所有文件而不包含父文件夹

发布于 2024-11-23 18:21:32 字数 627 浏览 3 评论 0原文

我想创建一个批处理文件,可以从我放入脚本中的文件夹生成 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文件具有这样的结构,没有文件夹父(MyFolder)

subFolder1
subFolder2
file1.txt
file2.doc
file3.js

可以有人帮我解决这个问题吗?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

阳光下的泡沫是彩色的 2024-11-30 18:21:32

按如下所示更改 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(并且它将被排除)。

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

瑶笙 2024-11-30 18:21:32

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

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

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

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"

太阳公公是暖光 2024-11-30 18:21:32

现在我按照您的要求列出
我在桌面上创建了 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 命名。

*.* 表示压缩所有文件

在此处输入图像描述

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

风月客 2024-11-30 18:21:32
@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 ------------------
@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 ------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文