【发布时间】:2014-09-12 10:10:29
【问题描述】:
我经常通过 ftp 接收多个 .zip 文件。
我将每个 .zip 文件单独解压缩到一个名为 .zip 文件名称的文件夹中,并将文件夹中的每个文件重命名为原始 .zip 文件的名称。
例子:
如果我收到一个名为“10 smith street windsor.zip”的 .zip 文件,其中包含文件“permit application.doc”、“architectural drawing.doc”,我会将 .zip 的内容解压缩到一个名为“10 smith street windsor”并在每个文件前加上文件夹名称,即“10 smith street windsor - permit application.doc”、“10 smith street windsor - 建筑图纸.doc”。
我想使用批处理文件自动执行此手动过程,该批处理文件为每个 .zip 文件创建一个名为的文件夹,将 .zip 文件的内容复制到该文件夹,然后将文件夹中的每个文件重命名为原始 .zip 文件。
我的偏好是使用 7zip (7za) 的命令行版本。
我已经修改了我在网上找到的现有脚本(见下文),但它仅适用于没有空格的文件夹名称。例如,下面的脚本为 .zip 文件“10_smith_street_windsor.zip”创建了一个文件夹,并成功重命名/为文件夹/zip 内容添加前缀,但在带有空格“10 smith street windsor.zip”的 .zip 文件上失败
rem loop through all the zips
for %%c in (*.zip) do (`enter code here`
rem make a temporary folder with the same name as zip to house the zip content
if not exist %%~nc md %%~nc
rem extract zip content into the temporary folder
7za e -o"%%~nc" %%c
if exist "%%~nc" (
rem jump into the temporary folder
pushd "%%~nc"
if exist *.* (
rem loop through all the files found in the temporary folder and prefix it with the zip's name
for %%i in (*.*) do (
ren "%%i" "%%~nc.%%i"
)
)
rem jump out of the temporary folder
popd
)
)
希望有任何帮助:-)
问候
乔治
【问题讨论】:
标签: batch-file for-loop zip rename