zip文件解压缩并重命名文件并压缩回来

时间:2014-04-04 04:50:41

标签: zipfile

我需要帮助创建一个bat文件,使我能够自动执行以下操作。

我有一个名为abc.zip的ZIp文件,它包含files-test.txt,dec.drl,tes.txt。我需要的是首先需要解压缩文件,然后将文件名重命名为parent zip文件如abc.txt,abc.drl,abc.txt。 然后最后它需要拉回来。

任何输入都受到高度赞赏。

提前致谢。

最诚挚的问候, 塞卡尔

1 个答案:

答案 0 :(得分:0)

OP,下面是应该完成你要做的事情的代码。将代码块复制并粘贴到您喜欢的编辑器(notepad-mod,notepad ++,programmer notepad等)中,并将其保存为批处理文件。我已经为每个步骤添加了注释,但是如果您在理解某些命令时仍有问题,请参阅@Alexej Magura发布的链接以获取更多信息(尤其是循环中的变量扩展)。

注意:我使用的是7zip而不是winzip。您可以使用适当的winzip命令替换7zip命令。此脚本已在Windows 7上进行了测试。

:: # creating test files
echo hello>files-test.txt
echo world>dec.drl
echo !!!!!>test.txt

:: # showing you the directory listing
dir
pause

:: # creating test zip file (abc.zip)
7z a -tzip abc files-test.txt
7z a -tzip abc dec.drl
7z a -tzip abc test.txt

:: # showing you the directory listing
dir
:: # showing you the content inside the zip file
7z l abc.zip
pause
:: # Finished prepping your scenario (abc.zip with 3 files inside)



:: # Core Logic
:: # Looping through all the zips
for %%c in (*.zip) do (
    :: # Make a temporary folder with the same name as zip to house the zip content
    if not exist %%~nc md %%~nc
    :: # Extracting zip content into the temporary folder
    7z e -o%%~nc %%c
    if exist %%~nc (
        :: # Jump into the temporary folder
        pushd %%~nc
        if exist *.* (
            :: 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
            )
            :: # Zip all the files with the zip prefix with orginal zip name but with a number 2 (abc2.zip)
            if exist %%~nc.* (
                7z a -tzip %%~nc2 %%~nc.*
            )
            :: # Move the new zip back out of the tempory folder
            if exist %%~nc2.zip move %%~nc2.zip ..
        )
        :: # Jump out of the temporary folder
        popd
        :: # Showing you the directory listing
        dir
        :: # Showing you the content inside the new zip
        7z l %%~nc2.zip
        :: # Remove the temporary folder (Clean up)
        rd /s/q %%~nc
    )
)