Link to home
Start Free TrialLog in
Avatar of Filip Heens
Filip HeensFlag for Belgium

asked on

is there a way to automatically answer NO to All in a powershellscript

is there a way to automatically answer NO to All in a powershellscript
I made a script to automatically delete files older than 7 days and empty folders.
But sometimes I get the question "Confirm
The item at Microsoft.PowerShell.Core\FileSystem::xxxxxx has
children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
I want to automaticaaly answer NO to ALL on this... but I can't seem to find how to do this...

the script I use is...

	$foldersfile = ".\folders.csv"
	$folderpaths = Import-CSV $foldersfile -delimiter ";"

        ForEach ($folderpath in $folderpaths){
                $path=$folderpath."folderpath"
                do{
                       Get-ChildItem -Path $Path -Filter 'thumbs.db' -Force -Recurse |   remove-Item -force
                       $dirs = gci $path -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
                       $dirs | Foreach-Object { Remove-Item $_ }
                       #delete the thumbs.db files
                       write-host "folder done"
                       Get-ChildItem -Path $path -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-7))} | remove-Item
                       write-host "files done"
                } while ($dirs.count -gt 0) #end doWhile
        }#end foreach

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

Should be enough to add the -File argument to the second Get-ChildItem, since you're only looking for files there anyway, and testing against a folder's LastWriteTime doesn't really work.
But other than that, you should delete the files first, then the empty folders. There's no need to check the folder again and again for files.
Try it like this:
$foldersfile = ".\folders.csv"
$folderpaths = Import-CSV $foldersfile -delimiter ";"

$daysAgo = (Get-Date).AddDays(-7)
ForEach ($folderpath in $folderpaths) {
	$path = $folderpath."folderpath"
	# delete the thumbs.db files
	Get-ChildItem -Path $Path -Filter 'thumbs.db' -Force -Recurse | Remove-Item -Force
	Write-Host "thumbs.db done"
	# delete old files
	Get-ChildItem -Path $Path -File -Recurse | Where-Object {$_.LastWriteTime -lt $daysAgo} | Remove-Item -Force
	Write-Host "files done"
	While ($emptyDirs = Get-ChildItem -Path $path -Directory -Recurse | Where-Object {(Get-ChildItem -Path $_.FullName).Count -eq 0} | Select-Object -ExpandProperty FullName) {
		$emptyDirs | ForEach-Object {Remove-Item -Path $_}
		Write-Host "folder done"
	}
}

Open in new window

Avatar of Filip Heens

ASKER

thanks oDdA also ;-)