简体   繁体   English

如何测试 Zip 文件在 Windows 批处理文件中是否有效

[英]How to test if a Zip file is valid in a windows batch file

I am using the command line version of 7-zip and I can use this to test if a zip file is valid by using the t command.我正在使用 7-zip 的命令行版本,我可以使用 t 命令来测试 zip 文件是否有效。

But I'm trying to create a batch file which will cycle through a bunch of zip files in a directory and do one thing if the zip is empty, and another thing if the zip has some files archived in it.但是我正在尝试创建一个批处理文件,该文件将在目录中的一堆 zip 文件中循环,如果 zip 为空,则执行一件事,如果 zip 中有一些存档文件,则执行另一件事。

Any pointers how you do this using a batch file?任何指示您如何使用批处理文件执行此操作?

this may help这可能有帮助

@echo off

setlocal enabledelayedexpansion

set "zipPath=C:\temporal\"
set "zipProg=C:\Program Files (x86)\7-Zip\7z.exe"

call:shortIt "%zipPath%", zipPath
call:shortIt "%zipProg%", zipProg

pushd %zipPath%

for /F "tokens=*" %%a in ('dir /b "%zipPath%" ^| find ".zip"') do (
  set/a numFiles=0, isOk=0, size=0, compressed=0
  for /F "tokens=*" %%i in ('%zipProg% t "%zipPath%%%a"') do (
    echo %%i | find /I "ok" >NUL && set/a isOK=1
    echo %%i | find /I "files" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a numFiles=%%n
    echo %%i | find /I "size" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a size=%%n
    echo %%i | find /I "compressed" >NUL && for /F "tokens=2 delims=:" %%n in ("%%i") do set/a compressed=%%n
  )
  if !isOk! neq 0 if !numFiles! equ 0 if !size! neq 0 set/a numFiles=1 

  if !isOk! equ 0 (
       echo(
       echo(%zipPath%%%a is not an archive or an error ocurred
       echo(
  ) else (
    if !numFiles! neq 0 (
      echo(%zipPath%%%a contains !numFiles! files [!size! to !compressed!]
    ) else (
       echo(%zipPath%%%a is empty
    )
  )
)


popd
endlocal

exit/B 0

:shortIt
SetLocal & set "token=%~s1"
EndLocal & set "%2=%token%"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM