जाँच करें कि क्या कोई फ़ाइल मौजूद है या Windows PowerShell में नहीं है?


112

मेरे पास यह स्क्रिप्ट है जो डिस्क के दो क्षेत्रों की फाइलों की तुलना करती है और पुराने संशोधित तारीख के साथ नवीनतम फाइल को कॉपी करती है।

$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

मैं इसे संशोधित करना चाहूंगा ताकि यह करने से बचा जाए यदि फ़ाइल दोनों क्षेत्रों में मौजूद नहीं है और चेतावनी संदेश प्रिंट करता है। क्या कोई मुझे बता सकता है कि पावरशेल का उपयोग करके कोई फ़ाइल मौजूद है तो मैं कैसे जांच सकता हूं?

जवाबों:


199

बस cmdlet के विकल्प की पेशकश करने के लिए (क्योंकि किसी ने भी इसका उल्लेख नहीं किया है):Test-Path

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

करता है (लगभग) के रूप में एक ही बात है

Test-Path $path -PathType Leaf

वाइल्डकार्ड वर्णों के लिए कोई समर्थन नहीं है



1
@orad हां, मैंने इसे देखा, एक उत्तर एक उपेक्षित एक्ज़िस्ट () कॉल के साथ पोस्ट किया, लेकिन एक ही सकारात्मक प्रतिक्रिया के साथ नहीं मिला ;-)
मथियास आर जेसेन

5
का उपयोग करना [System.IO.File]::Existsभी सापेक्ष पथों को अलग तरीके से हल करता है , और Test-Pathगैर-फ़ाइलपैथ्स (जैसे, रजिस्ट्री स्थान) के साथ उपयोग किया जा सकता है। का उपयोग करें Test-Path
jpmc26

2
@ जैमी नेटिव .NET विधियाँ आमतौर पर प्रक्रिया की कार्यशील निर्देशिका के सापेक्ष पथों को हल करती हैं, जरूरी नहीं कि वर्तमान फ़ाइलशैल पथ को पॉवरशेल में रखा जाए। आप कर सकते हैं[System.IO.File]::($(Join-Path $PWD $path))
मैथियास आर जेसेन

1
और अगर आपको लगता है कि यह [System.IO.Directory]::Exists($path)फ़ोल्डर्स के लिए नहीं है । दोनों मेरे सिस्टम पर यूएनसी रास्तों का समर्थन करते हैं, लेकिन छिपे हुए शेयरों $को रास्ते में "` $ "के रूप में भागने के लिए याद रखना
क्रिस रुड

71

परीक्षण-पथ का उपयोग करें :

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

उपरोक्त कोड को अपने ForEachलूप में रखकर वही करना चाहिए जो आप चाहते हैं



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

टेस्ट-पाथ अजीब जवाब दे सकता है। उदाहरण के लिए "टेस्ट-पाथ c: \ temp \ -PathType लीफ" गलत देता है, लेकिन "टेस्ट-पाथ c: \ temp * -PathType लीफ" सच देता है। उदास :(

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.