windows 为什么它不改变文件中的内容?

sauutmhj  于 2023-02-16  发布在  Windows
关注(0)|答案(1)|浏览(136)

此代码假定查找磁盘中的所有demo.txt并将其从“demo”更改为“demodemo997182625”,然后检查文件是否已更改

$found = 0;
$notfound = 0;

foreach ($file in Get-ChildItem -Path C:\ -Recurse -Filter demo.txt -ErrorAction SilentlyContinue )
{
  (Get-Content $file).Replace("demo","demo997182625") | Set-Content $file

   $x = (Get-Content $file).contain("demo997182625")

   if($x -eq $null){

  $found = 1 + $found;
  }

  else {

  $notfound = 1 + $notfound;
  
  }

}            
Write-Host "Changed" $found;
Write-Host "Not Changed" $notfound;
s8vozzvw

s8vozzvw1#

A few remarks on your code:

  • the .Replace() and .Contains() string methods work case-sensitive, so .Replace("demo","demo997182625") won't find and replace "Demo". To have it work case-insensitively, use the -replace operator instead.
  • updated files can be reprocessed by Get-ChildItem, unless you have that part finish completely first. The easiest way to do that is by enclosing it between brackets
  • I would only save the file if there was something updated (i.e. the new value was found after -replace ), otherwise leave it be
  • Get-ChildItem returns both FileInfo and DirectoryInfo objects. Since you are interested in changing files only, append the -File switch
  • best use the FullName property of the found file on the Get- and Set-Content cmdlets instead of the whole FileInfo object
$found = $notfound = 0

# surround the Get-ChildItem line with brackets, so it will finish before iterating on the result
# otherwise, it could reprocess files that were allready updated
foreach ($file in (Get-ChildItem -Path 'C:\' -Recurse -Filter 'demo.txt' -File -ErrorAction SilentlyContinue)) {
    # -replace uses regex, so surround the search string with '\b' boundary markers in order to do a whole-word search
    $content = (Get-Content -Path $file.FullName -Raw -Force) -replace '\bdemo\b', 'demo997182625'
    # test if the content now has the new value (again, use '\b' boundary markers)
    if ($content -match '\bdemo997182625\b') { 
        # save the updated file
        $content | Set-Content -Path $file.FullName -Force
        $found++ 
    } 
    else {$notfound++}

}            
Write-Host "Changed: $found"
Write-Host "Not Changed: $notfound"

P.S. If your search string contains characters that in regex have special meaning (see table below), you need to escape these with a backslash when using regex operators -replace and -match .

Special Characters in Regex

CharDescriptionMeaning
\BackslashUsed to escape a special character
^CaretBeginning of a string
$Dollar signEnd of a string
.Period or dotMatches any single character
|Vertical bar or pipe symbolMatches previous OR next character/group
?Question markMatch zero or one of the previous
*Asterisk or starMatch zero, one or more of the previous
+Plus signMatch one or more of the previous
( )Opening and closing parenthesisGroup characters
[ ]Opening and closing square bracketMatches a range of characters
{ }Opening and closing curly braceMatches a specified number of occurrences of the previous

相关问题