Script to move multiple folders base on a text file

rated by 0 users
This post has 2 Replies | 2 Followers

Not Ranked
Posts 2
mike8332 Posted: 04-10-2012 3:59 PM

I just started looking into PS, so my knowledge on PS is close to none.

I need a PS script to 
1. look into a text file on C:\ that contains folder names.
2. use that text file and see if some of those folders are in location A (D:\Folder1\folder2)
3. move those folders that exists in the text file and location A to location B (D:\Folder1)

Thank you in advance.

Top 75 Contributor
Posts 34

Hi there

What about something like this?


$text = gc C:\foldernames.txt
$dir = Get-ChildItem D:\Folder1\folder2


foreach ($item in $text) {
  foreach ($folder in $dir) {
    if ($folder.name -like $item) {
      Move-Item "D:\Folder1\folder2\$item" 'D:\Folder1'
      Write-Host "Folder $item moved successfully"
      [array]$totalitems += $item
      break
      }
    }
  }

""
"Folders moved: $totalitems"

 

You will also have a variable $totalitems with a collection of the folders that were successfully moved.

Hope it helps.

D.

Not Ranked
Posts 2

Thank you I'll give it a try.

Page 1 of 1 (3 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.