2016-11-11 86 views
-1

我有2個文件夾:'舊'和'新'。大多數來自「舊」的文件都已複製到「新建」。然而,「舊」和「新」子文件夾的結構不同,因此「舊」文件的文件路徑與「新建」中的文件的路徑非常不同。循環遍歷'Old'中的每個文件,在'New'中搜索該文件,並將每個文件的新舊文件路徑寫入文本文件。由於文件數量太多,所以我想編寫一個腳本,我是Powershell的新手,很難找出哪些cmdlet可以幫助我完成任務。指導。謝謝。Powershell腳本打印文件夾中的每個文件的文件路徑

+0

我用這個來嘗試打印到一個文本文件中的「新」的所有文件的全稱/路徑: 「的ForEach(以GET-ChildItem -recurse $文件){ $ file.fullname | out-file output.txt}' 但是,這隻寫了一個文件的路徑。 –

+0

你可能想做''foreach($ file in $(gci -recurse)){$ file.fullname >> output.txt}''或者你可以使用''Out-File -append''。那裏會發生什麼,它只輸出一個文件,因爲它每次都會覆蓋您寫入該文件的任何內容。 – 4c74356b41

+0

謝謝,這工作!我現在試圖找出如何檢索每個文件的名稱,在C:\ Old中搜索它,然後將路徑(fullname)附加到output.txt –

回答

0

嘗試這樣的事情

#list old files 
$patholdfile="c:\temp" 
$listoldfile=Get-ChildItem $patholdfile -File -Recurse | select Name, FullName 

#list new files 
$pathnewfile="c:\temp2" 
$listnewfile=Get-ChildItem $pathnewfile -File -Recurse | select Name, FullName 

#extract liste file old and name and search all file in newlist 
[email protected]() 
foreach ($currentfile in $listoldfile) 
{ 
    $resultsearch+=New-Object psobject -Property @{ 
    Name=$currentfile.Name 
    OldPath=$currentfile.FullName 

    #if you want the firt founded uncomment this and comment after 
    #NewPaths=($listnewfile | Where {$_.Name -eq $currentfile.Name} | select FullName -First 1).FullName 

    NewPaths=($listnewfile | Where {$_.Name -eq $currentfile.Name} | select FullName).FullName -join "~" 
    } 

} 

#export result in csv file 
$resultsearch | select name, OldPath , NewPaths | export-csv -Path "c:\temp\result.txt" -NoTypeInformation 
相關問題