Powershell: Multiple subfolders named the same. Want all tied to Path variable - powershell

Is there a simple way of taking multiple subfolders under the same root and assigning it to a single variable?
My folder structure looks like the following
C:\Unsigned Items\City A\Jeff\Signed
C:\Unsigned Items\City B\Erik\Signed
C:\Unsigned Items\City C\Dave\Signed
I want to assign all of the subfolders named "Signed" to a single variable, such as $srcRoot
I started by just adding each path like the following. It works, but seems inefficient as I have about 50 "cities".
$srcRoot = "C:\Unsigned Items\City A\Jeff\Signed","C:\Unsigned Items\City B\Erik\Signed" ect

So I gather you want an aray of directory fullnames from a source folder where two of the subfolder paths have different named, but all contain a folder called Signed.
Just use Get-ChildItem starting with the path all folders have in common (C:\Unsigned Items) and for the subfolders with different names simply use the wildcard asteriks.
Then take the FullName property from the resulting DirectoryInfo objects:
$srcRoot = (Get-ChildItem -Path 'C:\Unsigned Items\*\*\Signed' -Directory).FullName

Related

Strange Powershell GCI Recurse Results with Wildcard

I'm trying to use GCI with the Recurse argument to get a list of all files WITHIN the subfolders of a specified path.
I'm using the following line:
gci 'C:\temp\TestRecurse\*\*' -Recurse
Underneath the TestRecurse folder, I have the following:
TestRecurse
|---b.txt
|---dir1
|------a.txt
I expect a.txt to be returned. However, I'm getting a.txt and b.txt. Stranger still to me, if I put a.txt into another subfolder:
TestRecurse
|---b.txt
|---dir1
|------dir2
|---------a.txt
The same statement above only returns a.txt. I'm baffled as to how messing with the location of a.txt changes when b.txt is returned. Can someone explain to me why this happens, and why b.txt is ever returned at all?
Update
I should mention, while they're appreciated, I'm not necessarily looking for a workaround. This is part of a larger script in our environment that is in charge of moving files around in various ways while trying stay flexible. It's not behaving as I expected it would, so I'm trying to understand why it's working the way it is. As pointed out by PetSerAl, understanding Get-ChildItem may be more trouble than it's worth.
Thanks!
You're including a wildcard for the parent directory (TestRecurse\*), so you are getting files contained in it as well. Try getting the folder list of the TestRecurse, then iterating through them.
Structure:
TestRecurse\b.txt
TestRecurse\dir1
TestRecurse\dir1\a.txt
Code:
Get-ChildItem 'C:\tmp\TestRecurse\' | ` # Get the list of items within TestRecurse
? {$_.PSIsContainer} | ` # Filter items that are folders
% {Get-ChildItem $_.FullName -Recurse} # Iterate through and get all items within those folders
This only returns folders and files within dir1, but not dir1 itself.

Get-ChildItem: Same subfolder in different parents folders

If have the following structure:
F:\ParentA\FindMe
F:\ParentA\DoNotFindMe
F:\ParentB\FindMe
F:\ParentB\SomethingElse
So in every parent folder (lots of them) i have subfolders and there is exactly one of those subfolders with the same name every time i want to find.
I thought about $children = Get-ChildItem "F:\*\FindMe" but this only returns the first (in the top case parentA) but as i said i need every parent folder.
Looping through is an alternative, but I was looking for a sleek/quick solution..

Simple script to move files from one location to another

I need help with creating a power-shell script that copies folder recursively from one location to another. Here is how I plan to do it:
There will be a list of folders to copy, which are combination of name and ID number.
Create an array to store variables.
Create a loop that will look for each variables in array and copy the folders to another location.
Here is my code to copy a single folder, but I need to make it more feasible so that it can copy folders depending on variables:
$AID = (4069302,4138482)
foreach ($number in $AID ) {
Get-ChildItem "C:\Users\sshres19\Desktop\Script\GAEBox" -Recurse -Filter "*$number*" |
Copy-Item -destination "C:\Users\sshres19\Desktop\Script\Reg"
}
The script needs to copy all folders and files within given condition.
Adding -Recurse to your Copy-Item will make it copy a folder and it's contents.

How to find folders efficiently with specific name using powershell?

I want to use powershell to search for folders with specific name in some path, I have this:
get-childitem -path $path -Recurse -Directory -filter $folderName |
foreach{
write-host $_.FullName
}
It works but it is very slow, because there are a lot of files to search for. The case I am dealing is that there are huge amount of files inside the folder I want to find itself. It is wasting time to check for all these files. So I am wondering if there is a way to not dig into this folder when the folder name matches what I want to search for. Cannot do it by removing -recurse tag because the folder I want to search is not necessarily just inside $path but maybe some levels down.
Thanks!
Assuming you have access to all folders in the path, you could use Directory.GetDirectories():
$recurse = [System.IO.SearchOption]::AllDirectories
[System.IO.Directory]::GetDirectories($path,$folderName,$recurse)

How do I move files from an old folder structure to new?

I want to modify my existing folder structure. I had a file tree that was organized in the following way:
Client Name
State/Province
City
Order Number
But I have modified it to add an address before the order number separated by a hyphen, as such:
Client Name
State/Province
City
Order Number - Address
I created the new folder structure with a macro I had that generated the original, and I figured this was much easier than renaming the existing folders.
So now I want to upload the empty folders to my server, but before I do that I want to take the files from all the old structure and put them into the new one.
Simply, I am trying to write a script to match the folder name in the original hierarchy to the new hierarchy that contains the original name plus address, and then copy the files in the original folder to the similarly named one in the new structure.
How would I do this? VBA, Powershell, Batch Command? I'm not very well versed in PS.
Use a foreach loop with the Get-ChildItem,
Copy-Item,
and Test-Path Powershell cmdlets:
#Get all address subfolders
$addr_folders = Get-ChildItem c:\"Client Name"\State/Province\City\ -recurse | Where-Object {$_.PSIsContainer -eq $True}
#Loop through all address subfolders
foreach ($address in $addr_folders)
{
#Copy contents from existing subfolder path to new folder
if (Test-Path c:\"Client Name"\State/Province\City\"Order Number"\$address)
{
Copy-Item c:\"Client Name"\State/Province\City\"Order Number" c:\"Client Name"\State/Province\City\"Order Number - " $address
}
}