【问题标题】:Check if a file exists or not in Windows PowerShell?检查 Windows PowerShell 中是否存在文件?
【发布时间】:2015-10-31 00:30:59
【问题描述】:

我有这个脚本,它比较磁盘的两个区域中的文件,并将最新的文件复制到具有较旧修改日期的文件上。

$filestowatch=get-content C:\H\files-to-watch.txt

$adminFiles=dir C:\H\admin\admin -recurse | ? { $fn=$_.FullName; ($filestowatch | % {$fn.contains($_)}) -contains $True}

$userFiles=dir C:\H\user\user -recurse | ? { $fn=$_.FullName; ($filestowatch | % {$fn.contains($_)}) -contains $True}

foreach($userfile in $userFiles)
{

      $exactadminfile= $adminfiles | ? {$_.Name -eq $userfile.Name} |Select -First 1
      $filetext1=[System.IO.File]::ReadAllText($exactadminfile.FullName)
      $filetext2=[System.IO.File]::ReadAllText($userfile.FullName)
      $equal = $filetext1 -ceq $filetext2 # case sensitive comparison

      if ($equal) { 
        Write-Host "Checking == : " $userfile.FullName 
        continue; 
      } 

      if($exactadminfile.LastWriteTime -gt $userfile.LastWriteTime)
      {
         Write-Host "Checking != : " $userfile.FullName " >> user"
         Copy-Item -Path $exactadminfile.FullName -Destination $userfile.FullName -Force
       }
       else
       {
          Write-Host "Checking != : " $userfile.FullName " >> admin"
          Copy-Item -Path $userfile.FullName -Destination $exactadminfile.FullName -Force
       }
}

这是files-to-watch.txt的格式

content\less\_light.less
content\less\_mixins.less
content\less\_variables.less
content\font-awesome\variables.less
content\font-awesome\mixins.less
content\font-awesome\path.less
content\font-awesome\core.less

我想对此进行修改,以便在两个区域中都不存在该文件并打印警告消息时避免这样做。谁能告诉我如何使用 PowerShell 检查文件是否存在?

【问题讨论】:

    标签: powershell powershell-3.0


    【解决方案1】:

    你想使用Test-Path:

    Test-Path <path to file> -PathType Leaf
    

    【讨论】:

      【解决方案2】:

      使用Test-Path:

      if (!(Test-Path $exactadminfile) -and !(Test-Path $userfile)) {
        Write-Warning "$userFile absent from both locations"
      }
      

      将上面的代码放在你的 ForEach 循环中应该做你想做的事

      【讨论】:

        【解决方案3】:

        查看文件是否存在的标准方法是使用Test-Path cmdlet。

        Test-Path -path $filename
        

        【讨论】:

          【解决方案4】:

          您可以使用Test-Path cmd-let。所以像......

          if(!(Test-Path [oldLocation]) -and !(Test-Path [newLocation]))
          {
              Write-Host "$file doesn't exist in both locations."
          }
          

          【讨论】:

            【解决方案5】:

            只是将the alternative 提供给Test-Path cmdlet(因为没有人提到它):

            [System.IO.File]::Exists($path)
            

            做(几乎)与

            相同的事情
            Test-Path $path -PathType Leaf
            

            除了不支持通配符

            【讨论】:

            • @orad 是的,我看到了,发布了一个否定 Exists() 调用的答案,但没有得到相同的积极响应 ;-)
            • 使用[System.IO.File]::Exists 也可以使用resolves relative paths differentlyTest-Path 可以用于非文件路径(例如,注册表位置)。使用Test-Path
            • @Jamie Native .NET 方法通常解析相对于进程工作目录的路径,不一定是 powershell 中的当前 FileSystem 路径。你可以做[System.IO.File]::($(Join-Path $PWD $path))
            • 如果您没有猜到文件夹是 [System.IO.Directory]::Exists($path)。两者都支持我系统上的 UNC 路径,但要进行隐藏共享,请记住将路径中的 $ 转义为“`$”
            【解决方案6】:

            Test-Path 可能会给出奇怪的答案。例如。 “Test-Path c:\temp\ -PathType 叶”给出假,但“Test-Path c:\temp* -PathType 叶”给出真。伤心:(

            【讨论】:

              【解决方案7】:
              cls
              
              $exactadminfile = "C:\temp\files\admin" #First folder to check the file
              
              $userfile = "C:\temp\files\user" #Second folder to check the file
              
              $filenames=Get-Content "C:\temp\files\files-to-watch.txt" #Reading the names of the files to test the existance in one of the above locations
              
              foreach ($filename in $filenames) {
                if (!(Test-Path $exactadminfile\$filename) -and !(Test-Path $userfile\$filename)) { #if the file is not there in either of the folder
                  Write-Warning "$filename absent from both locations"
                } else {
                  Write-Host " $filename  File is there in one or both Locations" #if file exists there at both locations or at least in one location
                }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-11-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-09-24
                • 2018-09-24
                相关资源
                最近更新 更多