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
Related
I am trying to zip files that are in directories that have subdirectories and I can't figure out how to zip the files and not the subdirectories.
Here is the current setup:
C:\users\user\appdata\local\folder\
Inside of this folder, I need 3 out of the 20 or so folders that are in there so I used the Get-Childitem to accomplish this:
GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*}
Now that I have that, I don't want the subdirectories and just want the files that are sitting in the folder itself. I have not found a way to do this, but I have gotten close with using this:
& "C:\program files\7-zip\7z.exe" "a" "D:\TestBackup\Zipfile.7z" (GCI C:\users\user\appdata\local\folder | ? {$_.name -like "*folder*} | select -expandproperty FullName)
But this gives me the entire contents of the folder. I want to keep the structure so that it looks like this:
folder 1\files
folder 2\files
folder 3\files
I hope I am explaining myself well. The files are all different types of extensions so I was wanting a blanket way to do this or to exclude the subdirectories when zipping.
I had to consult the FAQ to get this right:
7-Zip stores only relative paths of files (without drive letter
prefix). You can change current folder to folder that is common for
all files that you want to compress and then you can use relative
paths:
cd /D C:\dir1\
7z.exe a c:\a.7z file1.txt dir2\file2.txt
Solution:
# Set base directory that is common for all files
Push-Location 'C:\users\user\appdata\local\folder'
# Get names of directories that match filter
$folderNames = (Get-ChildItem -Directory -Filter '*folder*').Name
# Call 7-zip, passing the list of directory names.
& 'C:\Program Files\7-Zip\7z.exe' a 'D:\TestBackup\Zipfile.7z' $folderNames
# Restore current directory
Pop-Location
Remarks:
Push-Location sets the current directory, while Pop-Location restores the previous current directory. Changing the current directory is crucial for this solution, as explained by the 7-zip FAQ. It is also the only way to set the base directory for Resolve-Path -Relative.
Pass -Directory or -File to Get-ChildItem if you are only interested in either directories or files.
Use -Filter instead of Where-Object (alias ?) if you only need simple wildcard filtering. -Filter is faster because it uses the FileSystem provider which filters at a lower API level, which avoids PowerShell overhead.
I am trying to copy music lyric files (.lrc) from a folder with many subfolders such as artists and albums and paste them in an empty folder, but I still want to keep the folder structure without having to create each individual folder for those files to be put in. In other words: I want to take certain files from a folder and have it automatically create the folder structure in another folder which the original file was in.
For example: I have 10 lyric files accompanied with other music files in a single folder called "ArtistName" which is a subfolder of a subfolder of a folder called "Music". These lyric files need to be in another folder called "Music2" that is currently empty, but instead of just dumping the files in the root folder, I need to recreate the folder structure in which the original lyric files were in.
Is there any way to do this? Keep in mind, I am not very experienced when it comes to programming but I know some basics. Unfortunately I might need more of an explanation than most people here. Thank you to anyone who can help!
Here's a powershell answer:
Use Copy-Item with the -Container and -Recurse switches to copy the folder structure including files to a new location.
Copy-Item -Path "Old\Path\for\Music" -Recurse -Destination "New\Path\for\Music" -Container
If you only want to copy the lyric files while retaining the folder structure use a -Filter
Copy-Item -Path "Old\Path\for\Music" -Recurse -Filter "*.lrc" -Destination "New\Path\for\Music" -Container
You can do this in CMD
xcopy /s yourFolder\Subfolder\*.pdf destinationFolder\myfolder
powershell, recurse option keeps the folder structure
copy-item c:\\srcFolders\\* f:\\dst -force -recurse -verbose
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
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.
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.