如果不使用,则删除文件夹和子文件夹(Powershell) [英] delete folder and subfolder if is not used (Powershell)
问题描述
我是PowerShell的初学者。
I`m a beginner PowerShell user.
我需要一个powershell脚本。它必须这样做:
I need a powershell script. It must do:
在
\\routefolders\xxxxx
\\routefolders\jhon
\\routefolders\mike
\\routefolders\sandra
.
.
.
etc...
查找2年以下的文件
如果\routefolders\xxxxx中存在某个文件,则 XXXXX(搜索所有SUBFOLDERS)不执行任何操作
if exist some file in \routefolders\xxxxx "XXXXX" (searching in ALL SUBFOLDERS) do nothing
如果没有文件小于2年的文件会打印并删除文件夹和子文件夹\routefolders\xxxxx
if there is no file newer than 2 years print in a file and delete the folder and subfolders \routefolders\xxxxx
XXXXX文件夹是用户个人文件夹,如果用户不使用它,我想保存日志,然后删除该文件夹
The "XXXXX" folders are user-personal folders, if the user not use this i want to save a log and then to delete this folder
我有此功能,可以删除特定的文件夹。我需要这样的PS:
I have this, to delete a specific folder. I need some PS like this:
$RootDir="c:\temp\test"
$Users = Get-ChildItem $RootDir -Name
$FolderToDelete = "subcarpeta1"
foreach ($user in $Users){
$n = $n +1
write-host $n $user
#Remove-Item -path $rootdir\$user\$FolderToDelete -Force -Recurse
}
推荐答案
您可以尝试以下方法:
function Safely-Delete($fileItem)
{
if($fileItem.FullName.StartsWith("your path"))
{
Set-Location $fileItem.FullName
Write-Host "Removing Item: $fileItem"
Remove-Item $fileItem.FullName -Recurse
}
}
function Remove-File($path, $ageInYears)
{
foreach($childItem in Get-ChildItem -Path $path)
{
if($childItem.Lastwritetime -lt (date).AddYears(-$ageInYears))
{
Safely-Delete $childItem
}
}
}
$age = 2
Remove-File "your path" $age
这篇关于如果不使用,则删除文件夹和子文件夹(Powershell)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!