/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


PowerShell: Copy all folders with a specific name from .txt file?

09/29/2014 7712 views
$applications = get-content c:\applications.txt
foreach-object {Get-ChildItem "\\sdlcorpwwps01\WiseSharePoint\Projects" -Recurse | Where-Object {($_.PSIsContainer) -AND $_.NAME -eq $applications} | Move-Item -Destination "\\sdlcorpwwps01\smspkgsource$\Archives\Projects"}

Every time I run this command it wants to copy all the folders. I will be using this script to automate our archiving process. Any help?

Thanks! :D
1 Comment   [ + ] Show comment

Comments

  • Hi,
    I'm just starting my PowerShell adventure so my suggestion is basing on my VBS knowledge. I think that there might be a problem with $applications array and its usage in -eq operator. Usually comparison is done between two items so based on VBS syntax I would try with additional foreach to enumerate $applications list:

    $applications = get-content "c:\applications.txt"
    foreach ($App in $applications){
    foreach-object {Get-ChildItem "\\sdlcorpwwps01\WiseSharePoint\Projects" -Recurse | Where-Object {($_.PSIsContainer) -AND $_.NAME -eq $App} | Move-Item -Destination "\\sdlcorpwwps01\smspkgsource$\Archives\Projects"}
    }
    • This worked for one of the folders but I need all of then in my .txt file to be moved.

      https://drive.google.com/file/d/0BzYq22atosQPS09QUG9kdVJELWs/edit?usp=sharing
      • Are there any errors in the PowerShell console?
        I've tested it with local paths and worked fine for all your folders.
        Maybe the problem is with Move-item cmdlet and network paths:
        http://stackoverflow.com/questions/6138849/how-do-you-move-files-folders-across-volumes-with-powershell
        EDIT:
        My another thought is maybe you didn't wait for the completion of the job - as there are over hundred of entries in TXT file and probably much more directories in your REPO it probably will take awhile to finish.

All Answers

0
Here is a suggestion:

$baseDir = "E:\test"
$targetDir = "E:\test\target"
$toCopyPath = "E:\test\tocopy.txt"

$toCopy = Get-Content $toCopyPath

$toCopy | ForEach-Object {
  Copy-Item "$baseDir\$_" $targetDir -Recurse -Force
}


Content of tocopy.txt

folderb
folderc


The folder hierarchy:

E:

├───test
│   │   copy.ps1
│   │   tocopy.txt
│   │
│   ├───foldera
│   ├───folderb
│   ├───folderc
│   └───target
│       ├───folderb
│       └───folderc

The yellow marked folders are created after copy.ps1 has started.

Answered 11/26/2014 by: refLye
White Belt

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ