【问题标题】:How to pack files into a ZIP file which have same name - folder + .txt - in batch [duplicate]如何将文件打包成具有相同名称的 ZIP 文件 - 文件夹 + .txt - 批量 [重复]
【发布时间】:2022-11-16 06:11:30
【问题描述】:
我正在寻找帮助
这是一个文件的示例:
d:\folder\video1\ - folder with content
d:\folder\video1.txt -
d:\folder\video22\ - folder with content
d:\folder\video22.txt
d:\folder\video300\ - folder with content
d:\folder\video300.txt
****我要打包每个文件夹
d:\folder\video1\ - 包含内容的文件夹
d:\文件夹\video1.txt
到一个文件 video1.zip 里面有结构
我有 200 - 300 个文件夹和 200 - 300 个 txt 文件已经正确重命名
只有我需要打包这个****
我试过使用 total commander,它提供了这个选项,但你必须一个一个地做,我试过使用 winrar,但我只能批量压缩文件夹,然后我不得不一个一个地手动添加 txt 文件
任何人都可以让我知道这是否可以使用 PowerShell 或 cmd ,因为我对此很陌生。
提前致谢
【问题讨论】:
标签:
powershell
batch-file
cmd
zip
【解决方案1】:
@ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:your files w o"
FOR /f "delims=" %%e IN ('dir /b /ad "%sourcedir%*" ') DO IF EXIST "%sourcedir%%%e.txt" ECHO zip add "%sourcedir%%%e.txt"
)
GOTO :EOF
在应用于真实数据之前,请始终对照测试目录进行验证。
使用dir /b /ad执行目录扫描,获取%%e中的子目录名称,然后如果该目录中有符合要求的文件,则将其添加到您的zip文件中。
我不知道你打算使用哪个 zip 实用程序,所以我只是 echoed 命令的基本内容。