mpmackenna
mpmackenna

Reputation: 443

Move Directories that have names that are listed in a text file with Powershell

This should be easy but I am banging my head on the table trying to figure it out.

I have a text file with a list of directory names. For example:

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

I want to move every directory in E:\dir1 to E:\dir2 that has a name listed in the file. For example: If dir1 has directories "green,brown,yellow,black" I want to move "green,yellow" to dir2.

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

The above is just a guess and I am sure it is way off.

Normally in Bash I would write a script that would look something like the following.

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

Thanks!

Upvotes: 1

Views: 2151

Answers (2)

dugas
dugas

Reputation: 12443

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

Upvotes: 0

Mike Shepard
Mike Shepard

Reputation: 18156

I think you were pretty close

Try this:

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

Upvotes: 4

Related Questions