2017-09-22 31 views
0

假設我們有一個包含文件完整路徑的文本文件。第二個文本文件只包含相同的文件名,但它們可以以其他字符作爲前綴。比較兩個文本文件並替換powershell中包含常用詞的行

我想要做的是將文件名替換爲第二個文件,其中包含第一個文本文件中的完整路徑。如何做到這一點?

例如;

在textFile1.txt你有: enter image description here

在textFile2.txt你有: enter image description here

我需要一個第三textFile3.txt像這樣(只是手動使其成爲兩個文件,但我需要爲所有在樹中的文件) enter image description here

+0

可以請你一些例子更新你的問題? –

回答

1

假設文件名是唯一的,並且路徑不包含任何文件名的:

# Read files 
$File1 = Get-Content <file1> 
$File2 = Get-Content <file2> 

# Process each line 
Foreach ($Line in $File1) { 
    # Get only the filename for current line 
    $FileName = $Line.Split("\")[-1] 
    # Replace filename in file2 with the entire line of file1 
    $File2 = $File2 -replace $Filename,$Line 
} 
# Write new content to file2 
Set-Content -Path <file2> -Value $File2 

注:這是untestet,可能包含錯誤;)

相關問題