Get-ChildItem: Same subfolder in different parents folders - powershell

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..

Related

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

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

Powershell: Rename a folder when only partial name is known

I need to rename a folder without knowing the full folder name.
For Example C:\myfolder-2021-5-1 (I know the first part of the folder name)
I would like to rename it to c:\myfolder... Again, the script wont always know the full folder name.
Edit: I am new to Powershell. I have spent a few hours looking on Google and I don't see examples of people trying to rename a folder using a wildcard. There are very few folder renaming examples that I could find. Most of what I find pertains to renaming files not folders.
I get it people wanting me to "try" first and then ask questions. But, sometimes, especially for us newbies, we don't even know where to start.
I tried using several filename examples and just using a directory name with a wildcard and that did not work.
Don't know what else to say.
Any ideas?
Thanks!
You cant rename a directory if you dont know it's name - thats just not how files and directories work. What you can do is search for a directory that matches your criteria, then you can rename it.
(I'm assuming the reason you dont know the exact name is because its a date, but im also assuming the format of the directory name is consistent)
At its most basic level, this would work for your example:
get-childitem -Path c:\ -Directory -Filter "myfolder-????-*-*" | Move-Item -Destination "renamed-myfolder"
This will search for directories in C:\ that match the pattern "myfolder-????-*-*" - so this pattern would match your example folder C:\myfolder-2021-5-1. Then pass that Directory down the pipeline (|) into Move-Item where the directory is renamed to c:\renamed-myfolder.
This code has some major drawbacks though! It doesnt check if the new name exists before trying to rename the directory so it might fail. Also if more than 1 folder matches the filter only the first rename (Move-Item) will succeed. Its upto you to think about these edge cases and add suitable logic to detect/prevent them.
Its a good idea to use Test-Path command to check if the destination name already exists or not:
if(Test-Path -Path c:\renamed-myfolder){
throw "ERROR - c:\renamed-myfolder already exists!";
}
NOTE: Get-ChildItem filters * means "any characters" and ? means "one character" so "myfolder-*-*-*" would also work, but if the year is always 4 digits then use the ???? as its more specific. Ive used the filter "myfolder-????-*-*" as im assuming some days/months will be 2 digits like myfolder-2021-12-12.

moving files into matching directories

looking for some help please - I have no experience in code writing so have been looking for a question/answer that is close but.....
My huge movie database lives on NAS drive "Video Y", each movie in its own subdirectory; it has multiple video file types, most being .avi, and I wanted to convert all the .avi to .mp4 (some devices will not play .avi").
So I filtered out all the .avi files and put them in one new directory "0 temp holder for avi", so I could use VideoProc to convert; this converted and placed the .mp4 files in one new directory "00 temp holder MP4".
Now I want to move the .mp4 files back in their own original sub directory which still contains various files related to the movie like .srt etc.
I think the simplest way for me is lining up the files in alphabetical order and the directories in the same order, (as directory names and file names are not necessarily exactly the same), checking for mismatches and correcting as needed, and using some code to move the first file to first directory, and iterate from there. But I'm still stumped and not sure to go about it.
I put under the Windows10 and Powershell tag, but someone may assist with more accurate tags please.
Directory layout
Shown below at the beginning of the PowerShell script, the $arrVideoFolders variable is to load all the folders names into an array.
The $arrFolder variable is for the filenames of all your video files in a separate array.
Shown below,
$arrVideoFolders = Get-ChildItem (Folders for the videos) |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.Name}
$arrFolder = Get-ChildItem (Video file names) |
Where-Object {$_.PSIsContainer} |
Foreach-Object {$_.Name}
To give the logic of how i would write the rest of the PowerShell script.
Afterwards a foreach loop would be used to go through all folders, and foreach folder a second nested foreach loop would loop through your video files. An if statement would test, the video name is like, your folder name. This is because your video files names are similar, once found you can do a copy and paste, or cut and paste.
For testing maybe use folders, and simple empty text files instead of copying large files, and test in different folders.

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.

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.