Robb Hill
asked on
Powershell - to delete file folders on network share
I need a powershell script that can do the following:
It will iterate through a network share all the folders from a provided path.
So lets say the path is: \\company\extranet\clients
What I would want to do is iterate though every folder and subfolder/s in this directory structure.
Based on modified date being a given date range..in this example we can use the month of March.
If there are no "FILES" in the root folder then delete and log the folder name.
if there are Files in the root folder then log the folder name and count of files
The file structure after this is a number or letter.
\company\extranet\clients\ 1\1001
\company\extranet\clients\ 1\1002
\company\extranet\clients\ 1\1003
\company\extranet\clients\ 1\1004
or
\company\extranet\clients\ s\sharry
\company\extranet\clients\ h\hamilton business
These folders at this level would be what we would log and delete.
All the files and subfolders with files would be all under this level.
Please help
It will iterate through a network share all the folders from a provided path.
So lets say the path is: \\company\extranet\clients
What I would want to do is iterate though every folder and subfolder/s in this directory structure.
Based on modified date being a given date range..in this example we can use the month of March.
If there are no "FILES" in the root folder then delete and log the folder name.
if there are Files in the root folder then log the folder name and count of files
The file structure after this is a number or letter.
\company\extranet\clients\
\company\extranet\clients\
\company\extranet\clients\
\company\extranet\clients\
or
\company\extranet\clients\
\company\extranet\clients\
These folders at this level would be what we would log and delete.
All the files and subfolders with files would be all under this level.
Please help
The spec is a bit confused, this is my interpretation.
# Loop through folders like \company\extranet\clients\1\1001
Get-Item \\company\extranet\clients\* -Directory | ForEach-Object {
# Get the file count in this directory (only in this directory, not subfolders)
$fileCount = (Get-ChildItem $_.FullName -File | Measure-Object).Count
# Create the log entry
[PSCustomObject]@{
Directory = $_.FullName
FileCount = $fileCount
}
if ($fileCount -eq 0) {
# Delete the folder if there are no files (at this level).
# Remove-Item $_.FullName -Recurse -Force
}
} | Export-Csv CleanFolders.csv -NoTypeInformation
The Remove-Item command is commented out to allow you to test this safely before letting it change anything.
ASKER
This looks mostly correct.
as far as the date I am just referring to the modified date on the root folder.
I would want to traverse through the subfolders.
So for example if I manually clicked on a file folder under the Clients directory...lets say clients\1\1001
and did properties....It might say 10 files.
Those 10 files could be in subfolders within the 1001 folder.
So if there are any files under that level of the directory structure I would not want to delete.
as far as the date I am just referring to the modified date on the root folder.
I would want to traverse through the subfolders.
So for example if I manually clicked on a file folder under the Clients directory...lets say clients\1\1001
and did properties....It might say 10 files.
Those 10 files could be in subfolders within the 1001 folder.
So if there are any files under that level of the directory structure I would not want to delete.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You mention a date but in the rest of the question you never reference the modified date.
you say root folder but root folder is \company do you just want \company\extranet and below and only folders that are non-numeric?