Delete folders by using a text file or similar

Dear all,

I have quite a few folders that I need to delete in a directory. I currently have list of folder names to which require deletion. I was wondering if there was a scripting way if I could have say a text list, either csv,txt or similar that could be read and folders deleted accordingly?

A background story behind this.. I had a folder hit by a virus revently. Fortunatley, not all folders were affected. With the help of experts from EE, I've managed to export the folder contents into a list (csv) using Powershell and have analysed the data. I'am now left with names of the folders that have been affected.

Any assistance would be greatly appreciated.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Subash SundharanIT Infrastructure Architect Commented:
Try this PowerShell code
GC C:\Input.txt | %{Remove-Item $_ -Recurse -Force}

Open in new window

C:\Input.txt file format..

Open in new window

The code will delete all the sub folders and contents of the folders listed in the text file..

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
Bill PrewIT / Software Engineering ConsultantCommented:
Here is a BAT script approach.  It reads the lines of the specified text file, and removes the entire directory tree below the specified folder, and all files.

If the list file has the full path to the folders, then this will work:

@echo off
set ListFile=c:\temp\list.txt
for /f "usebackq tokens=*" %%A in ("%ListFile%") do rd /s /q "%%~A"

Open in new window

If the list file has just the name of the folders, and not the parent path where they are located, then this would handle that:

@echo off
set ListFile=c:\temp\list.txt
set BaseDir=d:\folder
for /f "usebackq tokens=*" %%A in ("%ListFile%") do rd /s /q "%BaseDir%\%%~A"

Open in new window

N00b2015Author Commented:
thanks Subsun and Bill!  both scripts worked well. saved me loads of time. :)
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

From novice to tech pro — start learning today.