Report folders which have a specific subfolder - powershell

I am building a script which I use to deploy files to multiple specific folders.
The destination folders are collected using this part.
$destinations = Get-ChildItem "C:\this\is\*\my\path\"
So my script replaces only if the folder has the subfolders "\my\path\"
If I now check my variable it will return the fullpathes but I only need the folder name. I tried using select -path to show at least only the path but it returned as well the length, mode etc.
my goal is to return only values like this:
folder 1
folder 2
folder 3
I am using powershell 3.0

So if we are checking for folders that have the child structure folder1\folder2 where the parent folder is in C:\Temp then we would do something like this:
$destinations = (Get-Item "C:\Temp\*\folder1\folder2").Parent.Parent.Name
Get-Item "C:\Temp\*\folder1\folder2" would just return System.IO.DirectoryInfo objects for folder2. We take those objects and find their grandparent folders and just return their names only.

Related

Listing unwanted files recursively in a directory - Powershell

I am trying to make a Powershell script that will display a list of files and folders in a directory('s) while not displaying a specific folder and a file.
I have a bunch of directories on the C:\ that have a folder "DATA" and a "Setup.ini" file within them.
Every once in a while people go into the data folder and make copies and then place those copies of files and directories right inside of the main directory with the "DATA" and ".ini" files.
Then at the end of each month, I normally need to go through each of these directories and delete anything that is not the "DATA" folder and ".ini" file.
Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini' | Where-Object { $_.FullName -notmatch 'Data'}
This seems to sort of work and is what I have been using this month.
It runs really slow. I also noticed that one of the Directories had 2 extra folders and a zip file within it.
The script only showed one of the folders and didn't show the zip file either.
I have tried adding other things to this code, but when I do that it begins showing me data inside of the "DATA" folder which I don't want.
Any idea what I could be doing wrong or ways to make this run quicker?
I think it is taking about 7-10mins to run through 113 directories.
Thank you!
Expand the -Exclude and drop the Where-Object:
Get-ChildItem -Recurse "C:\TestDir" -Exclude 'Setup.ini','Data'
HTH

Get folder's content in an usable variables

Is it possible to get all files names from a directory in variables ?
Consider this environment :
Dir/File.json
Dir/File7.json
Dir/File58.exe
Is it possible so that i can get File, and File7 (only the file with .json extension) in one or two variables that i'd use later in my code ?
I test dir > test.txt but : It show everything including folder or files with an other extension, and i don't know if i can then use this .txt file to get back the names individualy.
Using PowerShell
$FileNames = Get-ChildItem -Path 'C:\Dir' -Name *.json
This will return all json files in the folder C:\Dir

Loop subfolders with different parent folder

I have a folder structure that consists of multiple client names on the root of a drive. Within each of these client folders I have the same subfolder. What I'd like to do is loop through each of the client folders and for a specific sub folder within each of the client folders, I'd like to set specific permissions using the Get-Acl command in PowerShell.
I need the script to ignore the client's name (JOHN, PETER) but loop through each to change the permission to the PRIVATE folder or all folders within the Special File folder:
S:\CLIENT FILES\JOHN\Special File\PRIVATE
S:\CLIENT FILES\PETER\Special File\PRIVATE
Get-ChildItem allows the use of wildcards in the -Path parameter.
Get-ChildItem -Path 'S:\CLIENT FILES\*\Special File\PRIVATE' | Get-Acl

Zipping folders in powershell

Hope fellow scripters can help with this one :) Been breaking my head around the problem for few hours now.
I'm trying to zip up certain folders using powershell.
My folder structure is
Backups
BoxIntranet
Components
Content
Database
Exec
Files
Logs
Multibrowser
Multibrowser\Legacy\Customisation
Packages
ParentPortal
ParentPortal\customisation
StudentPortal
StudentPortal\customisation
Update
WebDav
There are a lot more files and folders in every one of the above but these are the ones I'm mainly interested in.
I am trying to zip it all up using either Write-Zip or Compress-Archive methods in PowerShell but my conditions are.
Only Content, Files, Database folders should be zipped from root
Multibrowser\Legacy\customisation, StudentPortal\Customisation and ParentPortal\customisation folders should also be backed up.
Folder structure should remain the same in the zip file meaning that Root of the zip file should have Content, Files, Database, Multibrowser, ParentPortal and StudentPortal folders. Whilst Content, Files and Database folders should have everything zipped up, Multibrowser, ParentPortal and StudentPortal folders should only have the specified sub directories and all files within them.
Code:
$FilesAndInclude = #("Content", "Files", "Database", "Multibrowser\Legacy\customisation",
"StudentPortal\customisation", "ParentPortal\customisation",
"BoxIntranet\customisation")
$FilesToExclude = #("connectionstrings.config", "inc_dbconn.asp")
Get-ChildItem -Path "C:\Folder" -Include $FilesAndInclude -Recurse -Exclude $FilesToExclude|
Compress-Archive -DestinationPath "Archive.zip"
I've tried the above and it doesn't do anything however if I remove the -Include parameter then it zips up everything however doesn't retain folder structure.
Is there any way to complete what I am after within powershell?
Ok, first things first, the reason that you are having a hard time using the -Include parameter is because it is designed to exclusively include only the things you specify. As such, it will look at the name of things (not their path), and check against the list and if it matches something in the list it will include that item. Since you only list folder names it is only including those folders (but not their contents). So you aren't getting any files passed down the pipe this way. To get around that you'll need to build your file list first, then pipe it to the cmdlet to zip things up.
Next issue is that Compress-Archive doesn't store path info, so you'll need to use Write-Zip. I have included what I think you would want for that cmdlet.
$FilesAndInclude = #("Content", "Files", "Database", "Multibrowser\Legacy\customisation",
"StudentPortal\customisation", "ParentPortal\customisation",
"BoxIntranet\customisation")
$FilesToExclude = #("connectionstrings.config", "inc_dbconn.asp")
[array]$FilesToZip = Get-ChildItem .\* -Exclude $FilesToExclude -File
$FilesToZip += $FilesAndInclude | ForEach{Get-ChildItem .\$_ -Exclude $FilesToExclude -File}
$FilesToZip | Write-Zip -EntryPathRoot $(Resolve-Path .\|Select -Expand Path) -OutputPath Archive.zip

Creating folders and moving files in powershell

I have a folder with thousands of files (let's say .txt) with different names:
VBH_V001.txt
VDD_V001.txt
DTG_V001.txt
ADC_V001.txt
DFD_V001.txt
etc....
I need to create directories in that folder with the name of each file and then move those files to directories. The result should be for example Folder with the the name VBH (without _V001.txt) and in that folder I should have VBH_V001.txt file. How can I do that. Please advise.
cd <path to your folder>
$files = Get-ChildItem -file;
ForEach ($file in $files)
{
$folder = New-Item -type directory -name $file.BaseName;
Move-Item $file.FullName $folder.FullName;
}
This script creates a directory for each file and moves the file to this directory. To exclude _V001 from the directory name, you can call the TrimEnd method on $file.BaseName -
$file.BaseName.TrimEnd ("_V001")
Step by step.
First of all, go to the directory that contains your files.
Get a list of objects that represent your files by using the Get-ChildItem cmdlet with the -file attribute. These objects contain properties - such as BaseName, FullName, and so on.
Save this list to the $files variable.
Loop through the list with ForEach.
In the ForEach body, create a directory for each file with the New-Item cmdlet. The -type attribute specifies the type for the new item (-type directory), for the -name attribute substitute the $file.BaseName property. BaseName property returns a string with the name of the file without the extension and path.
Save the object of the newly created directory into the $folder variable.
Move the file using the Move-Item cmdlet. This cmdlet requires two attributes: source path and destination path. As the source path, use the FullName property of the file object, and the FullName property of the directory object for the destination path. FullName property returns the name of the file or directory that contains the full path, for example D:\directory\file.txt for a file and D:\directory\anotherDirectory for a directory.
It's not a big deal, actually, and without shortcuts it looks like a plain English.
What you basically need to do, is to:
Get a list of files in a selected folder through PowerShell.
Create new folders in a loop by
using the New-Item cmdlet which have name made by using substring of a name of a selected file.
For each of the files, move the file to the new location, using the Move-Item cmdlet.
Hope that helps.