Delete Folders Based on Folder Name - Windows Server 2008 R2

Hello,
We have a windows server 2008 server. We have a share with a lot of folders. The subfolders inside these folders are labeled by date (ex 1-1-2015). Is there a way to run a script that will delete the folders based on their date? I know Windows sees is as a text name and not date. So, to me it sounds very complex. But please let me know how I can do this if possible. If not, is there a way to delete the subfolders (and its contents) by creation date?

Thank You!

EzraS
eshiramAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
you will need to use some sort of scripting or manually using explorer and searching by date. I tend to find that last accessed date is more valuable to retain than creation/modified  date.

#$debugpreference = "Continue" 
$pathtosearch = "F:\iTunes Media"
$files = Get-ChildItem $pathtosearch -Recurse
$olddays = 90
$olddate = (Get-Date).AddDays(-($olddays))
$olddate
foreach ($file in $files) {
if ($file.LastAccessTime -ge $file.CreationTime)
    {$latesttime = $file.LastAccessTime
    }
    else {
    $latesttime = $file.CreationTime
    }
    if ($latesttime -le $olddate) {
        #remove-item $file.FullName -WhatIf
         write-debug("{0} {1}" -f $file.Name, $file.CreationTime)
        }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eshiramAuthor Commented:
Hi David,
Thanks for the response. I ran the script (I changed the path to search and olddays to my needs). But when I ran it, it said "Delete.vbs(1, 1) Microsoft VBScript compilation error: Expected statement". What does this mean? I really don't know anything about scripting. My knowledge of scripting is as far as mapping network drives via login scripts.

Thanks!
0
eshiramAuthor Commented:
Also, I tried running two scripts (one to delete by folders and the second by files. The one by folders did not work for me, which is "forfiles -p "C:\Users\guest\Downloads" -d -60 -c "cmd /c IF @isdir == TRUE rd /S /Q @path"". However, when running the one to delete by files "forfiles /p "C:\Users\guest\Downloads" /s /d -60 /c "cmd /c del /s /q @file"", it does work. As it runs it will delete the files and leave the folders empty.

It doesn't really matter which scripting method I use as long as it works.

Thanks Again!
0
Bill PrewCommented:
Do you still need help with this?

One thing I noticed:
Thanks for the response. I ran the script (I changed the path to search and olddays to my needs). But when I ran it, it said "Delete.vbs(1, 1) Microsoft VBScript compilation error: Expected statement". What does this mean? I really don't know anything about scripting. My knowledge of scripting is as far as mapping network drives via login scripts.
It looks like you took the first solution and tried to run it as a VBS (vbscript) item, but it was actually a PS1 file (powershell).  You can try renaming it to a PS1 extension and running it.

If you still need help let me know, I probably have some actual vbscript solutions that I could adapt.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.