7-Zip 命令行在压缩时会继续包含文件列表中没有的文件,如果这些文件与列表中的文件同名

7-Zip 命令行在压缩时会继续包含文件列表中没有的文件,如果这些文件与列表中的文件同名

尝试将选定的文件和文件夹放入 bat 脚本中,将其压缩为 zip 文件。除了子文件夹中的文件与我传递给 7z 的列表文件同名的情况外,其他一切都运行良好。无论我尝试什么,7z 都会将这些文件包含到 zip 中,即使它们不在列表中。

以下是带有空文件的文件夹结构,用于重现我所遇到的情况: https://drive.google.com/file/d/1XM5-I7FY2mdvyM4vTUVbNXfshYqy6hyX/view?usp=sharing

参考:

+---folder1
       +---folder2
       |       file2.txt
       |       file4.txt
       |       file5.txt
       |
       +---folder3
       |       file6.txt
       |       file7.txt
       |       file8.txt
       |
       |---file1.txt
       |---file2.txt
       \---file3.txt

请帮帮我,我已经很头疼了。这是我的 bat 脚本:

@echo off
SETLOCAL EnableDelayedExpansion

echo.Compresssing selected files with 7-Zip
echo.Ultra compression into .zip arcvhive
echo.---------------------------------------------


::---------------------------------------------------------------------------------------------------
::here's output extension
SET "type=-tzip"
REM SET "type=-t7z"

::change noesis path to yours here
SET "ZPATH=%ProgramFiles%\7-Zip"


::---------------------------------------------------------------------------------------------------
::Collect files into 7zip.lst
echo.

if exist %ZPATH%\7z.exe (

    if [%1]==[] goto :eof 
    :loop

        SET "FPATH=%~dp1"
        
        if exist "%1" (
            if exist "%1\" (
                echo The given item is a directory.
                dir %1 /s /b /A:-D | findstr /V /I /C:"\\folder2\\" >> 7zip.lst
            ) else (
                echo The given item is a file.
                echo %1 >> 7zip.lst
            )
        ) else (
            echo The given item does not exist.
        )

        shift
        if not [%1]==[] goto loop
)

echo path: %FPATH%
for %%f in ("%FPATH%\" .) do set ANAME=%%~nxf
echo name: %ANAME%


::---------------------------------------------------------------------------------------------------
:: Ultra Compression
if exist %FPATH%7zip.lst (

    :: Make paths relative
    powershell -Command "(gc %FPATH%7zip.lst) -replace [Regex]::Escape('%FPATH%'), '' | Out-File %FPATH%7zip.lst -encoding utf8"

    "%ZPATH%\7z.exe" a ^
        %type% ^
        -spf2 ^
        -stl ^
        -mx9 ^
        -aoa ^
        "%FPATH%%ANAME%_Patreon.zip" ^
        -ir@"%FPATH%7zip.lst"

    REM del /f "%FPATH%7zip.lst"
)

pause

相关内容