Author Posts

March 21, 2018 at 9:20 am

Hi Team,
I am writing a script which checks if a specific sub-folder exists in the list of folders and if so get the list of files older than 2 days from that sub-folder. Below is my folder structure and script. however the Test-path condition always returns false and code inside it doesn't gets executed.

Dir Structure:
f:\test\123\Avengers
f:\test\123\Justiceleague
f:\test\456\Avengers
f:\test\456\Justiceleague
f:\test\789\Avengers
f:\test\789\Justiceleague

SCript:

$folder = gci -Path F:\test\
foreach ($f in $folder)
{
foreach ($i in gci $f.FullName)
{
$i.FullName
# check if the there is a folder called Avengers
if (Test-Path ($i.FullName -contains 'Avengers'))
{
write-host " Avengers folder exist"
Get-ChildItem -Path $i.FullName -File | Where lastwritetime -lt (Get-Date).AddDays(-2)
}
}
}

March 21, 2018 at 10:22 am

I was able to get it using fix path.