developer tip

Windows PowerShell에 파일이 있는지 확인 하시겠습니까?

copycodes 2021. 1. 9. 10:12
반응형

Windows PowerShell에 파일이 있는지 확인 하시겠습니까?


디스크의 두 영역에서 파일을 비교하고 이전 수정 날짜가있는 파일 위에 최신 파일을 복사하는이 스크립트가 있습니다.

$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을 사용하여 파일이 있는지 확인하는 방법을 알려줄 수 있습니까?


cmdlet에 대한 대안 을 제공 하기 위해 (아무도 언급하지 않았으므로) :Test-Path

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

(거의) 같은 일을

Test-Path $path -PathType Leaf

와일드 카드 문자에 대한 지원 없음을 제외하고


사용 테스트 경로 :

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

ForEach루프에 위의 코드를 배치 하면 원하는 작업을 수행 할 수 있습니다.


Test-Path를 사용하려고합니다.

Test-Path <path to file> -PathType Leaf

The standard way to see if a file exists is with the Test-Path cmdlet.

Test-Path -path $filename

You can use the Test-Path cmd-let. So something like...

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

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

Test-Path may give odd answer. E.g. "Test-Path c:\temp\ -PathType leaf" gives false, but "Test-Path c:\temp* -PathType leaf" gives true. Sad :(

ReferenceURL : https://stackoverflow.com/questions/31879814/check-if-a-file-exists-or-not-in-windows-powershell

반응형