【问题标题】:Batch file to unzip file contents to folder and rename / prefix contents with zip file name将文件内容解压缩到文件夹并使用 zip 文件名重命名/前缀内容的批处理文件
【发布时间】: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


    【解决方案1】:
    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        for %%z in (*.zip) do (
            if not exist "%%~nz" md "%%~nz"
            7za e -o"%%~nz" "%%~fz"
            for %%f in ("%%~nz\*") do ren "%%~ff" "%%~nz - %%~nxf"
        )
    

    对于每个 zip 文件,如果不存在同名文件夹,则创建它。将 zip 文件解压缩到新文件夹中。对于文件夹中的每个文件,将文件重命名为 zip 的名称,后跟文件的原始名称。

    【讨论】:

    • MC ND 你的答案是完美的。非常感谢您花时间回复。
    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多