2013-12-10 138 views
1

這應該很容易,但我正在試圖找出它的桌子上我的頭。移動名稱與PowerShell文本文件中列出的目錄

我有一個包含目錄名稱列表的文本文件。 例如:

Get-Content C:\file.txt 
yellow 
green 
red 

我想在電子商務的每個目錄中移動:\ DIR1到E:具有文件中列出的名稱\ DIR2。 例如: 如果dir1的目錄爲「綠色,棕色,黃色,黑色」,我想將「綠色,黃色」移動到dir2。

Get-Content C:\file.txt | Foreach-Object -Process rename/move "E:\dir1\"$_ "E:\dir2\"$_ 

以上只是一個猜測,我相信它是離開的。

通常在Bash中我會寫一個腳本,看起來像下面這樣。

For dir in `cat ./file.txt` 
do 
    mv ~/$dir /old/$dir 
    echo "moving ~/$dir to /old/$dir" 
done 

謝謝!

回答

4

我覺得你非常接近

試試這個:

Get-Content C:\file.txt | 
     Foreach-Object { 
       move-item -path "E:\dir1\$_" -destination "E:\dir2\$_" 
     } 
+1

+1 - 請注意,如果在file.txt的一個空行,將方向1複製到方向2。 – dugas

0
Get-content .\File.txt | ? { $_.Trim() -ne [string]::Empty } | ForEach-Object { Move-Item -WhatIf "E:\Dir1\$_" "E:\Dir2\$_" } 
相關問題