检查Windows PowerShell中是否存在文件?

时间:2015-08-07 14:12:18

标签: powershell powershell-v3.0

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

$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检查文件是否存在吗?

7 个答案:

答案 0 :(得分:88)

只是向https://github.com/rails/turbolinks#events提供the alternative(因为没有人提及):

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

(几乎)与

相同
Test-Path $path -PathType Leaf

,但不支持通配符

答案 1 :(得分:38)

使用Test-Path

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

将上述代码放在ForEach循环中可以执行您想要的操作

答案 2 :(得分:12)

您想使用Test-Path。

Test-Path <path to file> -PathType Leaf

答案 3 :(得分:3)

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

Test-Path -path $filename

答案 4 :(得分:2)

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

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

答案 5 :(得分:0)

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
  }
}

答案 6 :(得分:-2)

测试路径可能给出奇怪的答案。例如。 &#34; Test-Path c:\ temp \ -PathType leaf&#34;给出false,但是&#34; Test-Path c:\ temp * -PathType leaf&#34;给出了真实。伤心:(