【问题标题】:7zip is zipping the source path / empty variable7zip 正在压缩源路径/空变量
【发布时间】:2019-02-01 07:03:03
【问题描述】:

我制作了一个 powershell 脚本,它在变量 $toBeZipped 中检查不同文件夹中是否存在早于 x 天的日志文件。

只要有文件且变量不为空,脚本就可以正常工作。 如果变量为空,因为没有匹配过滤器的文件,7zip正在压缩7z.exe所在的文件夹

我该如何解决这个问题?

这是命令:

"C:\Program Files\7-Zip\7z.exe" A -t7z $zipFile $toBeZipped -m0=lzma2 -mx=9 -aoa

【问题讨论】:

  • so ... 测试 $Var 以查看其中是否有任何内容?
  • 什么意思?我测试了它,它是空的。那就是问题所在。在没有 $var 的情况下运行命令时的行为相同
  • 也许你可以添加一个 if 语句来检查变量是否为空。如果不是空进程,否则什么也不做
  • @mikejohnsen - 在使用前进行测试...如果它为空,则根本不要运行该命令。

标签: powershell file logging zip 7zip


【解决方案1】:

使用“if 语句”检查$toBeZipped 是否为空。像这样的:

if($toBeZipped -ne $null) {
"C:\Program Files\7-Zip\7z.exe" A -t7z $zipFile $toBeZipped -m0=lzma2 -mx=9 -aoa
} else {
Write-Host "toBeZipped is empty"
}

【讨论】:

    【解决方案2】:

    谢谢。使它可以检查 var 是否为空。

    我使用了下面的 if 语句

    IF([string]::IsNullOrEmpty($Files)) {
    Write-Host "Given string is NULL or EMPTY"
    Continue
    } else {
    #Zip commmand
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-13
      • 2010-09-08
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      相关资源
      最近更新 更多