Ik heb dit script dat bestanden in twee delen van de schijf vergelijkt en het nieuwste bestand over de ene kopieert met de oudere gewijzigde datum.
$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
}
}
Hier is het formaat van bestanden-to-horloge.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
Ik zou dit willen wijzigen zodat dit wordt voorkomen als het bestand niet in beide gebieden bestaat en een waarschuwingsbericht afdrukt. Kan iemand me vertellen hoe ik kan controleren of een bestand bestaat met PowerShell?
Antwoord 1, autoriteit 100%
Gewoon om aan te bieden het alternatiefvoor de Test-Path
cmdlet (omdat niemand het heeft genoemd):
[System.IO.File]::Exists($path)
Doet (bijna) hetzelfde als
Test-Path $path -PathType Leaf
behalve geen ondersteuning voor jokertekens
Antwoord 2, autoriteit 34%
Gebruik Testpad:
if (!(Test-Path $exactadminfile) -and !(Test-Path $userfile)) {
Write-Warning "$userFile absent from both locations"
}
Het plaatsen van de bovenstaande code in uw ForEach
-lus zou moeten doen wat u wilt
Antwoord 3, autoriteit 11%
U wilt Test-Path
gebruiken:
Test-Path <path to file> -PathType Leaf
Antwoord 4, autoriteit 4%
De standaardmanier om te zien of een bestand bestaat, is met de cmdlet Test-Path
.
Test-Path -path $filename
Antwoord 5, autoriteit 3%
U kunt de cmd-let Test-Path
gebruiken. Dus zoiets als…
if(!(Test-Path [oldLocation]) -and !(Test-Path [newLocation]))
{
Write-Host "$file doesn't exist in both locations."
}
Antwoord 6
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
}
}
Antwoord 7
Testpad kan vreemd antwoord geven. B.v. “Testpad C: \ Temp \-Pathtype Leaf” geeft false, maar “Test-Path C: \ Temp *-Pathtype Leaf” geeft waar. SAD: (