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


108

我有这个脚本,可以比较磁盘两个区域中的文件,并在修改日期较旧的那个区域中复制最新文件。

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

Answers:


196

只需提供替代Test-Pathcmdlet的(因为没有人提到它):

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

(几乎)做与

Test-Path $path -PathType Leaf

除了不支持通配符



1
@orad是的,我看到了,它以否定的Exists()呼叫发布了答案,但没有得到相同的肯定答复;-)
Mathias R. Jessen

5
使用[System.IO.File]::Exists可以以不同的方式解析相对路径,并且Test-Path可以与非文件路径(例如注册表位置)一起使用。使用Test-Path
jpmc26

2
@Jamie本机.NET方法通常解析相对于进程工作目录的路径,而不必解析Powershell中当前的FileSystem路径。您可以做[System.IO.File]::($(Join-Path $PWD $path))
Mathias R. Jessen

1
而且,以防万一您不认为这是[System.IO.Directory]::Exists($path)文件夹。我的系统上都支持UNC路径,但这样做隐藏共享记得逃脱$路径为“`$”
克里斯陆克文

71

使用测试路径

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

将上面的代码放在ForEach循环中应做您想要的


22

您要使用Test-Path

Test-Path <path to file> -PathType Leaf

7

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

Test-Path -path $filename

6

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

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

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

-4

测试路径可能会给出奇怪的答案。例如,“ Test-Path c:\ temp \ -PathType leaf”给出了false,但是“ Test-Path c:\ temp * -PathType leaf”给出了true。悲伤:(

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.