【问题标题】:powershell if file doesnt exist log string to filepowershell 如果文件不存在日志字符串到文件
【发布时间】:2013-01-28 01:07:04
【问题描述】:

我有许多文件路径存储在数据库中。我需要检查文件是否确实存在。我以前做过,但丢失了它的脚本,需要一些帮助。

我将所有路径放在一个文本文件中,并希望遍历它们,并检查它们是否存在。如果它们不存在,我想将不存在的路径放在日志文件中。

类似这样的:

# ! equals -not

$log = "e:\pshell\notExists.log"
$log | out-file $log

$list = Get-Content "e:\pshell\files.txt"

Foreach ($file in $list)
{
  CHECK IF FILE EXISTS
  IF IT DOESNT then Write-Output $file
}

一点帮助?

【问题讨论】:

    标签: powershell file-exists logfile


    【解决方案1】:

    测试路径?

    $log = "e:\pshell\notExists.log" $log | out-file $log
    
    $list = Get-Content "e:\pshell\files.txt"
    
    Foreach ($file in $list) 
    { 
       If (!(test-path $file))
       {
          Write-Output $file
       }
    }
    

    【讨论】:

      【解决方案2】:

      如果您的 inputfile 是每行一个文件路径,请尝试:

      $log = "e:\pshell\notExists.log"
      
      Get-Content "e:\pshell\files.txt" | Where-Object {
          #Keep only paths that does not exists
          !(Test-Path $_)
      } | Set-Content $log
      

      【讨论】:

        【解决方案3】:
        $log = "e:\pshell\notExists.log" 
        
        Get-Content "e:\pshell\files.txt" |
        where {!(test-path $_)} |
        add-content $log
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-05-30
          • 1970-01-01
          • 2013-11-07
          • 1970-01-01
          • 2011-01-28
          • 2012-08-16
          • 2015-04-10
          相关资源
          最近更新 更多