Solved

Delete Folders from a text file

Posted on 2010-11-18
9
858 Views
Last Modified: 2012-05-10
I've got a PS script to delete files and that works fine (see attached)

What i'm now looking to add to it is a Get-Content for it.

I have a Text file that contains a whole list of names, which will be folder names.  How can i get the Delete to only look for those folders and delete them and any sub folder/files that may reside in them?

NB: currently in the script you'll see ive got ti set up to only include TMP files obviously this will not be the case for this as i'll want it to delete all folders/files from with the named folders
$drive = new-object -com wscript.network
$drive.MapNetworkDrive("Y:", "\\<Server name>\E$")
get-childitem y:\ -include *.tmp -recurse -errorAction silentlyContinue| foreach ($_) {remove-item -recurse $_.fullname -whatif}
$drive = New-Object -com WScript.Network
$drive.RemoveNetworkDrive("Y:")

Open in new window

0
Comment
Question by:Colchester_Institute
  • 4
  • 4
9 Comments
 
LVL 1

Expert Comment

by:tuxnani
Comment Utility
rm command is supposed to have very powerful nature and can, in recursive mode, delete the files and directory as well.
so use
rm -R .
for the purpose
0
 
LVL 1

Author Comment

by:Colchester_Institute
Comment Utility
thats not really telling me how to get the names from a text file.

I've currently written an old script using the RMDIR /S /Q parameters (see Attached)

but ideally was looking for how to do this using powershell


REM - Map drive to server

net use L: \\<server name>\E$

REM - Delete Profiles

For /f %%N in (name.txt) DO CALL rmdir /S /Q L:\<folder1>\<folder2>\%%N

REM - Delete User Files

For /f %%N in (Name.txt) DO CALL rmdir /S /Q L:\<folder1>\<folder2>\%%N

REM - Remove Mapped Drive

Net use L: /delete

Open in new window

0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

Something like this:
Get-Content YourList.txt | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Recurse -Force -WhatIf
  }
}

Open in new window

HTH

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
Comment Utility
ok i think that will work and ive added on the mapping to the drive command.

Just two things i've noticed.  How can i get it not to display errors

i.e. its coming up red telling me that so and so folder i dont have permissions well thats ok as thats not the folder i'm trying to delete??? so should it not just skip it with out even telling me?

and then when it got to the folder it was meant to delete it tells me the fully qaulified file name must be less than 260 characters and the directory name must be less than 248...how can i adjust this so it just deletes everything even if 204-260+ in length
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility

> permissions well thats ok as thats not the folder i'm trying to delete???

It'll be permission to access the folder to read it, we're traversing all folders (-Recurse) looking for specific folders with the name you define.

The error can be suppressed by adding -ErrorAction SilentlyContinue to Get-ChildItem. e.g.
Get-Content YourList.txt -ErrorAction SilentlyContinue | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Force -WhatIf
  }
}

Open in new window

I've removed the -Recurse flag from Remove-Item. Lets see if it'll just trash the entire tree from there.

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
Comment Utility
ohh so close yet so far...

Now it brings up the ask box for me to say yes, yes to all, no, no to all and suspend.

Also the get-childitem is still trying to access other folders not specifed but returning the error the Access to path is denied

which is fine as it shouldnt eve n be attempting to look at those paths?
0
 
LVL 70

Expert Comment

by:Chris Dent
Comment Utility
Ah, I'm sorry I put the ErrorAction flag in the wrong place.
Get-Content YourList.txt | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse -ErrorAction SilentlyContinue | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Force -WhatIf
  }
}

Open in new window

Good job it's almost time to go home :)

Chris
0
 
LVL 1

Author Comment

by:Colchester_Institute
Comment Utility
ok i think thats solved it barring the fact it prompts me for a response to see if i want to delete.  In otherwords it dont just go ahead and do it
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 250 total points
Comment Utility

Upgraded again :)

Note that this version really does delete, no more -WhatIf.

Chris
Get-Content YourList.txt | ForEach-Object {
  # Find a folder in $Path with the name of the folder
  Get-ChildItem $Path -Filter $_ -Recurse -ErrorAction SilentlyContinue | ForEach-Object {

    # For each folder it finds, delete it and all sub-folders
    # Note that WhatIf will have this tell you about the change, it will not delete anything while this is present
    Remove-Item $_.FullName -Force -Confirm:$False
  }
}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now