PowerShell: Copy all folders with a specific name from .txt file?
$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
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
Answers (1)
Please log in to answer
Posted by:
refLye
9 years ago
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.
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"}
} - rad33k 10 years ago
https://drive.google.com/file/d/0BzYq22atosQPS09QUG9kdVJELWs/edit?usp=sharing - mlubbers 10 years ago
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. - rad33k 10 years ago