使用 FOR 或 FORFILES 批处理命令单独归档特定文件 [英] Using FOR or FORFILES batch command to individually archive specific files
问题描述
我在不同的目录和子目录中有一组 .iso 文件:
I have a set of .iso files in different directories and sub-directories:
<folder1>
app1.iso
app2.iso
app3.iso
<subfolder1>
app1a.iso
app2a.iso
<subfolder2>
app2b.iso
app3b.iso
<folder2>
app4.iso
app5.iso
<subfolder5>
app20.iso
使用批处理文件并预装了 7-zip(配置了 PATH 环境变量),我希望能够将每个 .iso 存档到它自己的单独 .7z 存档文件中.我不想将当前文件夹中的所有 .iso 存档到一个存档中.
Using a batch file and having 7-zip pre-installed (with PATH environment variable configured), I want to be able to archive each .iso to it's own individual .7z archive file. I don't want to archive all .iso's in a current folder into one archive.
我尝试结合 FORFILES 创建自己的 7-zip 命令:
I attempted to create my own 7-zip command in conjunction with the FORFILES:
FORFILES /P . /M *.iso /S /C "cmd /c 7z a -t7z @path.7z -mx9 -mmt >> C: est7z-log.txt"
然而,它的作用是:
app1.iso.7z - contains: app1.iso, app2.iso, app3.iso
app2.iso.7z - contains: app1.iso.7z, app1.iso, app2.iso, app3.iso
app3.iso.7z - contains: app1.iso.7z, app2.iso.7z, app1.iso, app2.iso, app3.iso
app1a.iso.7z - contains: app1a.iso, app2a.iso
app2a.iso.7z - contains: app1a.iso.7z, app1a.iso, app2a.iso
...
我想要的是(每个在其相关文件夹或子文件夹中):
What I want is (each in their relevant folder or sub-folder):
app1.iso.7z - contains: app1.iso
app2.iso.7z - contains: app2.iso
app3.iso.7z - contains: app3.iso
app1a.iso.7z - contains: app1a.iso
...
谁能帮我处理批处理命令?
Can anyone help me on the batch command?
推荐答案
@ECHO OFF
SETLOCAL
SET "sourcedir=U:sourcedir"
FOR /f "delims=" %%a IN (
'dir /s/b /a-d "%sourcedir%*.iso" '
) DO (
ECHO(7z a -t7z "%%~fa.7z" "%%~fa" -mx9 -mmt
)
GOTO :EOF
您需要更改sourcedir
的设置以适应您的情况.
You would need to change the setting of sourcedir
to suit your circumstances.
所需的 7z 命令仅用于测试目的ECHO
.在您确认命令正确后,将 ECHO(7z
更改为 7z
以实际存档文件.
The required 7z commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO(7z
to 7z
to actually archive the files.
请注意重定向到日志丢失.这样做是为了让建议的操作显示在屏幕上.
Note that the redirection-to-log is missing. This is done to allow the proposed actions to be displayed to the screen.
这篇关于使用 FOR 或 FORFILES 批处理命令单独归档特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!