Delete files from folder structure with exclude - powershell

currently I am trying to delete files inside a folder structure
root
|
|__subfolder1 (includes files)
|
|__subfolder2 (includes files)
|
etc
The script has to delete all files inside the subfolders except subfolder1 and not delete the subfolders. The thing I am not getting to work is to exclude the files inside of "subfolder1".
I am trying something like this
Get-ChildItem -Path E:\root -Include *.* -File -Recurse -Exclude E:\root\subfolder1 | foreach {$_.Delete()}

Since the subfolder you want to exclude is always directly under the root folder I'd do the processing in 2 steps:
Enumerate the child folders of the root folder and exclude subfolder1.
Enumerate all files from the remaining folders and delete them.
Something like this:
$root = 'root'
$excludes = 'subfolder1'
Get-ChildItem $root -Directory -Exclude $excludes | ForEach-Object {
Get-ChildItem $_.FullName -File -Recurse -Force | Remove-Item -Force
}

Related

Get files from specific folders in a directory in PowerShell

I have 4 folders in a directory. All of those folders contains some files. I want to list the file names only from the two folders.
C:\MainFolder\FolderOne\FileOne.txt
C:\MainFolder\FolderTwo\FileTwo.txt
C:\MainFolder\FolderThree\FileThree.txt
C:\MainFolder\FolderFour\FileFour.txt
I only want to list the files under FolderTwo and FolderThree.
If I use -Recurse -Include "FolderNames" it'll list only the folder names.
$source="c:\MainFolder" #location of starting directory
$files=#("*.txt", "*.doc") #if you want to include extensions add -include ($files) to get-ChildItem
Get-ChildItem -recurse ($source) -File | Where-Object {$_.PSParentPath -match "Two|Three"}
get-childitem -recurse 'foldertwo','folderthree'
Did it like this
$arrPath ='C:\MainFolder\FolderTwo','C:\MainFolder\FolderThree'
get-childitem -recurse $arrPath

Copying Folders with Wildcards

I am trying to copy a whole bunch of files using Powershell, from one directory to another on my computer.
I used Get-ChildItem C:\Users\Tom\Google Drive\My Files\*\Assessment 1\* to identify that this was the path that I wanted to copy too, and I know about Copy-Item, but I want to maintain parts of the path name when copied.
Example:
If I copy from C:\Users\Tom\Google Drive\My Files\Cool Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\Cool Stuff\Assessment 1
Whereas if I copy from C:\Users\Tom\Google Drive\My Files\New Stuff\Assessment 1\*
I want the files to go to a folder that is created called C:\Users\Tom\Archive\New Stuff\Assessment 1
You could use the Get-ChildItem cmdlet to recursively find all Assessment 1 folders within your base directory and then remove the base path using -replace to finally copy the items using the Copy-Item cmdlet:
$baseDir = 'C:\Users\Tom\Google Drive\My Files\'
$destination = 'C:\Users\Tom\Archive\'
Get-ChildItem $baseDir -directory -Filter 'Assessment 1' -Recurse | ForEach-Object {
$newPath = Join-Path $destination ($_.FullName -replace [regex]::Escape($baseDir))
Copy-Item $_.FullName $newPath -Force -Recurse
}

PowerShell remove last sub folder

Is there a way how to remove only last empty folder with PowerShell?
Example: I have a folder structure
..-mainFolder
...................-subFolder1
........................................- a
........................................- b
........................................- c
..................-subFolder2
........................................- a
........................................- b
Every night with robocopy i copy everything to another server and afterword i should delete all last sub folders (a,b,c, etc..).
With /MUVE it removes "subFolder1" & "subFolder2" but they should stay there
(if i remove folders "a", "b", "c" the "subFolder1" is empty too so i cant delete all empty folders.)
I cant use /FX and i don't know the name of folders just root directory path "C:\SharedFolders\". and i know that the folders that should be removed is in 3rd level.
You could use the Get-ChildItem cmdlet with the -Directory switch to retrieve all folders, filter the empty folders using the Test-Path cmdlet and finally delete the folders using Remove-Item:
Get-ChildItem 'C:\SharedFolders' -Directory -Recurse |
where { -not (Test-Path (Join-Path $_.FullName '*')) } |
Remove-Item
This will remove only the last empty folders, result:
..-mainFolder
...................-subFolder1
..................-subFolder2
You can use Get-ChildItem -Recurse to retrieve all folders, then call the GetFiles() and GetDirectories() methods on the directory objects to determine if they are empty:
$EmptyDirs = Get-ChildItem C:\path\to\mailFolder -Directory -Recurse | Where {-not $_.GetFiles() -and -not $_.GetDirectories()}
# and then remove those
$EmptyDirs | Remove-Item

Expand directory list and copy

I need to copy the following subset of folders from $sourceDir to $targetDir:
abc0001
abc0643
abc0456
...
The number of folders is unknown, but they all match a pattern abc0*.
Is there an elegant solution to expand abc0* to the actual list of folders and then copy them? I tried this:
dir "$sourceDir\abc0*" -Recurse | copy -Destination $targetDir -WhatIf
But it does not preserve the path, so all files end up in the root of $targetDir.
Give this a try:
dir $sourceDir abc0* | where {$_.psiscontainer} | copy -dest $targetDir -recurse

Powershell Get-ChildItem -recurse doesn't get all items

I'm working on a powershell script erase certain files from a folder, and move the rest into predefined subfolders.
My structure looks like this
Main
(Contains a bunch of pdb and dll files)
-- _publish
--Website
(Contains a web.config, two other .config files and a global.asax file)
-- bin
(Contains a pdb and dll file)
-- JS
-- Pages
-- Resources
I want to remove all pdb, config and asax files from the entire file structure before I start moving them. To which I use:
$pdbfiles = Get-ChildItem "$executingScriptDirectory\*.pdb" -recurse
foreach ($file in $pdbfiles) {
Remove-Item $file
}
And so on for all filetypes I need removed. It works great except for a pdb file located in the bin folder of the website. And for the ASAX file in the website folder. For some reason they get ignored by the Get-ChildItem recurse search.
Is this caused by the depth of the items within the resursive structure? Or is it something else? How can I fix it, so it removes ALL files as specified.
EDIT: I have tried adding -force - But it changed nothing
ANSWER: The following worked:
$include = #("*.asax","*.pdb","*.config")
$removefiles = Get-ChildItem "$executingScriptDirectory\*" -recurse -force -include $include
foreach ($file in $removefiles) {
if ($file.Name -ne "Web.config") {
Remove-Item $file
}
}
Get-ChildItem -path <yourpath> -recurse -Include *.pdb
You can also use the pipe for remove:
Get-ChildItem -path <yourpath> -recurse -Include *.pdb | rm