2017-10-16 58 views
-5

由於我正在處理多個文件,因此遇到了使用其他文件替換多個文件的問題。 我創建了一個項目,其中一個圖像出現在多個文件夾中,但已損壞,我需要用固定的一個來更改它。 爲了清楚起見,我在我的Project目錄中有一個image.jpg,在我的Project目錄中的多個目錄中有多個image.jpg文件,並且我想要查找和更改所有出現的已損壞image.jpg到固定image.jpg。 Wainting迴應!PowerShell,找到並替換目錄中的文件

+0

你到目前爲止試過了什麼?編輯你的問題併發布一些代碼。 – Jelphy

+0

_回覆!_。你嘗試了什麼? [mcve],[問] – Manu

回答

0

大家都投票給你,因爲你沒有付出努力。下面可以用來找到你想要的。這是醜陋而短暫的,但會完成你所要求的。今後請提供更多信息和代碼。

$dir = read-host "Please Enter Directory to Search." 
$nameOfBadFile = Read-Host "The name of the bad file, including extension" 
$goodImg = read-host "Full location of your known good file (C:\somefile.txt)." 
$i=0; 
foreach($file in (Get-ChildItem $dir -Recurse | ?{$_.Name -eq $nameOfBadFile})) 
{ 
    $file.Delete() 
    $i++; 
    Write-Host "Just Deleted an instance of the bad image. You Have deleted $i images so far." 
    $goodimg | Copy-Item -Destination $file.Directory 
    Write-Host "Copied good file to current directory, $file.Directory" 
} 
相關問題